Like Tree1Likes
  • 1 Post By laserlight

input validation

This is a discussion on input validation within the C++ Programming forums, part of the General Programming Boards category; I have this simple code for input validation where the only options are 1 and 2. But if the user ...

  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
    21,326
    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.
    Alexius Lim likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 01:52 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,326
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,326
    Yes, that looks correct.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 10:18 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21