Magic Card Trick Program

Could anyone help me figure out why my program doesnt print correctly?

SAMPLE:

Column 0 Column 1 Column 2
=======================================================
9 of Clubs 7 of Clubs 8 of Hearts
6 of Spades Jack of Clubs King of Spades
Jack of Hearts 4 of Diamonds 8 of Diamonds
6 of Hearts Jack of Diamonds 8 of Spades
8 of Clubs King of Clubs 9 of Hearts
4 of Hearts Ace of Clubs Ace of Spades
10 of Diamonds 5 of Spades 3 of Diamonds


#include <iostream>
#include <iomanip>
using namespace std;

// Function prototypes
void BuildDeck( int deck[], const int size );
void PrintDeck( int deck[], const int size );
void PrintCard( int card );
void Deal( int deck[], int play[][3] );
void PickUp( int deck[], int play[][3], int column );
void SecretCard( int deck[] );

int main(void)
{

/* declare and initialize variables */
int column = 0, i = 0;
char ViewDeck, PlayAgain;
char name[20];

/* Declare a 52 element array of integers to be used as the deck of cards */
int deck[52] = {0};

/* Declare a 7 by 3 array to receive the cards dealt to play the trick */
int play[7][3] = {0};



/* Generate a random seed for the random number generator. */

srand(time(0));

/* Openning message. Ask the player for his/her name */
cout << "Hello, I am a really tricky computer program and " << endl
<< "I can even perform a card trick. Here's how." << endl
<< "To begin the card trick type in your name: ";
cin >> name;

/* Capitalize the first letter of the person's name. */

if(name[0] >='a' && name[15]<'z')
{
name[0]-=32;
}

cout << endl << "Thank you " << name << endl;

do
{
/* Build the deck */
BuildDeck(deck, 52);

/* Ask if the player wants to see the entire deck. If so, print it out. */
cout << "Ok " << name << ", first things first. Do you want to see what " << endl
<< "the deck of cards looks like (y/n)? ";
cin>> ViewDeck;

if(ViewDeck == 'y' || ViewDeck == 'Y')
{
PrintDeck(deck, 52);
}

if (ViewDeck == 'n' || ViewDeck == 'N')



cout << endl << name << " pick a card and remember it..." << endl;

/* Begin the card trick loop */
for(i = 0; i < 3; i++)
{
/* Begin the trick by calling the function to deal out the first 21 cards */


Deal(deck,play);

/* Include error checking for entering which column */
do
{
/* Ask the player to pick a card and identify the column where the card is */
cout << endl <<"Which column is your card in (0, 1, or 2)?: ";
cin >> column;
} while(column < 0 || column > 2);

/* Pick up the cards, by column, with the selected column second */


PickUp(deck,play,column);
}

/* Display the top ten cards, then reveal the secret card */


SecretCard(deck);

/* if the player wants to play again */
cout << name << ", would you like to play again (y/n)? ";
cin >> PlayAgain;
} while(PlayAgain == 'y');

/* Exiting message */
cout << endl << endl << "Thank you for playing the card trick!" << endl;
return 0;
}


void BuildDeck( int deck[], const int size)
{
int used[52] = {0};
int card = 0, i = 0;

/* Generate cards until the deck is full of integers */
while(i < size)
{
/* generate a random number between 0 and 51 */
card = rand()%52;

/* Check the used array at the position of the card.
If 0, add the card and set the used location to 1. If 1, generate another number */
if(used[card] == 0)
{
used[card] = 1;
deck[i]=card;
i++;


}
}
return;
}


void PrintCard( int card )
{
int rank = 0;
int suit = 0;

// Determine the rank of the card and print it out i.e. Queen

rank=card%13;

// Determine the suit of the card and print it out i.e. of Clubs

suit=card/13;

switch (rank)

{
case 0:
cout<<"King";
break;


case 1:
cout<<"Ace";
break;


case 8:
cout<<"Jack";
break;


case 9:
cout<<"Queen";
break;



default:

cout<< setw(5)<<rank;

}



switch (suit)

{
case 0:

cout<<" of Clubs";

break;

case 1:

cout<<" of Hearts";

break;



case 2:

cout<<" of Diamonds";

break;



case 3:
cout<<" of Spades";
break;
}

cout<<endl;
return;

}

void PrintDeck( int deck[], const int size )
{
int i=0;

/* Print out each card in the deck */
for (i = 0; i < 51; i++)
{
PrintCard(deck[i]);
cout << endl;
}}

void Deal( int deck[], int play[][3] )
{
int row = 0, col = 0, card = 0;



/* deal cards by passing addresses of cardvalues from
the deck array to the play array */
cout << endl;
cout << " Column 0 Column 1 Column 2";
cout << "======================================================="
<< endl;


for(row = 0; row < 7; row++)
{
for( col = 0; col < 3; col++)
{

cout.width(5);
PrintCard(deck[card]);
card++;}
cout << endl;



}

return;
}

void PickUp( int deck[], int play[][3], int column )
{
int card = 0, row = 0, first=0, last=0;

switch (column)
{

case 0:
first=2;
last=1;
break;


case 1:
first=2;
last=0;
break;


case 2:
first=0;
last=1;
break;
}



for (row=0; row<7;row++)

{
deck[card]= play [row][first];
card++;
}

for(row=0; row<7; row++)
{
deck[card]=play[row][column];
card++;
}

for (row=0; row<7;row++)
{
deck[card]= play [row][last];
card++;

}



return;
}

void SecretCard( int deck[] )
{
int card = 0;

cout << endl <<"Finding secret card...";

for(card = 0; card < 10; card++)
{
PrintCard(deck[card]);
cout << endl;
}

cout << endl <<"Your secret card is: ";
PrintCard(deck[card]);
cout << endl;
return;
}
Hi,
could you tell us what exactly is your problem (like what output you are expecting and what output you see)?
closed account (48T7M4Gy)
Well for a start you need code tags, proper indentation and remove all the blank lines so people can read it.

However, despite this dogs breakfast, it appears your output functionality simply needs to be (possibly) corrected for rows vs columns combined with returning to column zero in the printout when the inner loop completes.

Try just printing out a 2d array of numbers as a separate familiarity exercise.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{
  const int R = 5;
  const int C = 3;
  
  int a[R][C] = {0};
  
  for (int i = 0; i < R; i++)
  {
    for(int j = 0; j < C; j++)
        a[i][j] = i+j;
  }
    
    for (int i = 0; i < R; i++)
    {
        for(int j = 0; j < C; j++)
            std::cout << a[i][j];
            
            std::cout << '\n';
    }
}

012
123
234
345
456
 
Exit code: 0 (normal program termination)
Last edited on
Expected output:


Column 0 Column 1 Column 2
=======================================================
9 of Clubs 7 of Clubs 8 of Hearts
6 of Spades Jack of Clubs King of Spades
Jack of Hearts 4 of Diamonds 8 of Diamonds
6 of Hearts Jack of Diamonds 8 of Spades
8 of Clubs King of Clubs 9 of Hearts
4 of Hearts Ace of Clubs Ace of Spades
10 of Diamonds 5 of Spades 3 of Diamonds

Which column is your card in (0, 1, or 2)?: 1

Column 0 Column 1 Column 2
=======================================================
9 of Clubs 6 of Spades Jack of Hearts
6 of Hearts 8 of Clubs 4 of Hearts
10 of Diamonds 7 of Clubs Jack of Clubs
4 of Diamonds Jack of Diamonds King of Clubs
Ace of Clubs 5 of Spades 8 of Hearts
King of Spades 8 of Diamonds 8 of Spades
9 of Hearts Ace of Spades 3 of Diamonds

Which column is your card in (0, 1, or 2)?: 2

Column 0 Column 1 Column 2
=======================================================
6 of Spades 8 of Clubs 7 of Clubs
Jack of Diamonds 5 of Spades 8 of Diamonds
Ace of Spades Jack of Hearts 4 of Hearts
Jack of Clubs King of Clubs 8 of Hearts
8 of Spades 3 of Diamonds 9 of Clubs
6 of Hearts 10 of Diamonds 4 of Diamonds
Ace of Clubs King of Spades 9 of Hearts

Which column is your card in (0, 1, or 2)?: 0
Finding secret card...
8 of Clubs
5 of Spades
Jack of Hearts
King of Clubs
3 of Diamonds
10 of Diamonds
King of Spades
6 of Spades
Jack of Diamonds
Ace of Spades
Your secret card is: Jack of Clubs



My output:
10 of Diamonds

12 of Spades

King of Diamonds

3 of Hearts

Ace of Clubs

2 of Clubs

Jack of Spades

6 of Clubs

2 of Spades

Jack of Hearts

4 of Diamonds

3 of Diamonds

5 of Diamonds

5 of Hearts

2 of Diamonds

10 of Clubs

Jack of Diamonds

11 of Hearts

Jack of Clubs

5 of Spades

7 of Hearts

2 of Hearts

6 of Hearts

5 of Clubs

4 of Hearts

King of Spades

Queen of Hearts

7 of Spades

12 of Hearts

4 of Clubs

Ace of Spades

Ace of Hearts

11 of Clubs

7 of Clubs

Queen of Clubs

King of Hearts

3 of Clubs

12 of Clubs

6 of Diamonds

7 of Diamonds

Queen of Spades

3 of Spades

4 of Spades

6 of Spades

11 of Diamonds

Ace of Diamonds

Queen of Diamonds

10 of Spades

10 of Hearts

King of Clubs

11 of Spades


Column 0 Column 1 Column 2=======================================================
10 of Diamonds
12 of Spades
King of Diamonds


Which column is your card in (0, 1, or 2)?:
closed account (48T7M4Gy)
Well that sample is not what I commented on and quite frankly, if that's what you want I'd leave your program the way it is, because as I said earlier all you need to do is look carefully at how you can make a small change to print the data in columns instead of rows.
Topic archived. No new replies allowed.