Simple cin question.

This is a discussion on Simple cin question. within the C++ Programming forums, part of the General Programming Boards category; I have a menu in a program I am writing and the user inputs an integer to select from the ...

  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
    21,749
    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-07-2008 at 11:10 PM. Reason: Code correction.
    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
    Apr 2005
    Posts
    41
    Thanks a lot for your help =).

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,321
    >> 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, 11:15 AM
  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, 10:29 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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