55

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Many closely related SO questions, including: http://stackoverflow.com/questions/1669821/ – Jonathan Leffler Nov 11 '09 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 :). –  Nov 13 '09 at 20:51
  • See also [Using `fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin). – Jonathan Leffler Sep 15 '16 at 05:32
  • Note that the loop mentioned by user208785 in the [comment](https://stackoverflow.com/questions/1716013/why-is-scanf-causing-infinite-loop-in-this-code#comment1611522_1716013) 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. – Jonathan Leffler Sep 05 '22 at 19:31

16 Answers16

42

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.

Jamison Dance
  • 19,896
  • 25
  • 97
  • 99
  • 1
    If the input is "abc", that code will print "Err. . ." three times. – Teddy Nov 11 '09 at 15:57
  • Yeah, it is pretty ghetto. I will tweak it a bit. – Jamison Dance Nov 11 '09 at 16:44
  • 2
    Now if the input is "ab-10", it will incorrectly remove the minus sign from the input and read "10" as the next number. – caf Nov 11 '09 at 21:21
  • I know it's old, but just change it to `while (!isdigit(c) && c != '-');`, that should also help with minus signs. – Michael Armbruster May 24 '15 at 15:39
  • This still results in multiple input lines, try `4t` and `t4`, `4t` will give you `-> Err. . .` and `t4` will not even give you any errors, but still multiple input lines: `-> -> ` – ilgaar Sep 11 '15 at 15:16
  • You better put the `getchar()` just before the printf("Err. . .\n"); – ilgaar Jan 22 '19 at 13:01
  • 2
    *returning the number of characters consumed* -- Actually, `scanf` returns the number of *fields* successfully read. So `scanf("%d")` might return 1, 0, or EOF. – Steve Summit Jul 09 '21 at 18:40
  • Despite the number of up-votes, this is not a great answer. The condition should be `if (scanf("%d", &number) != 1)` because you could get EOF or 0. You might need to capture the return value (`int rv; if ((rv = scanf(…)) != 1)`) to distinguish between EOF and error (another option would be `feof()`, which might actually be correct here but usually isn't). The variable `c` should be an `int`, not a `char`. The loop should probably be: `int c; while ((c = getchar()) != EOF && c != '\n' && !isdigit(c)) ;` (where I'd place the semicolon on a line on its own to indicate an empty loop body). – Jonathan Leffler Nov 21 '22 at 04:38
  • Note that if there's an erroneous character in the input, it's likely that the rest of the line is not useful. It is usual to read to the end of the line, rather than to stop on a digit after the faulty character. If someone type `a6`, it is not clear that they meant to type `6` and added the `a` — it is probably better to remove all the data. One advantage of the technique of reading a whole line with `fgets()` or POSIX `getline()` and then scanning it with `sscanf()` is that you can report errors better. See also [Using `sscanf()` in a loop](https://stackoverflow.com/q/3975236/15168). – Jonathan Leffler Nov 21 '22 at 04:45
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.)

Teddy
  • 6,013
  • 3
  • 26
  • 38
  • 2
    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. – Tim Post Nov 11 '09 at 15:58
  • @tinkertim: the question specifies gcc on Linux, guaranteeing that `strtol` is available – Andomar Nov 11 '09 at 16:00
  • Also, at least hinting how to turn such extension ON may help :) – Tim Post Nov 11 '09 at 16:02
  • @Andomar: It was getline() that I was taking issue with ;) – Tim Post Nov 11 '09 at 16:03
  • 2
    @TimPost *Both getline() and getdelim() were originally GNU extensions. They were standardized in POSIX.1-2008.* – alexia Sep 06 '14 at 16:14
  • suggesting to use `getline()` will break portability of the code. – ilgaar Dec 02 '18 at 05:40
  • If you use POSIX [`getline()`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) to read a line, you must remember to `free()` the space (or, at least, consider whether it needs freeing, and the answer will usually be "yes" so you end up freeing it). Note that `getline()` typically allocates space even if the first operation returns `-1` (indicating EOF; note that it explicitly returns `-1` and not `EOF`, even though `EOF` is usually `-1`). – Jonathan Leffler Nov 21 '22 at 15:48
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);
Lucas
  • 13,679
  • 13
  • 62
  • 94
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).

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
4

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);
    }
/* ... */
pmg
  • 106,608
  • 13
  • 126
  • 198
  • 2
    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). – Roman Nikitchenko Nov 11 '09 at 16:55
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);
        }
    }
}
j0k
  • 22,600
  • 28
  • 79
  • 90
  • 1
    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. – ilgaar Sep 11 '15 at 14:51
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;
}
Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75
nanotexnik
  • 328
  • 3
  • 10
  • 9
    Please read [Using `fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin) — 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. – Jonathan Leffler Jan 04 '15 at 07:31
  • 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. – Thomas Padron-McCarthy Nov 07 '16 at 08:35
  • 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? – DrBeco May 26 '17 at 16:18
  • 2
    `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. – David C. Rankin Oct 06 '17 at 23:12
  • using `fflush(stdin)` will cause undefined behavior and is not guaranteed to work portably. – ilgaar Dec 02 '18 at 05:32
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;
}
Cabbage Champion
  • 1,193
  • 1
  • 6
  • 21
  • See [Using `fflush(stdin)`](https://stackoverflow.com/q/2979209/15168) for a disquisition on why using `fflush(stdin)` anywhere other than Windows is fraught. – Jonathan Leffler Nov 21 '22 at 04:40
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;
}
ilgaar
  • 804
  • 1
  • 12
  • 31
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....

Krishna Chalise
  • 147
  • 1
  • 15
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
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
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);
salt
  • 820
  • 11
  • 26
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;
}
Jin Guo
  • 9
  • 2
-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) {
    ...
Andomar
  • 232,371
  • 49
  • 380
  • 404
  • 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. –  Nov 11 '09 at 16:17
  • 5
    Your `while` loop will consume everything, `'\n'` included. – pmg Nov 11 '09 at 16:29
  • 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. –  Nov 11 '09 at 16:31
-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
    `gets` is horribly unsafe and should never be used (it has been removed from standard C for that reason). – melpomene Jan 02 '19 at 17:57
  • 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 ^^. – midnightCoder Jan 04 '19 at 10:45
-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);
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    `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`). – melpomene Jan 02 '19 at 17:56