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

cin ALMOST does what I want!

P: 2
Hello,
I am writing a script that needs to get a numeric value from the keyboard. I have already solved the problem of when a user inputs a string, so that is not the problem! Now I need to solve the problem of when a user inputs a mixed type. Here is an example of what I mean, the script:
------------------------------------------------------------------------------------------------------
#include <iostream>
#include "CinHelp.h"

int main()
{
int p;
double r;
CinHelp myin;

cout << "Enter an int: ";
p = myin.getInt();
cout << p << endl;

cout << "Enter a double: ";
r = myin.getDouble();
cout << r << endl;
return (0);
}
------------------------------------------------------------------------------------------------------

This script uses a class I wrote to deal with the case where a user enters a string such as 'pizza' instead of a number. The class:
------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;

class CinHelp
{
public:

int getInt()
{
int inpt;

cin >> inpt;

while (cin.fail())
{
cout << endl
<<"Looking for an integer (1, 2, 3, etc.). Try again. ";
cin.clear();
cin.ignore(INT_MAX, '\n');
cin >> inpt;
}

return(inpt);
}


double getDouble()
{
double inpt;
cin >> inpt;

while (cin.fail())
{
cout << endl
<<"Looking for a number (1, 2.3, etc.). Try again. ";
cin.clear();
cin.ignore(INT_MAX, '\n');
cin >> inpt;
}

return(inpt);
}

};
------------------------------------------------------------------------------------------------------
This solves the problem of a user doing something like:
Enter an int: pizza
but it does not solve the problem of a user doing something like:
Enter an int: 44tre
If you copy these and run them, you will see that the characters are stuck in the stream somehow and end up messing with the next cin call. Can anyone please help me modify my CinHelp class to deal with this situation? I have no programming experience at all as of 3 weeks ago, I am learning completely on my own, so sorry if this is a really stupid question!
Thanks.
Nov 16 '06 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
when converting a decimal numeric value from characters (e.g. reading from cin) the conversion stops at the first non-digit and so long as some digits have been converted the stream is error free. So if 44tre is entered when reading an int, 44 is returned and the next conversion can fail.
What you can do is to check if the next character is white space and if not call cin.ignore() to skip the rest of the line. e.g.
Expand|Select|Wrap|Line Numbers
  1. int getInt()
  2. {
  3. int inpt;
  4. // read int - if fail report error and read again
  5. while (!(cin >> inpt))
  6. {
  7. cout << endl
  8. <<"Looking for an integer (1, 2, 3, etc.). Try again. ";
  9. cin.clear();
  10. cin.ignore(INT_MAX, '\n');
  11. }
  12. // if next character is not whitespace skip characters to end of line
  13. if(!isspace(cin.peek())) cin.ignore(INT_MAX, '\n');
  14. return(inpt);
  15. }
  16.  
of course this will not help if the user enters
44 tt

if you know all numeric input will be one value per line you can call cin.ignore() after every read to clear any surplus characters off the line
Nov 17 '06 #2

P: 2
Thank you horace1.
That works beatifully, and it is simple!

I think I will do this for cases when a user must only enter one number per line. Thanks again.
Nov 17 '06 #3

Post your reply

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