Why is this program going into an infinite loop after the first scanf?

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;
        }
}


from Recent Questions - Stack Overflow https://ift.tt/3rvBUxQ
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)