Validate the type of input in a do-while loop
Asked Answered
R

5

11

Basically, I need to ensure that input is an integer, like so:

do {
    printf("Enter > ");
    scanf("%d", &integer);
} while (/* user entered a char instead of an int */);

I have tried various methods, but it always end up with run-time error or infinite loop when I tried to enter a char. I have knew that fflush(stdin) is an undefined behavior, which is better not to involve it in my code in order to prevent any error plus it no longer works in VS2015 due to some reasons.

The codes below are the method that I have tried:

typedef enum {false, true} bool;
int ipt;
char c;
bool wrong_ipt;

do {
    c = '\0';
    printf("Enter > ");
    scanf("%d%c", &ipt, &c); //infinite loop occurs while a char has been entered
} while (c != '\n');

do {
    c = '\0';
    printf("Enter > ");
} while (scanf("%d", &ipt) != EOF);

do {
    wrong_ipt = false;
    do {
        ipt = NULL;
        printf("Enter > ");
        scanf("%d", &ipt);
        if (ipt == NULL) {
            wrong_ipt = true;
            break;
        }
    } while (ipt == NULL);
} while (wrong_ipt);

Is there anyway other than fflush(stdin) which can be used to prevent the infinite loop when user entered a char in C?

Thank you

Ruminate answered 26/7, 2015 at 2:17 Comment(4)
why not first validate input ?Halfhour
Does this help? #4072690. I don't want to list this as a possible duplicate but this question is directly related.Ultun
I attempted to code to ask for a new input again right after the char or any invalid input has been entered.Ruminate
sorry, but nope, I am trying to make it within c, not cppRuminate
A
5

The problem is that "scanf()" can leave unread data in your input buffer. Hence the "infinite loop".

Another issue is that you should validate the return value from scanf(). If you expect one integer value ... and scanf returns "0" items read ... then you know something went wrong.

Here is an example:

#include <stdio.h>

void discard_junk () 
{
  int c;
  while((c = getchar()) != '\n' && c != EOF)
    ;
}

int main (int argc, char *argv[])
{
  int integer, i;
  do {
      printf("Enter > ");
      i = scanf("%d", &integer);
      if (i == 1) {
        printf ("Good value: %d\n", integer);
      }
      else {
        printf ("BAD VALUE, i=%i!\n", i);
        discard_junk ();
      }
   } while (i != 1);

  return 0;
}

Sample output:

Enter > A
BAD VALUE, i=0!
Enter > B
BAD VALUE, i=0!
Enter > 1
Good value: 1
Acropetal answered 26/7, 2015 at 2:30 Comment(7)
So, is i a bool which stores the return value of scanf()?Ruminate
No, it's not a "bool". It's "The number of input items successfully matched and assigned [by scanf], which can be fewer than provided for, or even zero in the event of an early matching failure. ": linux.die.net/man/3/scanfAcropetal
But how does the discard_junk() fucntion manage to clean the buffer?Ruminate
First, I'd like to emphasize "discard_junk()" isn't recommended for production code. I just wanted to illustrate the problem. It "works" by calling getchar()" in a loop until there's nothing left. Look here. One alternative is to user fgets() (which always reads everything you've typed in) + sscanfAcropetal
It makes sense now, tyRuminate
Hi. Just don't quite understand the first two sentences: The problem is that "scanf()" can leave unread data in your input buffer. Hence the "infinite loop". Could you please give me some detailed explanation? Thanks in advance.Delegate
This answer has one problem: It accepts 6sdfj23jlj as valid input for the number 6, although the input should probably be rejected in this case. See my answer for a solution which also handles this case.December
D
5

This is a very good example of why scanf should generally not be used for user input.

Since user input is line-based, one would expect that an input function would always read one line of input at a time. However, that is not the way that the function scanf behaves. Instead, it consumes only as many characters as are required to match the %d conversion format specifier. If scanf is unable to match anything, then it does not consume any characters at all, so that the next call to scanf will fail for exactly the same reason (assuming that the same conversion specifier is used and that the invalid input is not explicitly discarded by you). This is what is happening in your code.

At the time of this writing, three of the other answers solve this problem by checking the return value of scanf and explicitly discarding the invalid input. However, all three (!) of these answers have the problem that they for example accept "6sdfj23jlj" as valid input for the number 6, although the whole line of input should obviously be rejected in this case. This is because scanf, as previously mentioned, does not read one line of input at a time.

Therefore, the best solution to your problem would probably be to use line-based input using fgets instead. That way, you will always read exactly one line of input at a time (assuming that the input buffer is large enough to store an entire line of input). After reading the line, you can then attempt to convert it to a number using strtol. Even if the conversion fails, the line of input will have been consumed from the input stream, so you will not have most of the problems described above.

A simple solution using fgets could look like this:

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

int main( void )
{
    char line[100];
    long number;

    //retry until user enters valid input
    for (;;) //infinite loop, equivalent to while(1)
    {
        char *p;

        //prompt user for input
        printf( "Please enter a number: " );

        //attempt to read one line of input
        if ( fgets( line, sizeof line, stdin ) == NULL )
        {
            fprintf( stderr, "Unrecoverable input error!\n" );
            exit( EXIT_FAILURE );
        }

        //attempt to convert input to number
        number = strtol( line, &p, 10 );

        //verify that conversion was successful
        if ( p == line )
        {
            printf( "Invalid input!\n" );
            continue;
        }

        //verify that remainder of line only contains
        //whitespace, so that input such as "6sdfj23jlj"
        //gets rejected
        for ( ; *p != '\0'; p++ )
        {
            if ( !isspace( (unsigned char)*p ) )
            {
                printf( "Encountered invalid character!\n" );

                //cannot use `continue` here, because that would go to
                //the next iteration of the innermost loop, but we
                //want to go to the next iteration of the outer loop
                goto continue_outer_loop;
            }
        }

        //input was valid, so break out of infinite loop
        break;

    //label for breaking out of nested loop
    continue_outer_loop:
        continue;
    }

    printf( "Input was valid.\n" );
    printf( "The number is: %ld\n", number );

    return 0;
}

Note that the goto statement should generally not be used. However, in this case, it is necessary, in order to break out of a nested loop.

This program has the following output:

Please enter a number: 94hjj
Encountered invalid character!
Please enter a number: 5455g
Encountered invalid character!
Please enter a number: hkh7
Invalid input!
Please enter a number: 6sdfj23jlj
Encountered invalid character!
Please enter a number: 67
Input was valid.
The number is: 67

However, this code is still not perfect. It still has the following problems:

  1. If the user enters 100 characters of input in a single line, then the entire line won't fit into the input buffer. In that case, two calls to fgets will be required to read the entire line, and the program will incorrectly treat that line as two separate lines of input.

  2. The code does not check if the number the user entered is representable as a long (e.g. whether the number is excessively large). The function strtol reports this by setting errno accordingly (which is a feature that scanf lacks).

These two problems can be fixed too, by performing additional checks and error handling. However, the code is now becoming so complex that it makes sense to put it all into its own function:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <errno.h>

int get_int_from_user( const char *prompt )
{
    //loop forever until user enters a valid number
    for (;;)
    {
        char buffer[1024], *p;
        long l;

        //prompt user for input
        fputs( prompt, stdout );

        //get one line of input from input stream
        if ( fgets( buffer, sizeof buffer, stdin ) == NULL )
        {
            fprintf( stderr, "Unrecoverable input error!\n" );
            exit( EXIT_FAILURE );
        }

        //make sure that entire line was read in (i.e. that
        //the buffer was not too small)
        if ( strchr( buffer, '\n' ) == NULL && !feof( stdin ) )
        {
            int c;

            printf( "Line input was too long!\n" );

            //discard remainder of line
            do
            {
                c = getchar();

                if ( c == EOF )
                {
                    fprintf( stderr, "Unrecoverable error reading from input!\n" );
                    exit( EXIT_FAILURE );
                }

            } while ( c != '\n' );

            continue;
        }

        //attempt to convert string to number
        errno = 0;
        l = strtol( buffer, &p, 10 );
        if ( p == buffer )
        {
            printf( "Error converting string to number!\n" );
            continue;
        }

        //make sure that number is representable as an "int"
        if ( errno == ERANGE || l < INT_MIN || l > INT_MAX )
        {
            printf( "Number out of range error!\n" );
            continue;
        }

        //make sure that remainder of line contains only whitespace,
        //so that input such as "6sdfj23jlj" gets rejected
        for ( ; *p != '\0'; p++ )
        {
            if ( !isspace( (unsigned char)*p ) )
            {
                printf( "Unexpected input encountered!\n" );

                //cannot use `continue` here, because that would go to
                //the next iteration of the innermost loop, but we
                //want to go to the next iteration of the outer loop
                goto continue_outer_loop;
            }
        }

        return l;

    continue_outer_loop:
        continue;
    }
}

int main( void )
{
    int number;

    number = get_int_from_user( "Please enter a number: " );

    printf( "Input was valid.\n" );
    printf( "The number is: %d\n", number );

    return 0;
}
December answered 19/10, 2021 at 19:8 Comment(5)
You are right. Validating the input is prefirable. However, you could have problems by using strtol with integer numbers (in the input string) beyond the range of long. I think the best solution is to check one character at the time, so that the retrieved integer value keeps under LONG_MAX.Mandamus
@pablo1977: I think you are taking the question too literally. Although OP states that they want to ensure that the input is an "integer", it seems to me that they simply want to read an int, and want to make sure that the input is valid. This is what my answer accomplishes. However, you are right that if the program is only supposed to check whether the input is an "integer", and if that integer may be too large to be representable as a long, then the best solution would probably be to check every single character of the input with isdigit, and maybe check for + or - at the start.December
Fairly robust code. It does get fooled in some esoteric cases like reading embedded null characters in as in "123\0xyz\n", but that is always a challenge with fgets().Birdie
@chux: Yes, my code assumes that a character code with the value 0 is the null terminating character. All code which uses C-style strings must make this assumption. Therefore, it is not possible to use the function fgets without making this assumption. If embedded null characters were indeed an issue, then it would be possible to use an input function that does not use C-style strings, such as getchar or fread. However, that would make the code much more complicated.December
@AndreasWenzel getline() allows both a buffer view and string view as the buffer is null character terminated (possible not the only 0 in the buffer) and provides read length. fgets() can be used to detect embedded 0's by prefilling the buffer with non-zeros, yet that is a kludge. Robust handling of input is challenging in C. Given increased hacker attacks, what I look for in text input that might contain 0s is a reasonable strategy to have some sort of defined behavior and no UB.Birdie
M
1

The format specifier %d tells to scanf that an integer value is expected in the command line. When the user enters a line of data, it's read as a string, and then scanf attempts to understand if the entered string can be interpreted as the decimal digits of an integer number.

If this interpretation succeds, then the value thus found is stored in the integer variable you passed as argument.

The number of rigth replacements done by scanf are retrieved by that function in the form of an int value. Since you expect only one input, the value 1 will indicates that everything was fine.

So, if there was an error, as for example, the user entered a non-valid integer number, the number returnd by scanf is less than the number of format specifiers. In this case, a value less than 1 informs that an error happened:

 int ipt, succeded;
 do {
    printf("ipt? ");
    succeded = scanf("%d", &ipt);
    if (succeded < 1) {    // Clean the input
      while (getchar() != '\n') 
        ;
    }
 } while(succeded < 1);
Mandamus answered 26/7, 2015 at 2:36 Comment(3)
You need to clear the input buffer once scanf fails to avoid an infinite loop.Insomnolence
@CoolGuy: Thanks. I added some cleaning (what bothers me is that the cleaning occupies more lines that what is actually relevant here).Mandamus
This answer has one problem: It accepts 6sdfj23jlj as valid input for the number 6, although the input should probably be rejected in this case. See my answer for a solution which also handles this case.December
L
1

Your fundamental mistake is that you never tell your program to consume the invalid input. In words, you told the program:

  • Read an integer if there is one. (Otherwise read nothing)
  • If you didn't get an integer, go back to step 1.

I assume what you thought you were doing was

  • Read the input, and if it was an integer, store it.
  • If it wasn't an integer, go back to step 1.

So what you need to do is to rewrite your code so it does what you meant to do (or come up with some other approach, as in the other answer). That is, write your program to:

  • Read some amount of input. (e.g. a line)
  • Scan the input to see if it is an integer, and store it.
  • If it wasn't an integer, go back to step 1.

Some relevant functions you may find useful are fgets, sscanf, and atoi. (also, try to resist the temptation to write buggy code; e.g. if you intend to read a line of input, make sure you actually do so, and correctly. Many people are lazy and will do the wrong thing if the line is long; e.g. just read part of the line, or cause a buffer overflow)

Lura answered 26/7, 2015 at 2:37 Comment(1)
thx for pointing my mistake too, because I don't really know the working principle of scanf()Ruminate
A
-2

I know I'm a little late to answer this but you could reduce the use of loops and use jump statements instead and then just use one if statement to check for bad input and still achieve the same result

      enter: printf("Enter > ");
      i = scanf("%d", &integer);
      if (i != 1) {
        printf ("BAD VALUE, i=%i!\n", i);
        discard_junk ();
        goto enter:
      }
      printf ("Good value: %d\n", integer);
Alegar answered 10/9, 2020 at 16:27 Comment(1)
Using goto is discouraged in normal code. I would use goto only in very special situations, such that jumping out of some cumbersome bunch of nested loops (which could be, anyway, a symptom of bad programming).Mandamus

© 2022 - 2025 — McMap. All rights reserved.