samedi 31 janvier 2015

Random number generator in C not accepting input for play again?

it works until it asks the user to play again. It will prompt the user but automatically quit and go back to the command line. can somebody tell me what's going on? it doesn't give me any warnings and i can't think of a reason why, i've tried a few things. I'm new to C.



#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {

char goAgain='y';
int theNum=0;
int guess=0;
int max=0;
do{
do{
printf("Enter a number over 99: ");
scanf("%d", &max);
if(max <= 99) {
printf("Please enter a number over 99");
}
}while(max <= 99);
srand(time(NULL));
theNum = (rand() % max) + 1;
do{
printf("Please enter a guess:\n ");
scanf("%d", &guess);
if(guess > theNum) {
printf("Too high\n");
}
else if(guess < theNum) {
printf("Too high\n");
}
else {
printf("That's correct!\n");
}
}while(theNum != guess);

printf("Would you like to play again? (y/n): ");
scanf("%c", &goAgain);
}while(goAgain == 'y');
return(0);
}




Aucun commentaire:

Enregistrer un commentaire