Thread: How to handle inputs if the user inputs the wrong thing

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    15

    How to handle inputs if the user inputs the wrong thing

    Did a search, nothing really came up so here goes..

    I have a long floating input in my program where the user needs to input a value with a decimal point.

    Code:
    printf("Please enter the altimeter setting (xx.xx): ");
    scanf("%lf", &s);
    And they input 29.92 or whatever..

    Problem is let's say they input 29,92, or the letter "a", or anything else besides a number and a decimal. That basically sends the program into an uncontrollable loop of sorts and crashes.

    How do I handle incorrect inputs like that? I have a simple while loop to keep unwanted numbers from being entered but I'm not sure about characters and symbols and such..

  2. #2
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    I think pretty much the only way would be to read input as c-string, and take it from there with validation.

  3. #3

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Xupicor View Post
    I think pretty much the only way would be to read input as c-string, and take it from there with validation.
    I see you brought a can, and lots of worms!

    Validating stupid user input is tough, because those stupids are so damn clever!

    Agree 100% with X man, above ^^^. Use fgets(), and then test the string buffer contents, from element 0 to the first end of string char. (there may be other junk char's beyond that, depending)

    Besides just being 100% more robust, fgets() won't let the user's bad input, start the endless looping you can see with scanf(), when it's expecting a number, and the user enters a letter instead.

    (Although you can stop that endless looping by adding a getchar() immediately after the scanf(), inside the loop.)

    But fgets(myBuffer, sizeof(myBuffer), stdin) is soooo good, you'll love it. Just don't do it in public, eh?

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    15
    Ok. This is bringing back memories of the class I took now.. I remember reading that FAQ before and most of it going right over my head.

    I'm going to come back to what you guys said tomorrow after I get some sleep.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    One more thing: The FAQ (linked above) does give some explanation of strtol(), and that is the method I would recommend. When I want an integer from the user, I always use a combo of fgets() + strtol(). If the FAQ is not quite enough for you, in terms of how it works, read this also. Of course, you don't want to get an integer, but a float. However, if you can use strtol() properly, then it is trivial to use strtod() to get a double. See the man pages for strtol() and strtod():



    It's a bit of thinking to understand it all, but in the end you will have a rock solid way of getting numeric user input, and once you have hammered out the code, it is easy enough to re-use it elsewhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program is checking for ent when user inputs out?
    By Blizzarddog in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2003, 01:16 PM
  2. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. Replies: 6
    Last Post: 04-12-2002, 08:33 AM
  5. Using escape sequences as user inputs
    By musayume in forum C Programming
    Replies: 4
    Last Post: 12-11-2001, 09:35 AM