We want to define an enumeration to keep track of the suit of cards in a card ga
ID: 3724068 • Letter: W
Question
We want to define an enumeration to keep track of the suit of cards in a card game. In this game, clubs are the best suit, followed by diamonds, spades and then hearts.
Define a enumerated type Suit that defines CLUB, DIAMOND, HEART and SPADE so that their numerical values are correctly ordered for this game. Note: the correct order is NOT CLUB, DIAMOND, HEART, SPADE... put them in the order so that "better suits" are higher numbers than worse suits.
Define a const string array SUIT_NAMES that we can use to look up the right string ("Club", "Diamond", "Heart" or "Spade") for each suit.
NOTE: I did part of this but am stuck on how to write the function. How should I do this? Thanks in advance!
#include <iostream>
using namespace std;
//Do not modify anything on or above the line below this
//YOUR_CODE_BELOW
enum Suit {CLUB = 3, DIAMOND = 2, SPADE = 1, HEART = 0};
const string SUIT_NAMES()
{
}
//YOUR_CODE_ABOVE
//Do not modify anything on or below the line above this
Suit getSuit() {
string s;
cin >> s;
if(s == "Diamond")
return DIAMOND;
if(s == "Club")
return CLUB;
if(s == "Heart")
return HEART;
if(s == "Spade")
return SPADE;
}
int main()
{
Suit s1 = getSuit();
Suit s2 = getSuit();
Suit winner = max(s1, s2);
cout << SUIT_NAMES[winner] << endl;
}
Explanation / Answer
If I am not wrong, this program will output the winner among the two suits being entered. E.g. if s1 is diamond(2) and s2 is spade(1) then winner will be max(2,1) i.e. 2.. Now to get what 2 represents we want to use SUIT_NAMES[].
So it seems, you are defining a function SUIT_NAMES() but actually it should be a constant array as below(replace function code with below statement):
const string SUIT_NAMES[4] = {"Heart", "Spade", "Diamond", "Club"};
So SUIT_NAMES[winner] = SUIT_NAMES[2] = Diamond
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.