By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

validation in c++

P: 3
i ma trying to check that my input is always an int.
if it w'll be a char it will genertae an error
Jun 4 '07 #1
Share this Question
Share on Google+
5 Replies


100+
P: 147
i ma trying to check that my input is always an int.
if it w'll be a char it will genertae an error
Using standard console I/O?
One way would be to use cin to read an integer and check the return value of cin for error, like:
Expand|Select|Wrap|Line Numbers
  1. int num;
  2. if (!(cin >> num))
  3.   cout << "ERROR";
Jun 4 '07 #2

RedSon
Expert 5K+
P: 5,000
Using standard console I/O?
One way would be to use cin to read an integer and check the return value of cin for error, like:
Expand|Select|Wrap|Line Numbers
  1. int num;
  2. if (!(cin >> num))
  3.   cout << "ERROR";
Thats not going to validate that your input is an int.
Jun 4 '07 #3

RedSon
Expert 5K+
P: 5,000
i ma trying to check that my input is always an int.
if it w'll be a char it will genertae an error
The problem is chars are stored as ints. A char 'Z' is the same as int 90 if you are using ASCII. If you want to see if your input is not a char you would need to see if there is a standard i/o that will allow you to check or you can do something like

if (input > 'A' && input <= 'z')
then you know you have a char
Jun 4 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
Not true:

if (input > 'A' && input <= 'z')
then you know you have a char
You know you have a char when you cin >> to a char variable.

I realize that an A is 65 but if you cin >> int variable, the >> fails because A is not interger. Integer values for cin must be digits only 0 - 9.

Here is a better way to get an int:

Expand|Select|Wrap|Line Numbers
  1. bool GetInt(int& data);   //function prototype
  2.  
  3. int main()
  4. {
  5.      int test;
  6.      bool rval;
  7.      while (1)    //infinite loop. Need CTRL+C to stop program
  8.      {
  9.          rval = GetInt(test);
  10.          if (rval)
  11.          {
  12.              cout << "The integer is: " << test << endl;
  13.          }
  14.          else
  15.          {
  16.              cout << "Error! Eating an input character" << endl;
  17.              //You could use cin.get() here to see what you ate
  18.              cin.ignore();
  19.          }
  20.  
  21.      }
  22.  
  23.      return 0;
  24.  
  25. }
  26.  
  27. bool GetInt(int& data)
  28. {
  29.     cin >> data;
  30.     if (cin.good())
  31.     {
  32.       return true;
  33.     }
  34.     else
  35.     {
  36.         cin.clear();    //clear the fail bit that got us here
  37.         return false;
  38.     }
  39. }
  40.  
Here the cin >> data (an int) is followed by a test of the goodbit. In the case of the A, the goodbit is false. The istream is now in a fail state and the A is still in the input buffer. In this case you clear() the fail state. The A is still in the buffer.

So you have to eat the A and try again.
Jun 4 '07 #5

RedSon
Expert 5K+
P: 5,000
Not true:



You know you have a char when you cin >> to a char variable.

I realize that an A is 65 but if you cin >> int variable, the >> fails because A is not interger. Integer values for cin must be digits only 0 - 9.

Here is a better way to get an int:

Expand|Select|Wrap|Line Numbers
  1. bool GetInt(int& data);   //function prototype
  2.  
  3. int main()
  4. {
  5.      int test;
  6.      bool rval;
  7.      while (1)    //infinite loop. Need CTRL+C to stop program
  8.      {
  9.          rval = GetInt(test);
  10.          if (rval)
  11.          {
  12.              cout << "The integer is: " << test << endl;
  13.          }
  14.          else
  15.          {
  16.              cout << "Error! Eating an input character" << endl;
  17.              //You could use cin.get() here to see what you ate
  18.              cin.ignore();
  19.          }
  20.  
  21.      }
  22.  
  23.      return 0;
  24.  
  25. }
  26.  
  27. bool GetInt(int& data)
  28. {
  29.     cin >> data;
  30.     if (cin.good())
  31.     {
  32.       return true;
  33.     }
  34.     else
  35.     {
  36.         cin.clear();    //clear the fail bit that got us here
  37.         return false;
  38.     }
  39. }
  40.  
Here the cin >> data (an int) is followed by a test of the goodbit. In the case of the A, the goodbit is false. The istream is now in a fail state and the A is still in the input buffer. In this case you clear() the fail state. The A is still in the buffer.

So you have to eat the A and try again.
duh, I forgot cin does more then just read bits from the input stream. Its been so long since I did console apps like that. Guess its time to review the basics.
Jun 4 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.