# c++ card program



## roryneil (Feb 21, 2005)

I am trying to write a program that takes a deck of cards and shuffles them. I can create the deck. Then I thought I had a good algorithm for shuffling. Now it's down to "bare bones" for testing purposes. I take one deck of cards (a class), put all those values into a blank deck, and put them back in random order.
Here you will see that all I want to do is take 1 card, and switch it with another. It works in the function, but the data is back to original after it leaves the function. What am I doing wrong? (BTW, this is a specification file, inplementation file and client file. The cin >> pause in the client file is just to keep from getting out of the program before I can even see what happened). Thanks if you can help:

// SPECIFICATION FILE (deck.h)

#include <string>

using namespace std;

struct Card
{
string strValue;
int numValue;
char suit;
bool isThere;
};

class Deck
{
public:
Deck();
// Create a standard deck of cards

Deck(bool empty);
// Create empty deck, no values, isThere = false*/

void NextCard(int count) const;

Card Shuffle(Deck empty);
// Enters an empty deck to shuffle to

private:
Card cards[52];
};

// IMPLEMENTATION FILE (deck.cpp)

#include "deck.h"
#include <iostream>
#include <string>

using namespace std;

Deck:eck()
{
int count = 0;

do
{
if (count <=13)
cards[count].suit = 3;
else if (count <= 25)
cards[count].suit = 4;
else if (count <= 38)
cards[count].suit = 5;
else
cards[count].suit = 6;

switch(count)
{
case 0 :
case 13 :
case 26 :
case 39 : cards[count].strValue = " A";
cards[count].numValue = 14;
cards[count].isThere = true;
break;
case 1 :
case 14 :
case 27 :
case 40 : cards[count].strValue = " K";
cards[count].numValue = 13;
cards[count].isThere = true;
break;
case 2 :
case 15 :
case 28 :
case 41 : cards[count].strValue = " Q";
cards[count].numValue = 12;
cards[count].isThere = true;
break;
case 3 :
case 16 :
case 29 :
case 42 : cards[count].strValue = " J";
cards[count].numValue = 11;
cards[count].isThere = true;
break;
case 4 :
case 17 :
case 30 :
case 43 : cards[count].strValue = "10";
cards[count].numValue = 10;
cards[count].isThere = true;
break;
case 5 :
case 18 :
case 31 :
case 44 : cards[count].strValue = " 9";
cards[count].numValue = 9;
cards[count].isThere = true;
break;
case 6 :
case 19 :
case 32 :
case 45 : cards[count].strValue = " 8";
cards[count].numValue = 8;
cards[count].isThere = true;
break;
case 7 :
case 20 :
case 33 :
case 46 : cards[count].strValue = " 7";
cards[count].numValue = 7;
cards[count].isThere = true;
break;
case 8 :
case 21 :
case 34 :
case 47 : cards[count].strValue = " 6";
cards[count].numValue = 6;
cards[count].isThere = true;
break;
case 9 :
case 22 :
case 35 :
case 48 : cards[count].strValue = " 5";
cards[count].numValue = 5;
cards[count].isThere = true;
break;
case 10 :
case 23 :
case 36 :
case 49 : cards[count].strValue = " 4";
cards[count].numValue = 4;
cards[count].isThere = true;
break;
case 11 :
case 24 :
case 37 :
case 50 : cards[count].strValue = " 3";
cards[count].numValue = 3;
cards[count].isThere = true;
break;
case 12 :
case 25 :
case 38 :
case 51 : cards[count].strValue = " 2";
cards[count].numValue = 2;
cards[count].isThere = true;
break;
}

count ++;

} while(count <= 51);
}

Deck:eck(bool empty)
{
int count = 0;

do
{
cards[count].isThere = false;
cards[count].strValue = " ";
cards[count].numValue = 0;
cards[count].suit = ' ';
count++;
} while (count <= 51);
}

void Deck::NextCard(int count) const
{
cout << cards[count].strValue;
cout << cards[count].suit << " ";
}

Card Deck::Shuffle(Deck emptyDeck)
{
int index = 4;

//for (index = 0 ; index <= 51 ; index++)
{
cout << endl << "First is now " << cards[index].strValue;
cout << endl << "Empty is now " << emptyDeck.cards[index].strValue;
emptyDeck.cards[index] = cards[index];
cout << endl << "First is now " << cards[index].strValue;
cout << endl << "Empty is now " << emptyDeck.cards[index].strValue;
}
}

// simple deck client

#include <iostream>
#include "deck.h"
#include <string>

using namespace std;
char pause;

int main()
{
int count = 0;

Deck firstDeck;
Deck emptyDeck(false);

cout << "Before function:" << endl;
cout << "First card = ";
firstDeck.NextCard(4);
cout << endl << "Empty card = ";
emptyDeck.NextCard(4);

cout << endl << endl << "During function:" << endl;
firstDeck.Shuffle(emptyDeck);

cout << endl << endl << "After function:" << endl;
cout << "First card = ";
firstDeck.NextCard(4);
cout << endl << "Empty card = ";
emptyDeck.NextCard(4);

cin >> pause;
return 0;
}


----------



## Arcadion (Sep 2, 2004)

Oh, that's an easy one. You're passing the deck to the function using call-by-value instead of call-by-reference. What it's doing is taking a _copy_ of the deck, and shuffling that, rather than modifying the original deck. My C++ is a little rusty, but I can give you a few hints, to start with you should use pointers and allocate space on the heap to store the decks, like this:

Try changing these lines:
Deck firstDeck;
Deck emptyDeck(false);

To this:
Deck *firstDeck = new Deck;
Deck *emptyDeck = new Deck(false);

Now you have to access the contents of your classes a little differently, you use -> instead of ., so you'd change these lines:

firstDeck.NextCard(4);
cout << endl << "Empty card = ";
emptyDeck.NextCard(4);

To this:
firstDeck->NextCard(4);
cout << endl << "Empty card = ";
emptyDeck->NextCard(4);

Let me know how you get on.


----------



## roryneil (Feb 21, 2005)

You went through that mess quite quickly. I had some more problems, but while I was writing this I figured them out! Now I think I can implement the Shuffle algorithm. I'll let you know when I do. Thank you so much.


----------

