I recommend the use of arrays of card/suit name to look up card names:
Now you can implement the ostream operator the following way:Code:#include <string> const std::string SuitNames[] = { "Clubs", "Spades", "Hearts", "Diamonds" }; const std::string FaceNames[] = { "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" }; // I also recommend the use of enumerations for suit and face value enum Suit { Clubs = 0, Spades = 1, Hearts = 2, Diamonds = 3 }; enum Face { Deuce = 0, Three = 1, Four = 2, Five = 3, Six = 4, Seven = 5, Eight = 6, Nine = 7, Ten = 8, Jack = 9, Queen = 10, King = 11, Ace = 12 }; struct Card { const Suit suit_; const Face face_; Card( Suit s, Face f ) : suit_( s ), face_( f ) { } }; bool operator==( const Card& lhs, const Card& rhs ) { return lhs.suit_ == rhs.suit_ && lhs.face_ == rhs.face_; }
Code:#include <iostream> std::ostream& operator<<( std::ostream& os, const Card& card ) { os << FaceNames[card.face_] << " of " << SuitNames[card.suit_]; return os; }




Reply With Quote