Thread: Simple cin question.

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    41

    Simple cin question.

    I have a menu in a program I am writing and the user inputs an integer to select from the menu. I am trying to catch any input from the user that is not an integer and have the user select again. I just don't know how to check if the input is a non-integer. For example if the user inputs a character on accident then it should catch it.
    Code:
    int sel;
    cout<<"1: Do This"<<endl; 
    cout<<"2: Do this"<<endl;
    cout<<"Selection?";cin>>sel;
    Thanks For The help

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Basically, you need to use a loop. Note that if the user enters invalid input, cin >> sel fails and cin enters an error state. So you can test if the read failed, and if so, you clear the error state, discard what is left on the input stream, (optionally) print an error message, and then print the menu and get the user's selection again.

    An example of this would be:
    Code:
    int sel;
    for (;;)
    {
        cout << "1: Do This" << endl;
        cout << "2: Do this" << endl;
        cout << "Selection? ";
        if (!(cin >> sel))
        {
            cin.clear();
            cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
            cout << "Invalid selection. Please try again.\n";
        }
        else
        {
            cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
            break;
        }
    }
    You could also use a do while loop, perhaps with a function.

    EDIT:
    Incidentally, it occurred to me that my original example does not take into account valid followed by invalid input on the same line. You have a choice in such situations: either read the entire line as a string, parse it, and then report an error if invalid input is found at all, or simply accept the valid portion, then discard the invalid portion. This is what I have done with my corrected example with another cin.ignore() call before the break.
    Last edited by laserlight; 02-08-2008 at 12:10 AM. Reason: Code correction.
    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
    Apr 2005
    Posts
    41
    Thanks a lot for your help =).

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> my original example does not take into account valid followed by invalid input on the same line.

    Another solution that handles this and still considers it an error is:
    Code:
    int sel;
    for (;;)
    {
        cout << "1: Do This" << endl;
        cout << "2: Do this" << endl;
        cout << "Selection? ";
        if (!(cin >> sel) || cin.get() != '\n')
        {
            cin.clear();
            cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
            cout << "Invalid selection. Please try again.\n";
        }
        else
        {
            break;
        }
    }
    The extra code ignores the trailing newline if the read was successful, but if there's garbage after the int it shows the error and asks the user to try again. This will stop the user from entering something like 1.2 when the input should be an integer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. cin question
    By Zico in forum C++ Programming
    Replies: 10
    Last Post: 04-12-2002, 05:25 AM
  4. cin question....
    By Alien_Freak in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2002, 11:29 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM