Question: I'm trying to create a program that asks to type something and check if it is an integer. If it is an integer, then print "the integer is ...". Else, print "try again" and waits for another input. However, the program prints an infinite number of "try again" if you type in a character. Here's the source code:

```
#include <stdio.h>
#include <stdbool.h>

int main()
{
  int inp;
  bool t = 1;
  printf("type an integer\n");
  while (t) {
    if (scanf("%i", &inp) == 1) { 
      printf("The integer is %i", inp);
      t = 0;
    } else {
      printf("try again");
      scanf("%i", &inp);
    }
  }
}

```

Answer: 
When you entered a `char`, the variable `inp` in `scanf("%d", &inp)` would get `null`, since the input that doesn't match the format string. And the character you input would remain in the buffer, so that's the reason both your `scanf` would not stop.

A simplest way to fix this is modify your second `scanf("%i", &inp);` to `scanf("%c", &c);` (don't forget to declare a `char c` in your main function).
> 
> There is nothing in to break the while loop.
> 
> 
> 

consider getting rid of the boolean, and simply using a while (1) loop with a break. Also you should be using "%d" to indicate an integer in scanf/printf. And there is no need for the scanf call in the else, since your program would loop back and call scanf again anyway.

```
#include <stdio.h>
int main() {
  int inp = 0;
  printf("type an integer\n");
  while (1) {
    if (scanf("%d", &inp) == 1) { 
      printf("The integer is %d", inp);
      break;
    }
    else {
      printf("try again");
    }
  }
  return 0;
}

```

I hope this helped.