0

Here is my code. Im wanting to verifiy that the users enters an integer and not a char or string. When I run this program while using a letter instead of a number, it puts me in a infinate loop asking me for a valid number but doesn't let me enter anything.

int num0;
int num1;

printf("Please enter your First number: \n");

    while (scanf("%i", &num0) != 1)    {
        printf("Error Please enter a valid Number: \n");
        scanf("%i", &num0);
    }

    printf("Please enter your Second number: \n");

    while (scanf("%i", &num1) != 1)  {
        printf("Error Please enter a valid Number: \n");
        scanf("%i", &num1);
    }
Ewan Cook
  • 1
  • 1
  • 1
    Indeed, the data that can't be scanned just sits there waiting for you to do something *else* with it. It isn't discarded. You'd find it easier to use `fgets` and `sscanf`, and then it *is* easy to just dump the faulty input. – Weather Vane Sep 28 '22 at 17:02
  • Aside: don't use `%i` for input unless you know that the user will want to enter octal of hexadecimal numbers. Normal users will be confused if their `011` is accepted as decimal `9` instead of `11`. Use `%d`. – Weather Vane Sep 28 '22 at 17:05

0 Answers0