Question: C++ question. When I run my code, it's not printing the deck like it should. I need it to say Pick a Card on the
C++ question. When I run my code, it's not printing the deck like it should. I need it to say Pick a Card on the first line and then on the next line it should say The Card Deck.
The cards should print like Two of Clubs, Three of Clubs, and so on. Mine is coming out 0 space 0, 0 space 1, and so on.
Here is my code.
#include
#include
#include
using namespace std;
enum suits { CLUBS, DIAMONDS, HEARTS, SPADES };
enum cardValues { TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE };
struct cards {
suits suit;
cardValues card;
};
cards deck[52];
cards card1, card2;
void createDeck(cards card[]) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
card[13 * i + j].suit = (suits)i;
card[13 * i + j].card = (cardValues)j;
}
}
}
void printCards(cards card) {
switch (card.card) {
case TWO:
cout << "TWO";
break;
case THREE:
cout << "THREE";
break;
case FOUR:
cout << "FOUR";
break;
case FIVE:
cout << "FIVE";
break;
case SIX:
cout << "SIX";
break;
case SEVEN:
cout << "SEVEN";
break;
case EIGHT:
cout << "EIGHT";
break;
case NINE:
cout << "NINE";
break;
case TEN:
cout << "TEN";
break;
case JACK:
cout << "JACK";
break;
case QUEEN:
cout << "QUEEN";
break;
case KING:
cout << "KING";
break;
case ACE:
cout << "ACE";
break;
}
cout << " of ";
switch (card.suit) {
case CLUBS:
cout << "CLUBS";
break;
case DIAMONDS:
cout << "DIAMONDS";
break;
case HEARTS:
cout << "HEARTS";
break;
case SPADES:
cout << "SPADES";
break;
}
cout << " ";
}
void printDeck(cards card[]) {
for (int i = 0; i < 52; i++) {
cout << card[i].suit << " " << card[i].card << endl;
}
cout << endl;
}
void deal(cards deck[], cards &card) {
int randomCard = (rand() % 52) + 1;
card = deck[randomCard];
}
void winner(cards card1, cards card2) {
for (int i = 0; i < 52; i++) {
if (card1.suit == deck[i].suit && card1.card == deck[i].card) {
cout << " Card 2 wins";
printCards(card2);
return;
}
}
}
int main() {
createDeck(deck);
printDeck(deck);
deal(deck, card1);
cout << " Card 1 = ";
printCards(card1);
deal(deck, card2);
cout << " Card 2 = ";
printCards(card2);
cout << " And the winner is ";
winner(card1, card2);
system("pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
