Home > Software design >  How to print the elements of an array of structures?
How to print the elements of an array of structures?

Time:03-07

I am new to c , I was trying to print the fields of a structure using an array.

I know the compiler is not able to understand what is inside deck[1] in the line for(int x:deck[1]), since deck[1] is an unknown data type (a structure datatype defined as 'card'). So x is not able to scan the elements within this data type.

I wish to know, how may I print the elements within this deck[1], i.e {1,1,1}.

#include <iostream>
using namespace std;

struct card
{
    int face;
    int shape;
    int color;
};

int main()
{
    
    struct card deck[52];
    
    deck[1].face=1;
    deck[1].shape=1;
    deck[1].color=1;
    
    cout<< sizeof(deck)<<endl;
    
    for(int x:deck[1])
    {
        cout<<x<<endl
    }

    return 0;
}

CodePudding user response:

Note that you can't loop through the data members of an object of a class type such as card. You can only print out the data members individually. So you can use operator overloading to achieve the desired effect. In particular, you can overload operator<< as shown below.

#include <iostream>
struct card
{
    int face;
    int shape;
    int color;
    
    //overload operator<<
    friend std::ostream& operator<<(std::ostream &os, const card& obj);
};
std::ostream& operator<<(std::ostream &os, const card& obj)
{
    os << obj.face << " " << obj.shape << " " << obj.color;
    return os;
}
int main()
{
    
    card deck[52] = {};
    
    deck[1].face=1;
    deck[1].shape=1;
    deck[1].color=1;
    
    std::cout<<deck[1].face <<" "<<deck[1].shape <<" "<<deck[1].color<<std::endl;
    
    //use overloaded operator<<
    std::cout << deck[1]<<std::endl;

}

The output of the above program can be seen here:

1 1 1
1 1 1

CodePudding user response:

There are many ways to do this. Here's two ways.

1.

cout << deck[1].face << " " << deck[1].shape << " " << deck[1].color << endl;
  1. Overload the << operator to support printing card to cout:
std::ostream &operator<<(std::ostream &os, card const &c) { 
    return os << c.face << " " << c.shape << " " << c.color << endl;
}

Then you can just do

cout << deck[1] << endl;

CodePudding user response:

I would probably add a method for getting a string representation of a card object:

#include <sstream>

struct card
{
    // ...
    std::string toString() const
    {
        std::stringstream str;
        str << "card[face=" << face << ", shape=" << shape << ", color=" << color << ']';
        return str.str();
    }
};

Then just print the result:

int main()
{
    // ...
    std::cout << deck[1].toString() << '\n';
    // ...
}

(Note that I use '\n' instead of std::endl, because std::endl flushes the output buffer, which has a slight performance impact.)

You could also overload operator<< to remove the toString() method call:

std::ostream& operator<<(std::ostream& os, const card& c)
{
    return os << c.toString();
}

Now you could probably do some pointer hackery if you really want to treat your card object as an array, but I wouldn't recommend it as such workarounds often result in just messier and less safe code.

https://en.cppreference.com/w/cpp/io/basic_stringstream https://en.cppreference.com/w/cpp/language/operators#Stream_extraction_and_insertion

  • Related