Thread: What's wrong with this program?

  1. #1
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68

    What's wrong with this program?

    Hi all,

    Try to compile this program and see what happens.
    I use Visual C++ 6.

    The problem is that at the very end, where is prints out 13 different cards for each player, it arrives to the point where it says "Player 4:" and seems to ignore the loop that displays the cards.

    _____________________

    #include <iostream.h>
    #include <stdlib.h> // for randomize(), rand
    #include <time.h> // for randomize()
    #include <conio.h> // for getche()

    enum Suit { clubs, diamonds, hearts, spades };

    const int jack = 11; // from 2 to 10 are
    const int queen = 12; // integers without names
    const int king = 13;
    const int ace = 14;

    class card
    {
    private:
    int number; // 2 to 10, jack, queen, king, ace
    Suit suit; // clubs, diamonds, hearts, spades
    public:
    void init(int n, Suit s) // initialize card
    { suit = s; number = n; }
    void display() // display the card
    {
    if( number >= 2 && number <= 10 )
    cout << number;
    else
    switch(number)
    {
    case jack: cout << "J"; break;
    case queen: cout << "Q"; break;
    case king: cout << "K"; break;
    case ace: cout << "A"; break;
    }
    switch(suit)
    {
    case clubs: cout << 'c'; break;
    case diamonds: cout << 'd'; break;
    case hearts: cout << 'h'; break;
    case spades: cout << 's'; break;
    }
    } // end display()
    }; // end class card

    void main()
    {
    card deck[52]; // deck of cards
    int j = 0; // counts thru deck
    int num; // card number

    cout << endl;
    for(num=2; num<=14; num++) // for each number
    {
    deck[j].init(num, clubs); // set club
    deck[j+13].init(num, diamonds); // set diamond
    deck[j+26].init(num, hearts); // set heart
    deck[j++ +39].init(num, spades); // set spade
    }

    cout << "\nOrdered deck:\n";
    for(j=0; j<52; j++) // display ordered deck
    {
    deck[j].display();
    cout << " ";
    if( !( (j+1) % 13) ) // newline every 13 cards
    cout << endl;
    }

    srand(time(0)); // seed random number generator
    for(j=0; j<52; j++) // for each card in the deck,
    {
    int k = 1 + rand() % 52; // pick another card at random
    card temp = deck[j]; // and swap them
    deck[j] = deck[k];
    deck[k] = temp;
    }

    cout << "\nShuffled deck:\n";
    for(j=0; j<52; j++) // display shuffled deck
    {
    deck[j].display();
    cout << " ";
    if( !( (j+1) % 13) ) // newline every 13 cards
    cout << endl;
    }
    card player1[13];
    card player2[13];
    card player3[13];
    card player4[13];
    for (j=0; j<13; j++)
    {
    player1[j] = deck[j];
    player2[j] = deck[j+13];
    player3[j] = deck[j+26];
    player4[j] = deck[j++ +39];
    }
    cout << "\n\nPlayer 1:"<<endl;
    for (j=0; j<13; j++)
    {
    player1[j].display();
    cout << " ";
    }
    cout << "\n\nPlayer 2:"<<endl;
    for (j=0; j<13; j++)
    {
    player2[j].display();
    cout << " ";
    }
    cout << "\n\nPlayer 2:"<<endl;
    for (j=0; j<13; j++)
    {
    player3[j].display();
    cout << " ";
    }
    cout << "\n\nPlayer 4:"<<endl;
    for (j=0; j<13; j++)
    {
    player4[j].display();
    cout << " ";
    }
    getch(); // wait for keypress
    } // end main

    Also, in the following code:

    deck[j].init(num, clubs); // set club
    deck[j+13].init(num, diamonds); // set diamond
    deck[j+26].init(num, hearts); // set heart
    deck[j++ +39].init(num, spades); // set spade

    does anybody know why there is need to do j++ +39 and not just +39?


    What may I do to solve the problem?
    Thanks,
    Marc
    No matter how much you know, you NEVER know enough.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I'm not sure that using variable++ + other_number works. Try just expanding this to two lines. I can't think of a reason for thisnot to work, after all, it is just a regular operator, but it just looks really different. Also, if you're using classes, if an bject has an instance/reference to an object with a reference to the class that referenced it (confusing, I know, but try to bear with me), then that will last for a a little while until it runs out of memory from the stack. Try dynamic allocation and checking that little incrementation operator (the line that says player4[j] = deck[j++ +39];

    Hope this helps.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    for(num=2; num<=14; num++) // for each number
    {
    deck[j].init(num, clubs); // set club
    deck[j+13].init(num, diamonds); // set diamond
    deck[j+26].init(num, hearts); // set heart
    deck[j++ +39].init(num, spades); // set spade
    }

    //Since j is not incremented in the top part of the for-loop, j must be incremented once each time through the loop. It's the same as:
    for(num=2; num<=14; num++) // for each number
    {
    deck[j].init(num, clubs); // set club
    deck[j+13].init(num, diamonds); // set diamond
    deck[j+26].init(num, hearts); // set heart
    deck[j+39].init(num, spades); // set spade
    j++;
    }

    //However, in this loop:
    for (j=0; j<13; j++)
    {
    player1[j] = deck[j];
    player2[j] = deck[j+13];
    player3[j] = deck[j+26];
    player4[j] = deck[j++ +39];
    }

    //Variable j is incremented at the top of the for-loop, so it doesn't need to be incremented at the bottom. So:
    for (j=0; j<13; j++)
    {
    player1[j] = deck[j];
    player2[j] = deck[j+13];
    player3[j] = deck[j+26];
    player4[j] = deck[j+39];
    }

    //Now normally you don't deal the same player 13 cards in a row before dealing to the next player, so:
    int k = 0;
    for (j=0; j<13; j++)
    {
    player1[j] = deck[k++];
    player2[j] = deck[k++];
    player3[j] = deck[k++];
    player4[j] = deck[k++];
    }

  4. #4
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68

    Thanks

    I finished what you told me to do

    ____________________

    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    #include <conio.h>

    enum Suit { clubs, diamonds, hearts, spades };

    const int jack = 11; // from 2 to 10 are
    const int queen = 12; // integers without names
    const int king = 13;
    const int ace = 14;

    class card
    {
    private:
    int number; // 2 to 10, jack, queen, king, ace
    Suit suit; // clubs, diamonds, hearts, spades
    public:
    void init(int n, Suit s) // initialize card
    { suit = s; number = n; }
    void display() // display the card
    {
    if( number >= 2 && number <= 10 )
    cout << number;
    else
    switch(number)
    {
    case jack: cout << "J"; break;
    case queen: cout << "Q"; break;
    case king: cout << "K"; break;
    case ace: cout << "A"; break;
    }
    switch(suit)
    {
    case clubs: cout << 'c'; break;
    case diamonds: cout << 'd'; break;
    case hearts: cout << 'h'; break;
    case spades: cout << 's'; break;
    }
    } // end display()
    }; // end class card

    void main()
    {
    card deck[52]; // deck of cards
    int j = 0; // counts thru deck
    int num; // card number

    cout << endl;
    for(num=2; num<=14; num++) // for each number
    {
    deck[j].init(num, clubs); // set club
    deck[j+13].init(num, diamonds); // set diamond
    deck[j+26].init(num, hearts); // set heart
    deck[j++ +39].init(num, spades); // set spade
    }

    cout << "\nOrdered deck:\n";
    for(j=0; j<52; j++) // display ordered deck
    {
    deck[j].display();
    cout << " ";
    if( !( (j+1) % 13) ) // newline every 13 cards
    cout << endl;
    }

    srand(time(0)); // seed random number generator
    for(j=0; j<52; j++) // for each card in the deck,
    {
    int k = 1 + rand() % 52; // pick another card at random
    card temp = deck[j]; // and swap them
    deck[j] = deck[k];
    deck[k] = temp;
    }

    cout << "\nShuffled deck:\n";
    for(j=0; j<52; j++) // display shuffled deck
    {
    deck[j].display();
    cout << " ";
    if( !( (j+1) % 13) ) // newline every 13 cards
    cout << endl;
    }
    card player1[13];
    card player2[13];
    card player3[13];
    card player4[13];
    int k = 0;
    for (j=0; j<13; j++)
    {
    player1[j] = deck[k++];
    player2[j] = deck[k++];
    player3[j] = deck[k++];
    player4[j] = deck[k++];
    }
    cout << "\n\nPlayer 1:"<<endl;
    for (j=0; j<13; j++)
    {
    player1[j].display();
    cout << " ";
    }
    cout << "\n\nPlayer 2:"<<endl;
    for (j=0; j<13; j++)
    {
    player2[j].display();
    cout << " ";
    }
    cout << "\n\nPlayer 3:"<<endl;
    for (j=0; j<13; j++)
    {
    player3[j].display();
    cout << " ";
    }
    cout << "\n\nPlayer 4:"<<endl;
    for (j=0; j<13; j++)
    {
    player4[j].display();
    cout << " ";
    }
    getch();
    } // end main

    _________________________

    However, when I run in it, it still does not complete the final loop!

    What may I do?

    Thanks!
    Marc
    No matter how much you know, you NEVER know enough.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I think you just need to add a cout with a newline at the end of the player4 display. It probably buffers the output until a newline character is output. So:

    for (j=0; j<13; j++)
    {
    player4[j].display();
    cout << " ";
    }
    cout << endl; //Here
    getch();
    } // end main
    Last edited by swoopy; 12-19-2001 at 02:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM