# Thread: Simple do { while () loop entire program

1. ## Simple do { while () loop entire program

Program code:

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

int main(void)
{
int chosen = 0; // the lucky number
int guess = 0;  // Stores a guess
int count = 3; // The meximum number of trie
int limit = 20; // Upper limit for pseudo-random values
char input;

do
{
srand(time(NULL)); // Use clock vlaue as starting seed
chosen = 1 + rand() % limit;     // Random int 1 to limit

printf("\nThis is a guessing game.");
printf("\nI have chosen a number between 1 and 20"
" which you must guess.\n");

for( ; count > 0 ; --count)
{
printf("\nYou have %d tr%s left.", count,count == 1 ? "y" : "ies");
printf("\nEnter a guess: ");    // Prompt for a guess
scanf("%d", &guess);    // Read in a guess

// Check for a correct guess
if(guess == chosen)
{
printf("\nCongratulations. You guessed it!\n");
return 0;        // End the program
}
else if(guess < 1 || guess > 20) // Check for an invalid guess
{
printf("I said the number is between 1 and 20.\n ");
}
else
{
printf("Sorry, %d is wrong. My number is %s than that.\n", guess, chosen > guess ? "greater" : "less");
}
}

printf("\nYou have had three tries and failed. The number was %ld\n", chosen);

printf("Do you want to play another game? (Press Y to replay)\n");
scanf(" %c", &input);

} while(toupper(input) == 'Y'); // if 'Y' is pressed loop program

return 0;
}```

Please, only the final two printf() statements (functions) are executing and scanf() following a failed 3 error response, am I missing a bracket somewhere which would make the do while loop loop the entire program successfully on a "y" or "Y" response for the scanf() function:

Code:
`scanf(" %c", &input);`

2. Perhaps you need to "reset" count?

Also this:
Code:
` printf("\nYou have had three tries and failed. The number was %ld\n", chosen);`
should be producing a warning. If your compiler is not warning you about an issue with this line you need to either increase your compiler warning levels or get a better compiler (or maybe both).

3. Originally Posted by jimblumberg
Perhaps you need to "reset" count?

Also this:
Code:
` printf("\nYou have had three tries and failed. The number was %ld\n", chosen);`
should be producing a warning. If your compiler is not warning you about an issue with this line you need to either increase your compiler warning levels or get a better compiler (or maybe both).
thanks Jim. Yes i forgot to run gcc with -Wall now I see "chosen" is an int not a %ld also count is counting down a max of 3 loops

4. also count is counting down a max of 3 loops
Yes, and as presently coded it will do this exactly once.

5. Originally Posted by jimblumberg
Yes, and as presently coded it will do this exactly once.

this fixed it
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

int main(void)
{
int chosen = 0; // the lucky number
int guess = 0;  // Stores a guess
int count = 3; // The meximum number of trie
int limit = 20; // Upper limit for pseudo-random values
char input;

do
{
srand(time(NULL)); // Use clock vlaue as starting seed
chosen = 1 + rand() % limit;     // Random int 1 to limit

printf("\nThis is a guessing game.");
printf("\nI have chosen a number between 1 and 20"
" which you must guess.\n");

for( ; count > 0 ; --count)
{
printf("\nYou have %d tr%s left.", count,count == 1 ? "y" : "ies");
printf("\nEnter a guess: ");    // Prompt for a guess
scanf("%d", &guess);    // Read in a guess

// Check for a correct guess
if(guess == chosen)
{
printf("\nCongratulations. You guessed it!\n");
return 0;        // End the program
}
else if(guess < 1 || guess > 20) // Check for an invalid guess
{
printf("I said the number is between 1 and 20.\n ");
}
else
{
printf("Sorry, %d is wrong. My number is %s than that.\n", guess, chosen > guess ? "greater" : "less");
}
}

printf("\nYou have had three tries and failed. The number was %d\n", chosen);

printf("Do you want to play another game? (Press Y to replay)\n");
scanf(" %c", &input);

count = 3;

} while(toupper(input) == 'Y'); // if 'Y' is pressed loop program

return 0;
}```
this line fixed it, after the scanf() function:

Code:
`count = 3;`
Thanks Jim...that was a quick & easy fix.

6. Actually this would probably be better:

Code:
```for(count = 3 ; count > 0 ; --count)
// Or even better, just define the variable in the loop it's self.
for(for int count = 3 ; count > 0 ; --count)```
In modern C you no longer need to create your variables at the beginning of some scope and many people prefer to create the variables closer to first use. Also you can now define and initialize the for loop variables within the loop construct.

Also that srand() call should happen before the loop, it really should only be called once, and never inside some loop.

7. Originally Posted by jimblumberg
Actually this would probably be better:

Code:
```for(count = 3 ; count > 0 ; --count)
// Or even better, just define the variable in the loop it's self.
for(for int count = 3 ; count > 0 ; --count)```
In modern C you no longer need to create your variables at the beginning of some scope and many people prefer to create the variables closer to first use. Also you can now define and initialize the for loop variables within the loop construct.

Also that srand() call should happen before the loop, it really should only be called once, and never inside some loop.

alright thanks, i worked through that problem, i'm working on a new problem tonight, will paste soon. Also, i missed your typo "for int count"