Hello everyone I have pretty lengthy code here, i think I may need to place all the code, but all I need is one one thing, it's pretty much done:

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

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

int main(void)
{

    /* declare and initialize variables */
    int column = 0, i = 0;
    string name;
    char playAgain;
    char seeDeck;

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

    /* Opening message.  Ask the player for his/her name */
    cout << "Hello, I am a computer program that is so smart" << 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. */

    name = Cap_Name(name);

    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 >> seeDeck;

        if (seeDeck=='y')
        {
            cout << endl;
            PrintDeck(deck, 52);
        }





        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;

    for(i = 0; i < 52; i++)
    {
        do
        {
            /* 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 */
        } while(used[card] == 1);

        used[card]++;
        deck[i] = card;

    }
    return;
}

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

    /* 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;



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

        play[row][col] = deck[card];

        for (col = 0; col < 3; col++)
        {
            play[row][col] = deck[card];
            card++;

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


    for(int row = 0; row < 7; row++)
    {
        for(int col = 0; col < 3; col++)
        {
            cout.width(5);
            PrintCard(play[row][col]);
            cout << "        ";
        }
        cout << endl;
    }

    return;


}


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

    rank = card % 13, suit = (card - rank) / 13;

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

    if (rank == 0)
        cout << "   Ace ";

    else if (rank == 9)
        cout << "   10 ";

    else if (rank == 10)
        cout << "   Jack ";

    else if (rank == 11)
        cout << "   Queen ";

    else if (rank == 12)
        cout << "   King ";

    else
        cout << "    " << rank;

        cout << " of ";

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

    if (suit == 0)
        cout << " Clubs ";
    else if (suit == 1)
        cout << " Diamonds ";
    else if (suit == 2)
        cout << " Hearts ";
    else
        cout << " Spades ";
    return;

}

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

    for (row =0 ; row < 3; row++)
        {
            int pickupColumn = (column + row + 2) % 3;

            for(int cardInColumn = 0; cardInColumn < 7; cardInColumn++)
                {
                    deck[card++] = play[cardInColumn][pickupColumn];
                }

            }

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

    cout << endl <<"Finding secret card..." <<endl;
    for(card = 0; card < 10; card++)
    {
        cout.width(5);
        PrintCard(deck[card]);
        cout << endl;
    }

    cout << endl <<"Your secret card is: ";
    PrintCard(deck[card]);
    cout << endl;
    return;
}

// Capitalize the first letter in a string
string Cap_Name(string word)
{
    if (!word.empty())
    {
        word[0] = toupper(word[0]);
    }
    return word;
}

When the program deals the cards it prints like one long list:

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
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
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

When it's supposed to print like this:

Ace of Hearts Jack of Spades 7 of Clubs
2 of Spades 2 of Spades 3 of Spades
3 of Diamonds 4 of Diamonds 10 of Hearts
10 of Clubs 8 of Hearts 9 of Diamonds
Jack of Hearts 9 of Clubs Jack of Clubs
Queen of Clubs Queen of Hearts King of Diamonds
7 of Diamonds King of Spades 6 of clubs

I'm horrible at formatting, so if I can get a little help with it, thanks in advance.

Recommended Answers

All 5 Replies

Line 178 should only trigger one time in three.

Each time make it increment a counter. When the counter reaches 3, reset it to zero and only then output the endl.

Are you sure, When the loops print the portion with the columns, it prints correctly, but before that it prints the entire deck out, I think it has to do with line 147 right?

Hang on... I didn't give it a really good read. I scanned through looking for an endl after it prints out the card's suit...

Lots of stuff comes out. It looks like this; what in this do you not like? If it's the big list after "Do you want to see what the deck of cards looks like (y/n)?" that's a big list because at that point you call the function printDeck which just churns out all the cards with an endl after each card, on line 138. It doesn't happen anywhere else because nowhere else do you call printDeck.

Hello, I am a computer program that is so smart
I can even perform a card trick.  Here's how.
To begin the card trick type in your name: C

Thank you C
Ok C, first things first.  Do you want to see what 
the deck of cards looks like (y/n)? y

    6 of  Diamonds 
   10  of  Hearts 
    5 of  Clubs 
    5 of  Diamonds 
   10  of  Clubs 
    3 of  Clubs 
    5 of  Hearts 
   10  of  Diamonds 
   Queen  of  Clubs 
    7 of  Hearts 
    6 of  Hearts 
    6 of  Clubs 
   Queen  of  Diamonds 
   King  of  Diamonds 
   King  of  Spades 
   Ace  of  Diamonds 
   Jack  of  Hearts 
    1 of  Hearts 
    7 of  Diamonds 
    4 of  Diamonds 
    1 of  Diamonds 
    8 of  Diamonds 
   Ace  of  Spades 
    1 of  Spades 
   Ace  of  Hearts 
    4 of  Clubs 
   King  of  Clubs 
    7 of  Spades 
    2 of  Hearts 
    6 of  Spades 
    8 of  Spades 
    8 of  Hearts 
    2 of  Clubs 
    2 of  Spades 
   Jack  of  Clubs 
    7 of  Clubs 
   Jack  of  Diamonds 
   10  of  Spades 
    3 of  Hearts 
    8 of  Clubs 
    4 of  Hearts 
    3 of  Spades 
   Queen  of  Spades 
    4 of  Spades 
    1 of  Clubs 
    2 of  Diamonds 
   Jack  of  Spades 
   Queen  of  Hearts 
    5 of  Spades 
   King  of  Hearts 
    3 of  Diamonds 

C pick a card and remember it...

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

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

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

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

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

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

Finding secret card...
     5 of  Diamonds 
     6 of  Hearts 
     6 of  Clubs 
   Ace  of  Diamonds 
     3 of  Clubs 
     7 of  Hearts 
   Jack  of  Hearts 
   Queen  of  Clubs 
   Queen  of  Diamonds 
     4 of  Diamonds 

Your secret card is:     1 of  Diamonds 
C, would you like to play again (y/n)? n


Thank you for playing the card trick!

Yes exactly, i need to make print deck print in 3 columns just like the game is played

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.