Enum Connection to Input?


I am trying to define a card class for a card game and am having trouble connecting the values provided to the constructor to some predefined enum types.

If I defined two enum types to be:

enum Rank {Two, Three, [..other cards..] Jack, Queen, King, Ace};
enum Suit {Spades, Diamonds, Clubs, Hearts};

And a constructor which took in two chars representing the rank and the suit (because the user would enter it this way via standard input):

Card (Rank r, Suit s);

e.g. Card(J,D) -> Is internally equivalent to Card(Jack, Diamonds)

How do I make that connection/conversion?

I am also given:

string Card::RankName = "23456789TJQKA";
string Card::SuitName = "SDCH";

Any help would be much appreciated!

Let consider for example enumeration enum Suit {Spades, Diamonds, Clubs, Hearts};

In fact though Spades, Diamonds, Clubs, Hearts have type Suit in C++ they can be implicitly converted to an integral type and their values correspond to 0, 1, 2, 3.

Now consider definition string Card::SuitName = "SDCH";
'S' shall correspond to 0, 'D' - 1, 'C' - 2, and 'H' - 3. How to get these values? If for example the user will enter some character you should check whether this character is valid. You can use string member function find. For example

char c = 'D'; // let assume that this character is a user input.

if ( ( std::string::size_type n = SuitName.find( c ) ) != std::string::npos )
   Suit s = static_cast<Suit>( n );
Last edited on
Topic archived. No new replies allowed.