Thread: Loops, looping more than they should.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115

    Loops, looping more than they should.

    I am working on this program to deal 13 random cards, and for some reason half the time one of my loops is looping more than they should... i cant work out why...

    its the do loop in there...

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        start:
        system("cls");
        
        srand ( (unsigned)time ( NULL ) );
        int var[200];
        for (int i = 0; i < 199; i++ )
        var[i] = ( "&#37;d ", rand() % 100 );
        
        
        string getSpade[13] = {"A", "2", "3", "4", "5" ,"6", "7", "8", "9", "10", "J", "Q", "K"};
        random_shuffle(getSpade, getSpade + 13);
        string getHeart[13] = {"A", "2", "3", "4", "5" ,"6", "7", "8", "9", "10", "J", "Q", "K"};
        random_shuffle(getHeart, getHeart + 13);
        string getDiamond[13] = {"A", "2", "3", "4", "5" ,"6", "7", "8", "9", "10", "J", "Q", "K"};
        random_shuffle(getDiamond, getDiamond + 13);
        string getClub[13] = {"A", "2", "3", "4", "5" ,"6", "7", "8", "9", "10", "J", "Q", "K"};
        random_shuffle(getClub, getClub + 13);
        
        
        int nextSpade = 0, nextHeart = 0, nextDiamond = 0, nextClub = 0;
        
        
        string pOneSpade[13], pOneHeart[13], pOneDiamond[13], pOneClub[13];
        
        int f=0,i=0;
        
        do
        {
            if ((var[f]%2 != 0) && (nextSpade <= 12))
            {
                pOneSpade[nextSpade] = getSpade[nextSpade];
                nextSpade++; i++;
            }
            f++;
            if ((var[f]%2 != 0) && (nextHeart <= 12))
            {
                 pOneHeart[nextHeart] = getHeart[nextHeart];
                 nextHeart++; i++;
            }
            f++;
            if ((var[f]%2 != 0) && (nextDiamond <= 12))
            {
                 pOneDiamond[nextDiamond] = getDiamond[nextDiamond];
                 nextDiamond++; i++;
            }
            f++;
            if ((var[f]%2 != 0) && (nextClub <= 12))
            {
                 pOneClub[nextClub] = getClub[nextClub];
                 nextClub++; i++;
            }
            f++;
        } while (i <= 12);
        
        // Print player one's cards.
        cout << "Player One \n"
             << "S: ";
        for (int i = 0; i <= nextSpade-1; i++)
            cout << pOneSpade[i] << " ";
        cout << endl;
        
        cout << "H: ";
        for (int i = 0; i <= nextHeart-1; i++)
            cout << pOneHeart[i] << " ";
        cout << endl;
        
        cout << "D: ";
        for (int i = 0; i <= nextDiamond-1; i++)
            cout << pOneDiamond[i] << " ";
        cout << endl;
        
        cout << "C: ";
        for (int i = 0; i <= nextClub-1; i++)
            cout << pOneClub[i] << " ";
        cout << endl;
        
        // Play again option.
        char ex;  
        cout << "Play again? (y or n)";
        te:
        cin >> ex;
        switch (ex)
        {
               case 'y': goto start; break;
               case 'n': system("cls"); break;
               default: goto te; break;
        }
            return EXIT_SUCCESS;
    }
    Last edited by Loic; 04-30-2007 at 06:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  2. strings and loops...
    By twomers in forum C Programming
    Replies: 5
    Last Post: 12-12-2005, 11:28 AM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM