2

I'm just a beginner and am trying to make a a program that asks for a number and if a letter is input, it says "that's not a number" and asks for a number again, until a number is input.

However, my program keeps going into an infinite loop with the current code. Any help would be appreciated to fix this. Also, I would also like the program to say "please input something" if nothing is input, but don't know how to do this. Thanks.

#include <stdio.h>

int main()
{
    float i;
    printf("enter a number");
    while(scanf("%f", &i) != 1)
    {
        puts("That is not a number.");
        scanf("%f", &i);
    }
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
grammer
  • 245
  • 3
  • 14

4 Answers4

1

You need to clear the bad input from stdin after your scanf fails:

#include <stdio.h>

int main()
{
    float i;
    char trash[1024];

    while (1) 
    {
        printf("Please enter a number: ");
        fflush(stdout);

        if (1 == scanf("%f", &i))
          break;

        /* scanf failed: clear the bad input from stdin */

        if (NULL == fgets(trash, sizeof(trash), stdin))  /* NOTE: assumes 1 entry per line and no line longer than 1023 characters */
          exit((fprintf(stderr, "Unexpected EOF or error!\n"), 1));

        puts("That is not a number.");
    }

    printf("You entered: %f!\n", i);

    return 0;
}

As an alternative to the fgets() to clear the line, you could call scanf("%1023s", trash), which would only suck in the next whitespace delimited series of characters. This would allow you to handle multiple entries on a single line with mistakes intermixed, for example.

jschultz410
  • 2,849
  • 14
  • 22
0

Your program goes into infinite loop because after the invalid input, (scanf("%d", &i) != 1)condition being TRUE, the invalid input which is left in the input buffer is not consumed, it's still in the buffer. So the same invalid input is read over and over again.

To avoid, once scanf() fails, you need to flush out all the input buffer contains before calling next scanf().

Maybe inside the while loop, calling getchar() until a newline or EOF will help. Also, the second scanf() can be removed, IMHO.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

After your non numeric[More precisely input which doesn't match the formating of scanf()] input you need to clear the stdin. If not the same input will be read till stdin get cleaned or you terminate the program. A reference answer can be found on this question

Quoted

On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

Reason for infinite loop : Since you don't clear stdin, same values will be read by scanf() and always full fill while condition resulting in a infinite loop.

Use following edited code :

#include <stdio.h>

int main()
{
    float i;
    char c;

    printf("enter a number");
    while(scanf("%f", &i) != 1)
    {
        puts("That is not a number.");
        scanf("%f", &i);
        while ((c = getchar()) != '\n' && c != EOF); // Flush stdin
    }
}
Community
  • 1
  • 1
Kavindu Dodanduwa
  • 12,193
  • 3
  • 33
  • 46
0

Your code is taking input of a number. Thats why if you even give input a letter, it will take the ASCII value of the letter, which is a valid number. And Further more please take a character as an input. Here I have modified your code which should work

 #include <stdio.h>

int main()
{
char i;
printf("enter a number");
while(1)
{
    scanf("%c",&i);
    if (c >=48 && c <= 57) // here ascii value of numbers between 0-9 is 48-57 respectively
   { 
puts("That is a number.");
break;
}
else
{
puts("That is not a number.");
scanf("%c",&i);
}

}
}
Naseef Chowdhury
  • 2,357
  • 3
  • 28
  • 52