Thread: Problem with while( ) loop

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    6

    Problem with while( ) loop

    Hi,
    I'm trying to make this program, where you enter 2-4 players and deal them number of cards by random generator, and print the final output out for each player. In the loop I have to use the bool function to check if the card has been dealt or not. My problem is I have a never ending loop, and yes conditions are there and it should quit, but somehow it doesn't. Any help would great. Thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <stdbool.h>
    
    int main (void)
    {
        int playersNumb, cardsNumb;
        int i, j, n, p, temp;
        int card [52];
        bool cardDealt = false;
        
        
        printf("Please Enter Number of players: ");
        scanf("%d",&playersNumb);
        
        printf("Please Enter Number of cards being deal: ");
        scanf("%d",&cardsNumb);
        
        n=0;
        j=0;
        
        
        while (j < (playersNumb*cardsNumb)) //checking total numbers of cards dealt
        {
              for (i=0;i<playersNumb;i++)
              {
                  while (n < cardsNumb)  //checking cards dealt for each person
                  {
                        temp = rand();
                        
                        for (p=0;p<n;p++)
                        {
                            if (card[p] == temp) 
                            {
                                        cardDealt = true; //checks if the card already exist
                            }
                        }
                        
                        if (temp < 53 && cardDealt == false) //if random number less than 53 and card is still in the deck, stores it
                        {
                                 card[j] = temp;
                                 n++;
                                 j++;
                        }
                        
                        cardDealt = false;
                  }
              }
        }
        
        i=1;
        printf("Player %d - ",i);
        
        for (j=1;j<=(playersNumb*cardsNumb);j++)
        {
            if (j%cardsNumb == 0) {             //checks the modulus if 0 then breaks the line for next player
               printf("\n");
               printf("Player %d - ",i);
               } else {
                      printf("%d ",card[j]);
               }
                
            i++;
        }
        
        system ("pause");
        return 0;
        
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Did you meant this to be an if statment. I honestly dont have a clue how the card game works. But from the code, it seems the control donst even reach until 'j++' sometime.

    Code:
    while (j < (playersNumb*cardsNumb)) //checking total numbers of cards dealt
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    Quote Originally Posted by ssharish2005 View Post
    Did you meant this to be an if statment. I honestly dont have a clue how the card game works. But from the code, it seems the control donst even reach until 'j++' sometime.

    Code:
    while (j < (playersNumb*cardsNumb)) //checking total numbers of cards dealt
    ssharish
    Thank you for your reply
    It should be a while loop, which keeps running until it reaches number of cards we want in total. e.g. we start with 2 players and each being dealt 4 cards. In total we need 8 cards now. Reason it doesn't reach sometimes is because if the random number being generated more than 52 (which is outside of the deck) then we disregard that value and run it again.
    Again Thanks alot for your help Harish

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
                        if (temp < 53 && cardDealt == false) //if random number less than 53 and card is still in the deck, stores it
                        {
                                 card[j] = temp;
                                 n++;
                                 j++;
                        }
                        
                        cardDealt = false;
                  }
              }
              n = 0;      
    }
    Re-initialise 'n' back to 0 after dealt with each player

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    I would take a look at your implementation of the rand() function. First of all the lack of seeding the random generator will eventually cause problems; without seeding the generator will produce the same series of random numbers. In order to seed the generator you need to include the time.h header file and insert the following code before your outer loop
    Code:
    srand(time(NULL));
    Second it is possible to scale the return value of the rand function so you always get a value between 1 and 52 for your cards. I would suggest replacing your line
    Code:
    temp = rand();
    with something along these lines:
    Code:
    temp = 1 + rand() % 52; //produces a number between 1 and 52
    In general to produce a random number between a and b the syntax is: a + rand() % (b + 1 - a)

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    Thanks Harish, that was the problem.

    Thanks Andrew (assuming that's what your name is). With your tip my final code is much smaller and working perfectly.

    Thanks again both of you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while loop problem
    By slava in forum C Programming
    Replies: 5
    Last Post: 10-31-2008, 11:17 AM
  2. For Loop problem
    By lawzbhoy in forum C++ Programming
    Replies: 4
    Last Post: 05-14-2008, 11:39 AM
  3. Plz help with this loop problem,Thanks!
    By blue_blue in forum C Programming
    Replies: 4
    Last Post: 04-28-2008, 11:34 PM
  4. Loop Problem
    By shawry in forum C Programming
    Replies: 5
    Last Post: 04-01-2006, 09:55 AM
  5. problem with eof loop
    By Invincible in forum C++ Programming
    Replies: 2
    Last Post: 05-19-2002, 07:27 PM