Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

How to declare, initialize, and print struct with a float array in C++

1 Answer

0 votes
#include <iostream>

struct Player {
    float x[4];
    float y[4];
    int red, green, blue;
    bool up, down;
};

void printPlayer(const Player& player) {
    std::cout << "x: ";
    for (const auto& val : player.x) {
        std::cout << val << " ";
    }
    std::cout << "\ny: ";
    for (const auto& val : player.y) {
        std::cout << val << " ";
    }
    std::cout << "\nred: " << player.red 
              << "\ngreen: " << player.green 
              << "\nblue: " << player.blue 
              << "\nup: " << (player.up ? "true" : "false")
              << "\ndown: " << (player.down ? "true" : "false") << std::endl;
}

int main() {
    Player player = {
        {-1.0f, -1.0f, -1.0f, -1.0f}, // x[4]
        {-1.0f, -1.0f, -1.0f, -1.0f}, // y[4]
        0x0, 0x00, 0x0,               // red, green, blue
        false, false};                // up, down

    printPlayer(player);
}


/*
run:

x: -1 -1 -1 -1 
y: -1 -1 -1 -1 
red: 0
green: 0
blue: 0
up: false
down: false

*/

 



answered Jan 18, 2025 by avibootz

Related questions

2 answers 207 views
1 answer 128 views
...