Thread: Validating Input.

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    Validating Input.

    I would like to take in an input from the user that's supposed to be a number. I want to validate the input, and if it's not a number prompt the user to try again. If it is a number, I want to continue.

    I'm thinking I will need to take the input as a string, and check to make sure it is only one numerical digit. If it is, I can convert it to an integer. My question is this: How do I check to see if it's longer than one character, and whether or not it is a number. For example, if the output is "enter any number: " and the user types in "what?" I would like to prompt them to try again.

    I've done a Google search for "string parsing C" and the results showed me how to use a delimiter to break a string up, and maybe there is a way to use strtok to break off just the first character and discard the rest, but I didn't see anything showing me that.

    Any help is greatly appreciated. Thanks!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by CrunchyMarvin View Post
    I've done a Google search for "string parsing C" and the results showed me how to use a delimiter to break a string up, and maybe there is a way to use strtok to break off just the first character and discard the rest, but I didn't see anything showing me that.
    String parsing is a broad topic, and most of the time when people talk about it it usually means breaking up a list of data points. Doing that might involve strtok(). Turning a string into an integer might be part of it, but not necessarily. And breaking up the long string is usually the part that people have trouble with. Just so you know. It is nice that you tried.

    This page is probably more helpful. FAQ > How do I get a number from the user (C) - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Thank you for the quick reply! I'll read through that link.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Location
    Phoenix
    Posts
    70
    If you are allowed to use scanf, you could just use that. Remember that scanf returns a value, and that value is the number of items that were successfully filled. So if you want to get one integer from the user (you can check to see if it's a single digit after the scanf itself succeeds), and they enter an integer, then scanf will return 1. If they enter a character, scanf will return 0. Depending on if 1 or 0 is returned, you can go from there (if single digit, proceed to next step, if not single digit or if return is 0, try again).

    Otherwise, whiteflags link works for the string parsing route.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Quote Originally Posted by Sorin View Post
    If you are allowed to use scanf, you could just use that. Remember that scanf returns a value, and that value is the number of items that were successfully filled. So if you want to get one integer from the user (you can check to see if it's a single digit after the scanf itself succeeds), and they enter an integer, then scanf will return 1. If they enter a character, scanf will return 0. Depending on if 1 or 0 is returned, you can go from there (if single digit, proceed to next step, if not single digit or if return is 0, try again).

    Otherwise, whiteflags link works for the string parsing route.
    I don't understand what you're saying. My plan is to use scanf("%s", &string[]); how could I use that to see if it's a single integer?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CrunchyMarvin
    My plan is to use scanf("%s", &string[]); how could I use that to see if it's a single integer?
    If you want to do that, then you need to parse the string, e.g., using strtol, checking for an unsuccessful parse. Otherwise, you could get scanf to parse the input for you, e.g., with %d instead of %s, which is probably what Sorin had in mind.

    Oh, and if you do use %s, then you should specify the field width to avoid buffer overflow.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2008
    Location
    Phoenix
    Posts
    70
    Yes, the basic idea was:

    Code:
        int success = 0;
        int number  = 0;
    
        while(success != 1)
        {
            printf("Enter a number: ");
            success = scanf("%d", &number);
            //flush input buffer here in case they input characters
        }
    If success is 1, they entered a number. If it is 0, they didn't. Modify as necessary to check for if the number is single digit or not.

  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Oh ok. I see what you're saying now. That seems like a much, much easier way than parsing and converting a string. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with validating an input.
    By morrjame in forum C Programming
    Replies: 9
    Last Post: 12-20-2010, 09:55 PM
  2. validating input
    By c-rookie in forum C Programming
    Replies: 15
    Last Post: 08-23-2003, 07:55 PM
  3. Validating an input
    By fkheng in forum C Programming
    Replies: 13
    Last Post: 06-22-2003, 11:25 PM
  4. Validating input
    By Barjor in forum C# Programming
    Replies: 7
    Last Post: 02-27-2003, 09:42 PM
  5. validating user input
    By andrew_lillis in forum C++ Programming
    Replies: 10
    Last Post: 10-27-2002, 08:11 AM