check for bad input

This is a discussion on check for bad input within the C++ Programming forums, part of the General Programming Boards category; I have an integer variable that that I need to check if someone enters only integers. I have the following ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    3

    check for bad input

    I have an integer variable that that I need to check if someone enters only integers. I have the following code but if they enter something such as "dfgdsfdsfdsf" but it goes into an infinite loop and doesnt wait for the user to input data again. The variable guess is the variable that is an integer and cannot accept characters. The following is what I have so far:

    rndNbr = randomNbr(); //Generate a random number

    cout << "I have a number between 1 and 1000.\n";
    cout << "Can you guess my number?\n";
    cout << "Please type your first guess.";

    do
    {
    if(!(cin >> guess)||(guess > 1000)||(guess < 1))
    {
    cin.clear();
    cout << "\n" << "Not between 1 and 1000! Try again.\n";
    }
    }
    while((guess > 1000)||(guess < 1));

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    There are a bunch of ways to validate input, but one thing you have to remember to do if cin fails is clear the stream and discard the bad stuff. Since a failed call to cin doesn't read the bad stuff, you're stuck with it until you throw it away :-)
    Code:
    #include <iostream>
    
    using namespace std;
    
    int getInt()
    {
      int ret;
    
      if (!(cin>> ret) || ret < 1 || ret > 1000)
      {
        // Not good, but let the caller recover
        throw 1;
      }
    
      return ret; // It's good
    }
    
    int main()
    {
      while (true)
      {
        try
        {
          cout<<"Enter a number: ";
          cout<<"Your number is "<< getInt() <<endl;
        }
        catch(int)
        {
          cout<<"Invalid input"<<endl;
          cin.clear(); // Clear the stream
          while (cin.get() != '\n')
          {} // Discard bad stuff
        }
      }
    }
    *Cela*

  3. #3
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    ummm.

    you could use isdigit(guess) if i am not wrong.
    it checks if the entered number is digit.
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  4. #4
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    ummm.

    you could use isdigit(guess) if i am not wrong.
    it checks if the entered value is digit.
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    isdigit() only works for chars.

  6. #6
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    aha...

    i guess it also works for string.
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>i guess it also works for string.
    Only characters in the string, not whole strings though :-)
    *Cela*

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by PJYelton
    isdigit() only works for chars.
    isdigit() actually takes an int.

  9. #9
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Ok, it does take an int, but its meant to see if a char is in the ASCII range of '0'-'9'. So isdigit(5) will return false even though 5 is a number.

  10. #10
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Use a default

    I'm not going to give an example, 'cause all of my programs here at work are in C (Yuch!). But, I almost always offer the user a default entry. So, if the user simply presses [ENTER], or enters something invalid, he/she gets the default. (I also usually report-back that the default is being used.)

    So, my prompts might be something like these:

    cout << "Number of channels to test (1-4) [4] " << endl ;

    cout << "Test #2 ***FAILED*** Continue? Y/N [Y] " << endl ;

    In the first case, if the user enters anything other than 1 thru 4, the program will behave as if he/she entered 4. In the second case, anything other than an upper or lower case "N", and the program will assume "Yes".

    In this way, our programs "work" no matter what the user enters... just maybe not doing what the user intended.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. allegro issues
    By mramazing in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2009, 11:56 PM
  4. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 07:41 PM
  5. check if user input matches a word
    By fakebloo in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2004, 07:12 PM

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