Hello everyone, I'm working on a card trick program that essentially has the user pick a card and the program will figure out which card it is. However, When I get the the displayed cards, I get some results that have no value and some that are not alligned properly. I also am having an issue with getting a loop created in order to make the program loop itself until the user is done. Here is my code: `

        /* File: Cardtrick.cpp
        * Programmer: Dakotah Goetz
        * Date: 7-5-17
        * Course: COP 2931
        *
        * Purpose:
        * Write a program that performs a cardtrick. The program will create a
        * random deck of cards, deal them out, pick them up, and determine the
        * secret card.
        *
        */

        #include <iostream>
        #include <ctime>
        #include <string>
        #include <cmath>
        #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[]);
    string Cap_Name(string word);

    int main(void)
    {

        /* declare and initialize variables */
        int column = 0, i = 0;
        int  SeeTheDeck;
        int PlayAgain;
        string name;
        /* 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. */
        name = Cap_Name(name);

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

        do
        {
            /* Build the deck */
            BuildDeck(deck, 52);
            //while (PlayAgain == 'y');
            /* 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 (1 = y/0 = n)? ";
            cin >> SeeTheDeck;

            if (SeeTheDeck == '1')
            {
                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;
        system("pause");
    }

    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 */
        for (i = 0; i < 51; 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;

        /* 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 (row = 0; row < 7; row++)
        {
        for (int column = 0; column < 3; column++)

            {
                cout.width(0);
                play[row][column] = deck[card];
                card++;
                PrintCard(deck[card]);
                //PrintCard(play[row][col]);
            }
            cout << endl;
        }
        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:
            if (rank == 1)
                cout << "Ace";
            break;
        case 1:
            if (rank == 13)
                cout << "King";
            break;
        case 11:
            if (rank == 11)
                cout << "Jack";
            break;
        case 12:
            if (rank == 12)
                cout << "Queen";
            break;
        default:
            cout << setw(5) << rank;
        }

        switch (suit)
        {
        case 0:
            if (suit == 0)
                cout << " of Clubs\t";
            break;

        case 1:
            if (suit == 1)
                cout << " of Hearts\t";
            break;
        case 2:
            if (suit == 2)
                cout << " of Diamonds\t";
            break;

        case 3: 
            cout << " of Spades\t";
            break;

        }

        return;
    }

    void PickUp(int deck[], int play[][3], int column)
    {
        int card = 0, row = 0;
        int first = 0, last = 0;
        switch (column)
        {
        case 0:
            first = 1;
            last = 2;
            break;

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

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

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

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

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

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

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

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

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

        if (!word.empty())
        {
            word[0] = toupper(word[0]);
        }
        return word;

    }

**Here is a copy of the output I get: 
**
Hello, I am a really tricky computer program and
I can even perform a card trick.  Here's how.
To begin the card trick type in your name: dak

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

Dak, pick a card and remember it...

   Column 0           Column 1           Column 2
=======================================================
    8 of Diamonds           8 of Spades  of Diamonds
    6 of Spades     8 of Hearts Queen of Spades
Queen of Diamonds          10 of Diamonds           8 of Clubs
    6 of Diamonds           5 of Hearts     2 of Clubs
   10 of Hearts     9 of Diamonds           2 of Hearts
    6 of Clubs      4 of Spades    10 of Clubs
 of Hearts       of Diamonds        9 of Hearts

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

   Column 0           Column 1           Column 2
=======================================================
    6 of Spades Queen of Diamonds           6 of Diamonds
   10 of Hearts     6 of Clubs   of Hearts
    7 of Diamonds        of Diamonds    Queen of Spades
    8 of Clubs      2 of Clubs      2 of Hearts
   10 of Clubs      8 of Spades     8 of Hearts
   10 of Diamonds           5 of Hearts     9 of Diamonds
    4 of Spades  of Diamonds        9 of Hearts

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

   Column 0           Column 1           Column 2
=======================================================
   10 of Hearts     7 of Diamonds           8 of Clubs
   10 of Clubs     10 of Diamonds           4 of Spades
    6 of Spades    10 of Hearts     7 of Diamonds
    8 of Clubs     10 of Clubs     10 of Diamonds
    4 of Spades Queen of Diamonds           6 of Clubs
 of Diamonds        2 of Clubs      8 of Spades
    5 of Hearts  of Diamonds        9 of Hearts

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

Finding secret card...   
10 of Hearts
   10 of Clubs
    6 of Spades
    8 of Clubs
    4 of Spades
 of Diamonds
    5 of Hearts
   10 of Hearts
   10 of Clubs
    6 of Spades

Your secret card is:     8 of Clubs
Dak, would you like to play again (y/n)? y

Thank you for playing the card trick!
Press any key to continue . . .

I am not asking anyone to do the homework for me, but any pointers and help will be greatly appreciated!

I am unsure which lines are the cause of my problem. I think it has to do with the PrintCard function (Line 172), the Deal function (Line 146), as the output I posted seems to reference those functions more than the others.

I will look further into that. Would you be able to help me figure out why some numbers in my output do not show up? Below, there are two areas in my original post that point to what i mean - Both at the bottom of column 0.

    >  Column 0           Column 1           Column 2
    =======================================================
        8 of Diamonds           8 of Spades  of Diamonds
        6 of Spades     8 of Hearts Queen of Spades
    Queen of Diamonds          10 of Diamonds           8 of Clubs
        6 of Diamonds           5 of Hearts     2 of Clubs
       10 of Hearts     9 of Diamonds           2 of Hearts
        6 of Clubs      4 of Spades    10 of Clubs
   of Hearts of Diamonds   9 of Hearts
    Which column is your card in (0, 1, or 2)?: 0

As to the missing output. You should run this in say Visual C++ (there are free versions) and break on the line to examine the values and then you can work backwards to why the value was out of range or not working.

I notice line 183 where you switch (rank) and then on case 0 test if rank == 1, that won't work will it?

In short copy out lines 183 to 186 and stare at it till the lights go on.

Commenting out lines 183-186 results in around 30 errors from the code referencing those lines in other places. I've got my for loop taken care of now, so that is no longer a need. Once I can figure out the reason behind the numbers not appearing, I should be in good shape. I appreciate your time with this

I wanted you to understand your code.

Why would test rank to be 0 on line 185 then to be 1 on line 186? We are not using quantum computers yet.

PS. I wrote to COPY lines 183 to 186 and STARE at them till the lights came up. Once you see it, you will know what to do.

Again. COPY the lines to some text editor ALL on their own. Think about what you wrote.

I got it now. The numbers are appearing after i set the ace equal to 0 and the king equal to 1. My last issue is the indenting but that is a trivial matter. My loop is giving me trouble now and I cant seem to find the right place to insert my 'while {' operator. Here is my main code:

int main(void)
{

    /* declare and initialize variables */
    int column = 0, i = 0;
    int  SeeTheDeck = 0;
    int PlayAgain = 0;
    int KeepPlaying;
    string name;
    /* 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(NULL));

    /* 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. */
    name = Cap_Name(name);

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

    do
    {

        /* Build the deck */
        BuildDeck(deck, 52);
        //while (PlayAgain == 'y');
        /* 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 (1 = y/0 = n)? ";
        cin >> SeeTheDeck;

        while (SeeTheDeck != 0)
        {
            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 (1 = y/0 = n)? ";
            cin >> PlayAgain;
        } while (PlayAgain == 1);

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

The way this is outputting is that it is repeating at the line

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

and when I try to enter a line to accept a sentinel stop value, the whole program goes nuts. Here is an example of the output:

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

        Finding secret card...
                5 of Clubs
            6 of Diamonds
        King of Diamonds
        King of Hearts
            2 of Hearts
            8 of Spades
            4 of Hearts
            5 of Diamonds
            8 of Clubs
            8 of Hearts

        Your secret card is:     2 of Diamonds

        **HERE IS WHERE THE PROGRAM LOOPS TO**
        Dak, pick a card and remember it...

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

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

I'm unsure if you get it.

switch (rank)
            {
            case 0:
                if (rank == 1)

There's no reason for the if since the switch already tested the value of rank. Why if?

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.