Reading user input and comparing it to array

I am making a simple game where a user inputs a hand of cards and the program evaluates the hand, giving it a score.

A user should input a card hand of 13 cards as a string, where "JD" is Jack of Diamonds and "10S" is 10 of Spades and each card is separated by a space, comma, or newline.
- Check whether user has correctly formatted cards.
- Check whether user has 13 cards.
- Check whether there are duplicated cards.

The main problem is I don't know how to define the cards so that it can be used for all these requirements. I was thinking of an array of 52 cards, but I'm not sure how to translate user input into these cards if they are in the proper format mentioned above. At the moment I have a single string of the entire user input. I feel like I need to use functions, but I don't know where they are appropriate except to check for what characters that should not appear in a hand of cards.

Here are my attempts for each of the requirements--I would like to know if there's a simpler/cleaner method for each of my approaches that doesn't involve too complex of an implementation as I'm still a beginner.

First requirement: "If there is any character other than a number or letter that is not D,S,H, or C, then the cards are incorrectly formatted. If the first character is a number, it is incorrectly formatted. When coming across D, S, H, or C, the next element in the array (i.e. next char) must be an integer that is not 0. Etc...

Second requirement: "Traverse through the array and keep a counter of cards that are separated by a space, comma, or newline."

Third requirement: No idea how to keep track of duplicate cards at the moment, especially since I don't know how cards can be defined in an easy-to-understand implementation.

I appreciate any sort of help, really stuck at the moment.
Last edited on
Your first decision is how the user should indicate that the input is complete. If the program reads at most 13 cards and then reads some other input, but the user types more than 13 cards, then the excess cards are used by the program as "some other input", which probably fails.

Perhaps an empty line?


An another decision is when to do the checking. During input or after?
After is simpler. You take all input and then state whether it is valid or not.
During allows telling the user immediately about the error and continuing input operation until player has given exactly 13 valid unique cards.

The input is string. Words. You can easily generate a dictionary of valid words -- the deck. If a word from input can be found in dictionary, then it is a valid card. The dictionary could contain a value of each card as additional attribute; used for scoring.

You store each valid card into "hand". You can check whether hand already contains a new card before inserting the card into hand.


Are you familiar with the C++ Standard Library, its containers and algorithms?
Topic archived. No new replies allowed.