I am trying to get the program to allow the user to input a number and then have the computer tell the user if the number is too small, too big, or equal to a randomly generated number. The prompt and input work, but it gets stuck after the first scanf.
I think it has to do with scanf and not the conditionals, because I added printf("Testing stopping point") and that doesn't get printed to the user's screen. What am I doing wrong?
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <stdlib.h>
int main()
{
//Generate a random number
int n = 1;
int count = 0;
int randomNumber;
srand(time(NULL));
for (int i = 1; i <= n; i++)
{
randomNumber = rand() % 101;
}
printf("Guess a number between 1 - 100: ");
int input;
scanf("%d\n",&input);
printf("Testing stopping point");
do
{
if (input > randomNumber)
{
count +=1;
printf("Too large!Try again: ");
getchar();
}else if (input < randomNumber)
{
count += 1;
printf("Too small!Try again: ");
getchar();
}
}while (input != randomNumber);
if(input == randomNumber)
{
count +=1;
printf("Correct!\n");
printf("You guessed %d times\n", count);
return 0;
}
}
goto
? You can accomplish this all within the do-while loop. – SuperStormerscanf
again so input always says the same. also the last if is redundant – TruVortex_07scanf
at the start of thedo { [...] } while ();
– TruVortex_07