basically the program is just a guessing game. you guess a number between 1 and 1000. my friends run it and it works, so i think its a linux problem. when the program asks if i want to play again, it just kills the program unless i scan in two letters, when all im looking for is a 'y' or an 'n'. does anybody know why this happens? i run ubuntu
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
int readGuess()
{
int answer;
scanf( "%d", &answer );
return answer;
}
/*returns initial guess*/
int initGame(int *random)
{
*random = (rand()%1000) + 1;
printf("I have a number between 1 and 1000\n");
printf("Can you guess my number\n");
printf("Please type your first guess. ");
return readGuess();
}
int guessAgain()
{
return readGuess();
}
int main()
{
/*randomize number to guess.*/
srand((unsigned) time (NULL));
char cont = 'y';
int random;
int answer = initGame(&random);
printf("%d",random);
while (cont == 'y')
{
if (answer == random)
{
printf("Excellent! You guessed the number!\n");
printf(" Would you like to play again (y or n)? ");
fflush (stdin);
scanf( "%c", &cont );
printf( "DD%cDD", cont );
printf("\n\n");
if (cont == 'y')
answer = initGame(&random);
continue;
}
else if (answer <= random)
printf("Too low. Try again.\n");
else if (answer >= random)
printf("Too high. Try again.\n");
answer = guessAgain();
}
}