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
|
#include <iostream>
#include <string>
using namespace std;
bool translateCard (string input, string & result);
int main()
{
string str, cardname;
cout << "Please enter a card notation: "; // The input is picked by user
cin >> str;
if (translateCard(str, cardname))
cout << cardname << endl;
else
cout << "Invalid input" << endl;
return 0;
}
bool translateCard (string input, string & result)
{
static const string rank = "A23456789TJQK";
static const string suit = "HSDC";
static const string rname[13] =
{ "Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King" };
static const string sname[4] =
{ "Hearts", "Spades", "Diamonds", "Clubs" };
result = "";
if (input.size() != 2) // Input must be exactly 2 chars
return false;
char a = toupper(input[0]); // first character
char b = toupper(input[1]); // second character
size_t apos = rank.find(a); // validate a and b
size_t bpos = suit.find(b);
if ((apos == string::npos) || (bpos == string::npos))
return false;
result = rname[apos] + " of " + sname[bpos];
return true;
}
|