+ Reply to Thread
Results 1 to 3 of 3

Thread: Read an Integer from the User

  1. #1
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Read an Integer from the User

    Read an Integer from the User, Part 1

    Obtaining user input can be done in many surprisingly different ways. This code is somewhere in the middle: safer than scanf("%d", &n), but not bulletproof. It is meant to be a simple but relatively safe demonstration.

    The function mygeti reads user input from the stdin into a string using fgets. Then it attempts to convert this string to an integer using sscanf. If both functions succeed, a 1 is returned; if either fails, a 0 is returned.

    Code:
    #include <stdio.h>
    
    int mygeti(int *result)
    {
            char buff [ 13 ]; /* signed 32-bit value, extra room for '\n' and '\0' */
            return fgets(buff, sizeof buff, stdin) && sscanf(buff, "%d", result) == 1;
    }
    
    int main(void)
    {
            int value;
            do {
                    fputs("Enter an integer: ", stdout);
                    fflush(stdout);
            } while ( !mygeti(&value) );
            printf("value = %d\n", value);
            return 0;
    }
    
    /* my output
    Enter an integer: one
    Enter an integer:
    Enter an integer: 42
    value = 42
    
    Enter an integer: 24f
    value = 24
    
    Enter an integer:    125 help
    value = 125
    */
    Leading whitespace, and other trailing characters are some things not handled. Those issues are handled in Read an Integer from the User, Part 2. Reading a Floating-Point Value from the User can be done similarly.
    Last edited by dcs; 09-27-2008 at 02:55 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: CodeCall Contest #6

    Read an Integer from the User, Part 2

    Some issues, such as leading whitespace and trailing characters that cannot be part of a number, were not handled in Read an Integer from the User, Part 1. Here such issues receive lip service.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int mygeti(int *result)
    {
            char c, buff [ 13 ]; /* signed 32-bit value, extra room for '\n' and '\0' */
            return fgets(buff, sizeof buff, stdin) && !isspace(*buff) &&
            sscanf(buff, "%d%c", result, &c) == 2 && (c == '\n' || c == '\0');
    }
    
    int main(void)
    {
            int value;
            do {
                    fputs("Enter an integer: ", stdout);
                    fflush(stdout);
            } while ( !mygeti(&value) );
            printf("value = %d\n", value);
            return 0;
    }
    
    /* my output
    Enter an integer: one
    Enter an integer:
    Enter an integer: f123
    Enter an integer: 123f
    Enter an integer:  123
    Enter an integer: 123
    Enter an integer: 1.23
    Enter an integer: -42
    value = -42
    */
    
    /* note: this line in the above has a space character following the 123
    Enter an integer: 123
    */

  4. #3
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: CodeCall Contest #6

    Read an Integer from the User, Part 3

    This is a strtol version of Read an Integer from the User, Part 2.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int mygeti(int *result)
    {
       char *end, buff [ 13 ];
       fgets(buff, sizeof buff, stdin);
       *result = strtol(buff, &end, 10);
       return !isspace(*buff) && end != buff && (*end == '\n' || *end == '\0');
    }
    
    int main(void)
    {
       int value;
       do
       {
          fputs("Enter an integer: ", stdout);
          fflush(stdout);
       } while ( !mygeti(&value) );
       printf("value = %d\n", value);
       return 0;
    }
    
    /* my output
    Enter an integer: one
    Enter an integer:
    Enter an integer: f123
    Enter an integer: 123f
    Enter an integer:  123
    Enter an integer: 123
    Enter an integer: 1.23
    Enter an integer: -42
    value = -42
    */

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Read user input with bash?
    By Crop in forum Linux Programming and Scripting
    Replies: 4
    Last Post: 08-29-2009, 09:21 AM
  2. extended to integer
    By Darkyere in forum Pascal and Delphi
    Replies: 2
    Last Post: 12-13-2008, 12:01 PM
  3. Read a Floating-Point Value from the User
    By dcs in forum C Tutorials
    Replies: 2
    Last Post: 09-27-2008, 03:08 PM
  4. Read a Line of Text from the User
    By dcs in forum C Tutorials
    Replies: 1
    Last Post: 09-27-2008, 02:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts