1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
enum Suit { Spades, Hearts, Diamonds, Clubs };
struct Card
{
Suit suit;
unsigned face;
};
std::ostream& operator<<(std::ostream& os, const Card& card)
{
const char* suits [] = {"Spades", "Hearts", "Diamonds", "Clubs"};
const char* faces [] = { "", "Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
const char* unknown = "Unknown";
const char* suit = (card.suit >= Spades && card.suit <= Clubs) ? suits[card.suit] : unknown;
const char* face = (card.face >= 1 && card.face <= 13) ? faces[card.face] : unknown;
return os << face << " of " << suit;
}
void print(const Card* deck, unsigned cards)
{
const Card* card = deck;
for (unsigned i = 0; i < cards; ++i)
std::cout << *card++ << '\n';
}
int main()
{
Card cards[52];
print(cards, 52);
std::cout << "\nWhat must we do with data before we use it? Initialize it? You don't say!\n";
}
|