54

I've a small C-program which just reads numbers from stdin, one at each loop cycle. If the user inputs some NaN, an error should be printed to the console and the input prompt should return again. On input of "0", the loop should end and the number of given positive/negative values should be printed to the console. Here's the program:

#include <stdio.h>

int main()
{
    int number, p = 0, n = 0;

    while (1) {
        printf("-> ");
        if (scanf("%d", &number) == 0) {
            printf("Err...\n");
            continue;
        }
        
        if (number > 0) p++;
        else if (number < 0) n++;
        else break; /* 0 given */
    }

    printf("Read %d positive and %d negative numbers\n", p, n);
    return 0;
}

My problem is, that on entering some non-number (like "a"), this results in an infinite loop writing "-> Err..." over and over. I guess it's a scanf() issue and I know this function could be replace by a safer one, but this example is for beginners, knowing just about printf/scanf, if-else and loops.

I've already read the answers to the questionscanf() skips every other while loop in C and skimmed through other questions, but nothing really answer this specific problem.

4
  • 1
    Many closely related SO questions, including: stackoverflow.com/questions/1669821 Commented Nov 11, 2009 at 22:34
  • 1
    In response to all the answers and hints: Adding while (getchar() != '\n'); before "continue" inside the if-statement works really fine for me and (hopefully) solves all/most of the problems. Further, it's reasonable explainable to beginners :). Commented Nov 13, 2009 at 20:51
  • See also Using fflush(stdin). Commented Sep 15, 2016 at 5:32
  • Note that the loop mentioned by user208785 in the comment above should be int c; while ((c = getchar()) != EOF && c != '\n') ; — The type of c should be int and the code needs to test both EOF and newline, though the code will normally get a newline before EOF. Commented Sep 5, 2022 at 19:31

16 Answers 16

41

scanf consumes only the input that matches the format string, returning the number of characters consumed. Any character that doesn't match the format string causes it to stop scanning and leaves the invalid character still in the buffer. As others said, you still need to flush the invalid character out of the buffer before you proceed. This is a pretty dirty fix, but it will remove the offending characters from the output.

char c = '0';
if (scanf("%d", &number) == 0) {
  printf("Err. . .\n");
  do {
    c = getchar();
  }
  while (!isdigit(c));
  ungetc(c, stdin);
  //consume non-numeric chars from buffer
}

edit: fixed the code to remove all non-numeric chars in one go. Won't print out multiple "Errs" for each non-numeric char anymore.

Here is a pretty good overview of scanf.

Sign up to request clarification or add additional context in comments.

9 Comments

If the input is "abc", that code will print "Err. . ." three times.
Yeah, it is pretty ghetto. I will tweak it a bit.
Now if the input is "ab-10", it will incorrectly remove the minus sign from the input and read "10" as the next number.
I know it's old, but just change it to while (!isdigit(c) && c != '-');, that should also help with minus signs.
returning the number of characters consumed -- Actually, scanf returns the number of fields successfully read. So scanf("%d") might return 1, 0, or EOF.
|
10

scanf() leaves the "a" still in the input buffer for next time. You should probably use getline() to read a line no matter what and then parse it with strtol() or similar instead.

(Yes, getline() is GNU-specific, not POSIX. So what? The question is tagged "gcc" and "linux". getline() is also the only sensible option to read a line of text unless you want to do it all by hand.)

7 Comments

You can not rely on non standard extensions for something as crucial as user input without providing them in your own tree in case they do not exist. If you edit your answer to reflect this, I will withdraw my down vote.
@tinkertim: the question specifies gcc on Linux, guaranteeing that strtol is available
Also, at least hinting how to turn such extension ON may help :)
@Andomar: It was getline() that I was taking issue with ;)
@TimPost Both getline() and getdelim() were originally GNU extensions. They were standardized in POSIX.1-2008.
|
9

I think you just have to flush the buffer before you continue with the loop. Something like that would probably do the job, although I can't test what I am writing from here:

int c;
while((c = getchar()) != '\n' && c != EOF);

4 Comments

"premature optimization is the root of all evil" ... but switch the constants: '\n' is much more likely to show up than EOF :)
You'd hope EOF is 100% guaranteed to show up; otherwise, you either have a really fast keyboard or a really slow CPU
The above complication in the while statement's condition is unnecessary.
@ilgaar What do you mean? It looks fine to me.
5

Rather than using scanf() and have to deal with the buffer having invalid character, use fgets() and sscanf().

/* ... */
    printf("0 to quit -> ");
    fflush(stdout);
    while (fgets(buf, sizeof buf, stdin)) {
      if (sscanf(buf, "%d", &number) != 1) {
        fprintf(stderr, "Err...\n");
      } else {
        work(number);
      }
      printf("0 to quit -> ");
      fflush(stdout);
    }
/* ... */

1 Comment

fgets() read some buffer and if it doesn't contain format right from the beginning the whole line is thrown away. This could be not acceptable (but could be desired, it depends on requirements).
4

Due to the problems with scanf pointed out by the other answers, you should really consider using another approach. I've always found scanf way too limited for any serious input reading and processing. It's a better idea to just read whole lines in with fgets and then working on them with functions like strtok and strtol (which BTW will correctly parse integers and tell you exactly where the invalid characters begin).

Comments

3

I had similar problem. I solved by only using scanf.

Input "abc123<Enter>" to see how it works.

#include <stdio.h>
int n, num_ok;
char c;
main() {
    while (1) {
        printf("Input Number: ");
        num_ok = scanf("%d", &n);
        if (num_ok != 1) {
            scanf("%c", &c);
            printf("That wasn't a number: %c\n", c);
        } else {
            printf("The number is: %d\n", n);
        }
    }
}

1 Comment

This still doesn't solve the problem entirely, since if you enter a combination of alphanumeric characters, like 6y: Input Number: 6y will result in : The number is: 6 Input Number: That wasn't a number: y the program reads the input character by character, when it finds a number character in the input, it thinks the input is a number, and when it finds a non numeric one it thinks it is not a number, but can not decide that 6y is not a number altogether, and of course in the process due to the [Enter] key being still present in the buffer, the same problem arises.
1

On some platforms (especially Windows and Linux) you can use fflush(stdin);:

#include <stdio.h>

int main(void)
{
  int number, p = 0, n = 0;

  while (1) {
    printf("-> ");
    if (scanf("%d", &number) == 0) {
        fflush(stdin);
        printf("Err...\n");
        continue;
    }
    fflush(stdin);
    if (number > 0) p++;
    else if (number < 0) n++;
    else break; /* 0 given */
  }

  printf("Read %d positive and %d negative numbers\n", p, n);
  return 0;
}

5 Comments

Please read Using fflush(stdin) — especially the comments to the question — for information about this. It works on Windows because Microsoft documents that it does; it doesn't work anywhere else (that I know of) in practice, notwithstanding some documentation suggesting the contrary.
It works on Linux now (or I should say glibc). It didn't before, and I don't know when they changed it. But the last time I tried it on a mac it crashed, and it is not in the standard, so I've added a warning about portability to this answer.
Not working for me here with my current version. $ ldd --version gives ldd (Debian GLIBC 2.19-18+deb8u9) 2.19. That should give all info needed. Anyone has a clue why?
fflush input stream is only defined for input streams associated with seekable files (e.g., disk files, but not pipes or terminals). POSIX.1-2001 did not specify the behavior for flushing of input streams, POSIX.1-2008 does, but only in the limited way described.
using fflush(stdin) will cause undefined behavior and is not guaranteed to work portably.
1

The Solution: You need to add fflush(stdin); when 0 is returned from scanf.

The Reason: It appears to be leaving the input char in the buffer when an error is encountered, so every time scanf is called it just keeps trying to handle the invalid character but never removing it form the buffer. When you call fflush, the input buffer(stdin) will be cleared so the invalid character will no longer be handled repeatably.

You Program Modified: Below is your program modified with the needed change.

#include <stdio.h>

int main()
{
    int number, p = 0, n = 0;

    while (1) {
        printf("-> ");
        if (scanf("%d", &number) == 0) {
            fflush(stdin);
            printf("Err...\n");
            continue;
        }

        if (number > 0) p++;
        else if (number < 0) n++;
        else break; /* 0 given */
    }

    printf("Read %d positive and %d negative numbers\n", p, n);
    return 0;
}

1 Comment

See Using fflush(stdin) for a disquisition on why using fflush(stdin) anywhere other than Windows is fraught.
0

try using this:

if (scanf("%d", &number) == 0) {
        printf("Err...\n");
        break;
    }

this worked fine for me... try this.. the continue statement is not appropiate as the Err.. should only execute once. so, try break which I tested... this worked fine for you.. i tested....

Comments

0

When a non-number is entered an error occurs and the non-number is still kept in the input buffer. You should skip it. Also even this combination of symbols as for example 1a will be read at first as number 1 I think you should also skip such input.

The program can look the following way.

#include <stdio.h>
#include <ctype.h>

int main(void) 
{
    int p = 0, n = 0;

    while (1)
    {
        char c;
        int number;
        int success;

        printf("-> ");

        success = scanf("%d%c", &number, &c);

        if ( success != EOF )
        {
            success = success == 2 && isspace( ( unsigned char )c );
        }

        if ( ( success == EOF ) || ( success && number == 0 ) ) break;

        if ( !success )
        {
            scanf("%*[^ \t\n]");
            clearerr(stdin);
        }
        else if ( number > 0 )
        {
            ++p;
        }
        else if ( number < n )
        {
            ++n;
        }
    }

    printf( "\nRead %d positive and %d negative numbers\n", p, n );

    return 0;
}

The program output might look like

-> 1
-> -1
-> 2
-> -2
-> 0a
-> -0a
-> a0
-> -a0
-> 3
-> -3
-> 0

Read 3 positive and 3 negative numbers

Comments

0

I had the same problem, and I found a somewhat hacky solution. I use fgets() to read the input and then feed that to sscanf(). This is not a bad fix for the infinite loop problem, and with a simple for loop I tell C to search for any none numeric character. The code below won't allow inputs like 123abc.

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(int argc, const char * argv[]) {

    char line[10];
    int loop, arrayLength, number, nan;
    arrayLength = sizeof(line) / sizeof(char);
    do {
        nan = 0;
        printf("Please enter a number:\n");
        fgets(line, arrayLength, stdin);
        for(loop = 0; loop < arrayLength; loop++) { // search for any none numeric charcter inisde the line array
            if(line[loop] == '\n') { // stop the search if there is a carrage return
                break;
            }
            if((line[0] == '-' || line[0] == '+') && loop == 0) { // Exculude the sign charcters infront of numbers so the program can accept both negative and positive numbers
                continue;
            }
            if(!isdigit(line[loop])) { // if there is a none numeric character then add one to nan and break the loop
                nan++;
                break;
            }
        }
    } while(nan || strlen(line) == 1); // check if there is any NaN or the user has just hit enter
    sscanf(line, "%d", &number);
    printf("You enterd number %d\n", number);
    return 0;
}

Comments

0

To solve partilly your problem I just add this line after the scanf:

fgetc(stdin); /* to delete '\n' character */

Below, your code with the line:

#include <stdio.h>

int main()
{
    int number, p = 0, n = 0;

    while (1) {
        printf("-> ");
        if (scanf("%d", &number) == 0) {
            fgetc(stdin); /* to delete '\n' character */
            printf("Err...\n");
            continue;
        }

        if (number > 0) p++;
        else if (number < 0) n++;
        else break; /* 0 given */
    }

    printf("Read %d positive and %d negative numbers\n", p, n);
    return 0;
}

But if you enter more than one character, the program continues one by one character until the "\n".

So I found a solution here: How to limit input length with scanf

You can use this line:

int c;
while ((c = fgetc(stdin)) != '\n' && c != EOF);

Comments

0
// all you need is to clear the buffer!

#include <stdio.h>

int main()
{
    int number, p = 0, n = 0;
    char clearBuf[256]; //JG:
    while (1) {
        printf("-> ");
        if (scanf("%d", &number) == 0) {
            fgets(stdin, 256, clearBuf); //JG:
            printf("Err...\n");
            continue;
        }

        if (number > 0) p++;
        else if (number < 0) n++;
        else break; /* 0 given */
    }

    printf("Read %d positive and %d negative numbers\n", p, n);
    return 0;
}

Comments

-1

Flush the input buffer before you scan:

while(getchar() != EOF) continue;
if (scanf("%d", &number) == 0) {
    ...

I was going to suggest fflush(stdin), but apparently that results in undefined behavior.

In response to your comment, if you'd like the prompt to show up, you have to flush the output buffer. By default, that only happens when you print a newline. Like:

while (1) {
    printf("-> ");
    fflush(stdout);
    while(getchar() != EOF) continue;
    if (scanf("%d", &number) == 0) {
    ...

3 Comments

Adding this while-loop before the if-statement does result in a wrong program behaviour. To be exact, the "->" prompt isn't shown after the first input, may it either be right or wrong.
Your while loop will consume everything, '\n' included.
Afaik fflush() doesn't work the same way on each system. At least on my Linux box, the fflush(stdout) doesn't help to show up the "->" prompt. Also, a call to setvbuf() doesn't help here, too.
-1

Hi I know this is an old thread but I just finished a school assignment where I ran into this same problem. My solution is that I used gets() to pick up what scanf() left behind.

Here is OP code slightly re-written; probably no use to him but perhaps it will help someone else out there.

#include <stdio.h>

    int main()
    {
        int number, p = 0, n = 0;
        char unwantedCharacters[40];  //created array to catch unwanted input
        unwantedCharacters[0] = 0;    //initialzed first byte of array to zero

        while (1)
        {
            printf("-> ");
            scanf("%d", &number);
            gets(unwantedCharacters);        //collect what scanf() wouldn't from the input stream
            if (unwantedCharacters[0] == 0)  //if unwantedCharacters array is empty (the user's input is valid)
            {
                if (number > 0) p++;
                else if (number < 0) n++;
                else break; /* 0 given */
            }
            else
                printf("Err...\n");
        }
        printf("Read %d positive and %d negative numbers\n", p, n);
        return 0;
    }

2 Comments

gets is horribly unsafe and should never be used (it has been removed from standard C for that reason).
i agree it is dangerous friend and i only used it here for a small application (hence the subjective 40 char array). if the problem at hand were more objective in its requirements then ya ^^.
-1

I've recently been through the same problem, and I found a solution that might help a lot of people. The function "scanf" leaves a buffer in memory ... and that's why the infinite loop is caused. So you actually have to "store" this buffer to another variable IF your initial scanf contains the "null" value. Here's what I mean:

#include <stdio.h>
int n;
char c[5];
int main() {
    while (1) {
        printf("Input Number: ");
        if (scanf("%d", &n)==0) {  //if you type char scanf gets null value
            scanf("%s", &c);      //the abovementioned char stored in 'c'
            printf("That wasn't a number: %s\n", c);
        }
        else printf("The number is: %d\n", n);
    }
}

1 Comment

scanf("%s", &c) is a type error. %s takes a char *, not a char (*)[5]. Also, since you're not limiting the number of characters read, this is a buffer overflow waiting to happen. Simply discarding the input would be a much better idea (%*s).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.