I'm trying to program a simple game where the user has to guess a number between 1 and 1000. The game works for the most part, however it always displays the "You are correct! Play again? (y or n)" message twice. I don't understand why. Could someone here take a look at my code and point me in the right direction? I'd greatly appreciate it. The game is supposed to start over again, with a new random number if the user wins and presses 'y' - it's supposed to end if the user wins and presses 'n'.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int i = 1;
int ran = 0;
int input = 0;
char cont = 0;
srand( time( NULL ) );
ran = 1 + ( rand() % 999);
printf( "I have a number between 1 and 1000.\nCan you guess my number?\nPlease type your first guess.\n");
while( i != 0){
scanf( "%d", &input);
if( input != ran ){
if( input > ran)
printf("You're too high. Try again.\n");
if( input < ran)
printf("You're too low. Try again.\n");
}
if(input == ran){
printf("You're correct! Play again? (y or n)\n");
scanf("%c", &cont);
if( cont == 'y'){
cont = 0;
printf( "I have a number between 1 and 1000.\nCan you guess my number?\nPlease type your first guess.\n");
}
if( cont == 'n'){
printf("Ending game.\n");
i = 0;
}
}
}
return 0;
}