Using scanf to accept user input

前端 未结 5 637
轻奢々
轻奢々 2020-12-06 13:52

gcc 4.4.2

I was reading an article about scanf. I personally have never checked the return code of a scanf.

#include 

int main(void)
         


        
相关标签:
5条回答
  • 2020-12-06 14:20

    Don't use scanf directly. It's surprisingly hard to use. It's better to read an entire line of input and to then parse it (possibly with sscanf).

    Read this entry (and the entries it references) from the comp.lang.c FAQ: http://c-faq.com/stdio/scanfprobs.html

    Edit: Okay, to address your additional question from your own edit: If you allow unstructured input, then you're going to have to attempt to parse the string in multiple ways until you find one that works. If you can't find a valid match, then you should reject the input and prompt the user again, probably explaining what format you want the input to be in.

    For anything more complicated, you'd probably be better off using a regular expression library or even using dedicated lexer/parser toolkits (e.g. flex and bison).

    0 讨论(0)
  • 2020-12-06 14:24

    I don't use scanf() for interactive user input; I read everything as text using fgets(), then parse the input as necessary, using strtol() and strtod() to convert text to numeric values.

    One example of where scanf() falls down is when the user enters a bad numeric value, but the initial part of it is valid, something like the following:

    if (scanf("%d", &num) == 1)
    {
      // process num
    }
    else
    {
      // handle error
    }
    

    If the user types in "12e4", scanf() will successfully convert and assign the "12" to num, leaving "e4" in the input stream to foul up a future read. The entire input should be treated as bogus, but scanf() can't catch that kind of error. OTOH, if I do something like:

    if (fgets(buffer, sizeof buffer, stdin))
    {
      int val;
      char *chk;
      val = (int) strtol(buffer, &chk, 10);
      if (!isspace(*chk) && *chk != 0)
      {
        // non-numeric character in input; reject it completely
      }
      else
      {
        // process val
      }
    }
    

    I can catch the error in the input and reject it before using any part of it. This also does a better job of not leaving garbage in the input stream.

    scanf() is a great tool if you can guarantee your input is always well-formed.

    0 讨论(0)
  • 2020-12-06 14:29

    I rarely use scanf. Most of the times, I use fgets() to read data as a string. Then, depending upon the need, I may use sscanf(), or other functions such as strto* family of functions, str*chr(), etc., to get data from the string.

    If I use scanf() or fgets() + sscanf(), I always check the return values of the functions to make sure they did what I wanted them to do. I also don't use strtok() to tokenize strings, because I think the interface of strtok() is broken.

    0 讨论(0)
  • 2020-12-06 14:30

    scanf() has problems, in that if a user is expected to type an integer, and types a string instead, often the program bombs. This can be overcome by reading all input as a string (use getchar()), and then converting the string to the correct data type.

    /* example one, to read a word at a time */
    #include <stdio.h>
    #include <ctype.h>
    #define MAXBUFFERSIZE   80
    
    void cleartoendofline( void );  /* ANSI function prototype */
    
    void cleartoendofline( void )
    {
        char ch;
        ch = getchar();
        while( ch != '\n' )
            ch = getchar();
    }
    
    main()
    {
        char    ch;                     /* handles user input */
        char    buffer[MAXBUFFERSIZE];  /* sufficient to handle one line */
        int     char_count;             /* number of characters read for this line */
        int     exit_flag = 0;
        int     valid_choice;
    
        while( exit_flag  == 0 ) {
            printf("Enter a line of text (<80 chars)\n");
            ch = getchar();
            char_count = 0;
            while( (ch != '\n')  &&  (char_count < MAXBUFFERSIZE)) {
                buffer[char_count++] = ch;
                ch = getchar();
            }
            buffer[char_count] = 0x00;      /* null terminate buffer */
            printf("\nThe line you entered was:\n");
            printf("%s\n", buffer);
    
            valid_choice = 0;
            while( valid_choice == 0 ) {
                printf("Continue (Y/N)?\n");
                scanf(" %c", &ch );
                ch = toupper( ch );
                if((ch == 'Y') || (ch == 'N') )
                    valid_choice = 1;
                else
                    printf("\007Error: Invalid choice\n");
                cleartoendofline();
            }
            if( ch == 'N' ) exit_flag = 1;
        }
    }
    
    0 讨论(0)
  • 2020-12-06 14:32

    I make a loop call fgets until the end of the line is read, and then call sscanf to parse the data. It's a good idea to check whether sscanf reaches the end of the input line.

    0 讨论(0)
提交回复
热议问题