My program is a guessing game where the user inputs numbers a maximum of three times to guess the correct number. I wanted to add a loop so that the program would repeat the game infinitely after the guesses from the previous game had been made, so I added the while loop where it says while (i >= 0). But every time I run the program, on any IDE, it refuses to repeat the program, and I'm not sure what I did wrong.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (void)
{
srand(time(0));
int i = 0;
while (i >= 0)
{
int x = (rand() % 50) + 1;
int guess;
int counter = 1;
printf("I am thinking of a number between 1 and 50. Can you guess what it is?\n");
printf("Please enter a guess: ");
scanf(" %d", &guess);
while (counter <= 3)
{
if (counter < 3 && guess !=x)
{printf("Sorry, it isn't %d\n", guess);
printf("Please enter a guess: ");
scanf(" %d", &guess);}
if (guess == x)
{printf("Correct! I was thinking of %d!\n", guess);}
if (counter == 3 && guess != x)
{printf("You lose! The number I was thinking of is %d\n", x);}
counter = counter + 1;
}
i++;
getchar();getchar();
return 0;
}
}
A return statement is the last statement in the outer while loop's compound statement. This likely would have been noticed if you were to have properly indented your source code.