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: 
OP's code fail to consume the offending non-numeric input. It remains in `stdin`, for the next input function. As it is unfortunately just another `scanf("%i", &inp)` which fails the same way - infinite loop.

After attempting to read an `int`, read the rest of the line.

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

int main() {
  int inp;
  int scan_count;
  printf("Type an integer\n");
  do {
    scan_count = scanf("%i", &inp); // 1, 0, or EOF

     // consume rest of line
    int ch;
    while ((ch == fgetchar()) != '\n' && ch != EOF) {
      ;
    }

  } while (scan_count == 0); 
  if (scan_count == 1) {  
    printf("The integer is %i\n", inp);
  } else {
    puts("End of file or error");
  }
}

```

An even better approach would read the line of user input with `fgets()`. [Example](https://stackoverflow.com/a/41146306/2410359)
check here `while(t)` its in an infinite loop because you have to set a condition for `t` something like while(t==1) or while(t>1) or (t<1) something like that. saying while(t) means that t can be anything and it will continue to run.