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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
/*
Using command line arguments read in the file called Deck.txt
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <fstream>
using namespace std;
//Enum types for Card objects
enum CardSuit { CLUBS, DIAMONDS, HEARTS, SPADES };
enum CardRank {ACE = 1, TWO, THREE ,FOUR, FIVE ,SIX, SEVEN , EIGHT, NINE, TEN, JACK , QUEEN, KING };
class Card
{
public:
CardSuit suit;
CardRank rank;
};
//Create two string arrays to use for overloaded operator << function
string ranks[] = { "", "Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King" };
string suits[] = { "Clubs", "Diamonds", "Hearts", "Spades" };
ostream& operator << (ostream & os, Card const& c) {
os << ranks[c.rank] << " of " << suits[c.suit];
return os;
}
void print(vector<Card> const& v)
{
for (size_t i = 0; i < v.size(); ++i)
{
cout << (i + 1) << "." << v[i] << endl;
}
cout << endl;
}
istream& operator >> (istream& is, Card& c)
{
if (!is)
return is;
string rank, garbage, suit;
is >> rank >> garbage >> suit;
for (unsigned i = 1; i < (sizeof(ranks) / sizeof(ranks[1])); ++i)
{
if (rank == ranks[i])
c.rank = CardRank(i);
//cout << c.rank << endl;
}
for (unsigned i = 0; i < (sizeof(suits) / sizeof(suits[0])); ++i)
{
if (suit == suits[i])
c.suit = CardSuit(i);
}
return is;
}
int main(int argc, char* argv[])
{
ifstream infile(argv[1]);
//Test for success
if (!infile)
{
cerr << "Input file opening failed.\n";
return EXIT_FAILURE;
}
//Load a vector with the Card data
vector<Card> deck;
Card c;
while (infile >> c)
{
deck.push_back(c);
}
print(deck);
}
|