Thread: How would I filter out non-int entries?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    49

    How would I filter out non-int entries?

    I am making a menu for a console program (a homework assignment). The menu is set up so that the use may enter an int 1-9 but when they enter an int outside the 1-9 range they get an "Invalid Entry" messege.


    Currently, if I try to enter a non-int character or a string the program crashes. My question is how do I make it so the program filters out non-int entries?


    Here is my code...

    Code:
    #include <iostream>
    using namespace std;
    
    
    int main(){
    
        int selection;
        
        cout << "Please select from the following:\n\n"
             << "1.  Add Customer\n"
             << "2.  Delete Customer\n"
             << "3.  Change Customer Name\n"
             << "4.  Rent Video\n"
             << "5.  Return Video\n"
             << "6.  Renew Video\n"
             << "7.  Report Overdue Videos\n"
             << "8.  Report Checked Out Videos\n"
             << "9.  Search for Customer\n\n";
        
        cin >> selection;
        cin.get();
        
        if( selection < 1 || selection > 9 )
            cout << "Invalid Entry";
        
        else
            cout << "Valid Entry";
            
        return 0;
    }

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    After you cin >> something, you must check for the inputstream to be in a fail condition using cin.fail(). If it is in a fail state, you cin.clear() it and also ignore the data in the input stream (buffer?) using the cin.ignore() function.

    Code:
    do
    {
         cout << prompt;
         cin >> input;
         
         if (cin.fail())
         {
                  cin.clear();
                  cin.ignore(numeric_limits<streamsize>::max(), '\n');
         }
         else if (input < low || input > high)
                  cout << errMsg << endl;
    }
    while (!cin.fail() && (input < low || input > high));
    
    return input;
    This is a small section of code from a C++ InputValidation class that I had made for my OO C++ class assingments which comes in very handy in all sorts of programs.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
        cout << "Please select from the following:\n\n"
             << "1.  Add Customer\n"
             << "2.  Delete Customer\n"
             << "3.  Change Customer Name\n"
             << "4.  Rent Video\n"
             << "5.  Return Video\n"
             << "6.  Renew Video\n"
             << "7.  Report Overdue Videos\n"
             << "8.  Report Checked Out Videos\n"
             << "9.  Search for Customer\n";
    
        while ( cout << "\nEnter your selection: " && !(cin >> selection)
                && selection>=1 && selection <=9 )
        {
            cout<< "Bad selection!";
    
            cin.clear();
            cin.ignore(std::numeric_limits < int >::max(), '\n');
        }
    is another option from the FAQ

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If the options are '1' through '9' only, another possibility is to read in the input as a character and check it to make sure it is between '1' and '9' (note the single quotes which are necessary to indicate characters instead of numbers). This has the advantage of avoiding the checking of cin for fail state (all characters are valid) and using ignore() and clear().

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    49
    Thanks for your input guys, it's just what I needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM