Program to generate a card deck

//So far this program generates a card deck, shuffles it, and outputs the card
//Header file----------------------------------------------------------------------------------------

//always include iostream
#include <iostream>
//random number generator
#include <cstdlib>
//random number generator
#include <ctime>

using namespace std;

//Initilize constant variables
const int SIZE = 52;

//create a class called card
class card
{
public:
//default constructer
card();
//constructer with parameters
card(string cardFace, string cardSuit);
//print function
string print();

private:
//create two card variables face and suit
string face;
string suit;

};

//create a class for deckOfCards
class deckOfCards
{
public:
// Default constructor: assigns the 52 cards to deck
deckOfCards();
//shuffles the deck once all the cards are assigned
void shuffle();
//deals out one card from the deck of 52, refrences class card
card dealCard();

private:
//variable card with a pointer to deck
card deck[SIZE]; // an array of cards of size SIZR
//keep track of what card you are dealing with
int currentCard;
};

//Main function
//-------------------------------------------------------------------------------------------------
int main()
{

//declare deckofcards(from class) called deck
deckOfCards deck;
//declate car called(from class) current card
//card currentCard;
//shuffle the deck that you just initalized
deck.shuffle();
//determine how many cards you want to print out to the user
//right now it is two because we decided that each player will get two cards when they start
for( int i = 0; i <= 2; i++)
{
//set current card equal to deck.dealcard
//the return value will replace it
currentCard = deck.dealCard();
//include to actually print out current card in the format we want
cout << currentCard.print() << endl;

}


return 0;
}
//------------------------------------------------------------------------------------------------//
//file implementation

//default constructor
card::card()
{
//nothing goes in here
}

//constructor with two parameters
card::card(string cardFace, string cardSuit)
{
//assigns the paraments above to the two variables face and suit
face = cardFace;
suit = cardSuit;
}

//print function defintion
string card::print()
{
//return the way the card will be displayed
return (face + " of " + suit);
}

//assigns the 52 cards to deck
deckOfCards::deckOfCards()
{
//put all the face values in an array as strings
string faces[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
//put all the suit values in an array as strings
string suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
//initilize deck from the card class to a new array using the word "new"
deck = new card[SIZE];
//current card is equal to zero
currentCard = 0;
//create a for loop to literally place each card into the new array
for(int count = 0; count < SIZE; count++)
{
//deck at postion count will be equal to card, each with a different face and suit
deck[count] = card(faces[count % 13], suits[count / 13]);
}

}


//shuffles the deck once all the cards are assigned
void deckOfCards::shuffle()
{
//start at current card
currentCard = 0;
//create a for loop so all 52 cards will be shuffled
for(int first = 0; first < SIZE; first++)
{
//create an int called second and set it equal to the random operator
int second = (rand() + time(0)) % SIZE;
//create an int called temp and set it equal to the deck at the first postiion
card temp = deck[first];
//swap deck at first and second postion
deck[first] = deck[second];
//swap deck and temp
deck[second] = temp;
}
}


card deckOfCards::dealCard()
{
//if we are out of cards
if(currentCard > SIZE)
{
//shuffle
shuffle();
}
//if we are not out of cards
if( currentCard < SIZE)
{
//return deck at that current card and then increment
return (deck[currentCard++]);
}
//return the first card in the deck that we just found
return (deck[0]);
}


here is my code, i tried to make it very detailed.
for some reason I keep getting these three errors and they don't make sense.
can someone please help :/

card.cpp:69:5: error: use of undeclared identifier 'currentCard'
currentCard = deck.dealCard();
^
card.cpp:71:13: error: use of undeclared identifier 'currentCard'
cout << currentCard.print() << endl;
^
card.cpp:110:10: error: array type 'card [52]' is not assignable
deck = new card[SIZE];
~~~~ ^
Last edited on
Error 1) currentCar is an 'int' and the function deck.dealCar() return a card type object. You have to make your dealCar() rerturn an 'int' or make currentCar a class object instead of an 'int'.

Error 2) you print function returns a string. you have to declare a string variable and make that variable call the print() function. Then you can cout that variable.
1
2
3
deckOfcards myobject;
string something;
something = myobject.print();



Error 3) Arrays won't take SIZE if you make it a const int. Instead do this:
1
2
//Initilize constant variables
#define SIZE 52 

This will substitute in 52 in place of SIZE and remember there is no semicolon (;) after 52
thank you!
Topic archived. No new replies allowed.