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. */
/* 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;
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++)
{
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.
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
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.