# Problem with while( ) loop

• 06-06-2011
007007
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;     }```
• 06-06-2011
ssharish2005
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
• 06-06-2011
007007
Quote:

Originally Posted by ssharish2005
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

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
• 06-06-2011
ssharish2005
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
• 06-06-2011
AndrewHunter
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)
• 06-06-2011
007007
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