Thread: input validation

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    59

    input validation

    I have this simple code for input validation where the only options are 1 and 2. But if the user entered a character, the loop becomes never-ending. How do i validate for incorrect input datatype?

    Code:
    int input;
    while (input != 1 && input != 2)
    {
         cout << endl << "Invalid input! Try again: ";
         cin >> input;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One approach is to read a line into a string, use that string to create a stringstream, then parse the contents of the stringstream:
    Code:
    int input;
    string line;
    while (getline(cin, line))
    {
        stringstream ss(line);
        if (ss >> input && ss.eof())
        {
            break;
        }
        else
        {
            cout << "\nInvalid input! Try again: ";
        }
    }
    Note that if the user triggers end of file in my example (unlikely, but possible), the loop will terminate without having read into input, so it may be wise to provide input with a default value.
    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

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    59
    what about the validation for
    Code:
     input != 1 && input != 2
    now that the datatype is stringstream, how do i check if the input is 1 or 2?
    Last edited by Alexius Lim; 12-01-2013 at 02:52 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The datatype is still int. The stringstream is just part of the processing to validate the input. This part parses the string for the input: ss >> input && ss.eof()

    So, how are you going to add the extra validation for the value of the parsed input?
    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

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    59
    This is what i got.
    Code:
        while (getline(cin, line))
        {
            stringstream ss(line);
            if (ss >> input && ss.eof() && (input == 1 || input == 2))
            {
                break;
            }
            else
            {
                cout << "\nInvalid input! Try again: ";
            }
        }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, that looks correct.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input Validation
    By caprice150 in forum C++ Programming
    Replies: 4
    Last Post: 10-18-2011, 06:18 AM
  2. Input/Validation
    By nokia123456 in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 11:26 AM
  3. C++ array input validation.
    By c++baby in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2008, 04:39 AM
  4. Input Validation!
    By noaksey in forum C Programming
    Replies: 2
    Last Post: 05-02-2004, 05:19 AM
  5. Input validation
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2001, 11:18 PM

Tags for this Thread