# Thread: Wanna play again?

1. ## Wanna play again?

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{

int guess;
int tries;
int number;

srand(time(0));
number=rand()%20;

printf("This is a guessing game! The computer will choose a number from 1-20, try to guess the number!\n");
printf("Enter your first guess!\n");
scanf("%d", &guess);
if (guess > 20)
printf("That guess is over 20!\n");

for (tries=1; guess != number;tries++)
{
if (guess > number)
printf("Wrong! %d is greater than the secret number!\n",guess);

if (guess < number)
printf("Wrong! %d is less than the secret number!\n",guess);

printf("Enter another guess!\n");
scanf("%d", &guess);

if (guess > 20)
printf("That guess is over 20!\n");

}

if(tries ==1)
printf("Amazing! You got it on your first guess!\n");

if(tries ==2)
printf("Excellent! It took you only 2 tries!\n");

if(tries ==3)
printf("Good job! It took you 3 tries\n!");

if(tries ==4)
printf("Pretty good! It took you 4 tries. Now try to beat this record!\n");

if (tries >=5)
printf("Not bad! It took 5 or more guesses!\n");

return 0;
}
I made this guessing game program. I want it to ask the user after he/she gets the correct answer if they want continue or play again... then the user will select an answer Y for yes, N for no, and the game will loop again. I know it's simple but I can't seem to get it!!! I've been trying for a while now! HELP!

2. Code:
user_wants_to_play = true;
while( user_wants_to_play )
{
PlayGame();
}

3. Code:
#include <stdio.h>
#include <ctype.h>

int main(void)
{
for ( ;; )
{
int c;
/* ... main body of program ... */
fputs("Continue? [y/n] ", stdout);
fflush(stdout);
c = tolower(getchar());
if ( c == 'n' )
{
break;
}
while ( c != '\n' && c != EOF )
{
c = getchar();
}
}
return 0;
}

4. Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{

int guess;
int tries;
int number;

srand(time(0));
number=rand()%20;

while (anwers != 'N');
{
printf("This is a guessing game! The computer will choose a number from 1-20, try to guess the number!\n");
printf("Enter your first guess!\n");
scanf("%d", &guess);
if (guess > 20)
printf("That guess is over 20!\n");

for (tries=1; guess != number;tries++)
{
if (guess > number)
printf("Wrong! %d is greater than the secret number!\n",guess);

if (guess < number)
printf("Wrong! %d is less than the secret number!\n",guess);

printf("Enter another guess!\n");
scanf("%d", &guess);

if (guess > 20)
printf("That guess is over 20!\n");

}

if(tries ==1)
printf("Amazing! You got it on your first guess!\n");

if(tries ==2)
printf("Excellent! It took you only 2 tries!\n");

if(tries ==3)
printf("Good job! It took you 3 tries\n!");

if(tries ==4)
printf("Pretty good! It took you 4 tries. Now try to beat this record!\n");

if (tries >=5)
printf("Not bad! It took 5 or more guesses!\n");

printf("Play Again!?);
}
return 0;
}

The bold areas are what I added. But it won't work!!! The damn thing loops on it's own after the correct answer!

5. Are you entering 'N' or 'n'? Your program as it is now will continue to loop until 'N' alone is entered. I suggest at least using toupper() in your code so that the user may enter 'n'.

6. That's because you're using scanf() right before getchar(). scanf() leaves a '\n' in the input buffer and that's what your call to getchar() is reading. The solution is in the FAQ (hint: you'll need to clear the input buffer.)

7. so I can't use scanf, then what am I suppose to use?

8. [QUOTE=98dodgeneondohc]
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{

int guess;
int tries;
int number;

srand(time(0));
number=rand()%20;

while (anwers != 'N');
{
printf("This is a guessing game! The computer will choose a number from 1-20, try to guess the number!\n");
printf("Enter your first guess!\n");
scanf("%d", &guess);
if (guess > 20)
printf("That guess is over 20!\n");

for (tries=1; guess != number;tries++)
{
if (guess > number)
printf("Wrong! %d is greater than the secret number!\n",guess);

if (guess < number)
printf("Wrong! %d is less than the secret number!\n",guess);

printf("Enter another guess!\n");
scanf("%d", &guess);

if (guess > 20)
printf("That guess is over 20!\n");

}

if(tries ==1)
printf("Amazing! You got it on your first guess!\n");

if(tries ==2)
printf("Excellent! It took you only 2 tries!\n");

if(tries ==3)
printf("Good job! It took you 3 tries\n!");

if(tries ==4)
printf("Pretty good! It took you 4 tries. Now try to beat this record!\n");

if (tries >=5)
printf("Not bad! It took 5 or more guesses!\n");

printf("Play Again!?);
}
return 0;
}
I just left that out for now. But the problem is, the program will not even stop and wait for an answer if they want to play again. It will just print out "Play Again!?" then it runs the game.

9. I don't think you read my post very thoroughly. getchar() is seeing the '\n' that scanf() is leaving on the input buffer. You'll have to get rid of the '\n' somehow before getting the user's answer.

10. Originally Posted by itsme86
I don't think you read my post very thoroughly. getchar() is seeing the '\n' that scanf() is leaving on the input buffer. You'll have to get rid of the '\n' somehow before getting the user's answer.

Sorry about that. I was very frustrated and tired yesterday. I'm actually teaching myself c programming. I don't quite undestand the scanf() function throughly. I just know how to use it, but don't know how it works. If scanf() is leaving behind the \n... how will I get rid of it? or is there another function I can use other than scanf() that will take the input?

11. you should change the if-conditional code to be switch code.

12. Something like:
Code:
while((answer = getchar()) != '\n')
;
Just add that code right before the answer = getchar(); line you already have. If you don't understand how that's achieving the goal then feel free to ask.

13. I'm not 100% sure on this. but wouldn't you also need the toupper() command to make sure that the input is made uppercase. I would give this a try at the end.
Code: