Question: I'm supposed to make a program where it prints out an array of 52 card objects Example: Ace of Spades, Queen of Hearts, etc. This

I'm supposed to make a program where it prints out an array of 52 card objects

Example: "Ace of Spades", "Queen of Hearts", etc.

This is the code I have right now:

Card.h:

#pragma once #include #include using namespace std; class Card { private: int value; int suit; public: Card(); Card(int value, int suit); string getValue(); string getSuit(); void print(); };

Card.cpp:

#include #include #include "Card.h" using namespace std; Card::Card() { }; Card::Card(int value, int suit) { Card c; c.value = value; c.suit = suit; } string Card::getSuit() { switch (suit) { case 1: return "Hearts"; case 2: return "Diamonds"; case 3: return "Spades"; case 4: return "Clubs"; default: return "Undefined"; } } string Card::getValue() { switch (value) { case 1: return "Two"; case 2: return "Three"; case 3: return "Four"; case 4: return "Five"; case 5: return "Six"; case 6: return "Seven"; case 7: return "Eight"; case 8: return "Nine"; case 9: return "Ten"; case 10: return "Jack"; case 11: return "Queen"; case 12: return "King"; case 13: return "Ace"; default: return "Undefined"; } } void Card::print() { cout << getValue() << " of " << getSuit() << endl; }

main.cpp:

#include #include #include "Card.h" //reference: http://www.geeksforgeeks.org/shuffle-a-given-array/ using namespace std;

void shuffle(Card* cards[], int n) { int temp=0; srand(time(NULL)); for (int i = n - 1; i > 0; i--) { int j = rand() % (i + 1); cards[temp] = cards[i]; cards[i] = cards[j]; cards[j] = cards[temp]; } } int main() { Card* cards[52]; int x= 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 13; j++) { cards[x] = new Card(j, i); x++; } } int y = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 13; j++) { cards[y]->print(); y++; } } return 0; }

I believe the code should work fine but no matter what, I just get 52 lines of the same string: "Undefined of Undefined"

Can anyone point the problem to me?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!