int a;
cin >> a;
could I check to make sure that what they gave me was an integer instead of a letter? If I can, is there any way to give them an error message and then have them try again?
Thanks,
Joe Kerlin
Checking input
Greetings all,
I was wondering, when you get input from a user is there a way to check if they put in the right type of information? For example, if I had this:
You could use isalpha(int AsciiVal) defined in to determine if the supplied character is a letter, you could also use isdigit.
I would think carefully about the data types you are using with cin , your int variable 'a' should not be an int but an array of chars or better yet a string.
Once you have determined the user has entered a number you can use atoi to put it into an int. *Note string is a C++ class and probably has it’s own member functions equivalent to the C functions I have described.
A good validation loop for C would be like this:
Hope that helps.
[edited by - KiwiMelon on February 13, 2003 10:21:37 PM]
[edited by - KiwiMelon on February 13, 2003 10:23:17 PM]
I would think carefully about the data types you are using with cin , your int variable 'a' should not be an int but an array of chars or better yet a string.
Once you have determined the user has entered a number you can use atoi to put it into an int. *Note string is a C++ class and probably has it’s own member functions equivalent to the C functions I have described.
A good validation loop for C would be like this:
Get User Input While Not a Digit Display Error Message Get User InputEnd While
Hope that helps.
[edited by - KiwiMelon on February 13, 2003 10:21:37 PM]
[edited by - KiwiMelon on February 13, 2003 10:23:17 PM]
cin
will return false if the input is invalid.For example, if the user enters 's', of type
char
, for the value for the int
variable a
, cin
will return false
:
int a;
cin >> a; // user enters 's'
if (!cin) // cin will return false
cout << "So this would be outputted ...";
When
cin
encounters bad input, it sets something (the failbit
) which stops it from recieving anymore input. So, to read input again for when we ask the user to re-enter the value, we need to turn the failbit
off (make it read again). To do this, we use the clear()
member function:
cin.clear();
When
cin
encounters bad input, though, it leaves the bad input in the input queue. We need to remove this, else when we read input from cin
again, it will read the bad input again. To read from the input queue one character at a time, until the newline character ('\n'
) left at the end of the bad input has been read, we use the get()
member function. get()
reads one character at a time at a time:
// get the character from the input queue; if it is not the
// newline character, also get the next character, and so on,
// until the newline has been encountered and removed
while (cin.get() != '\n')
continue;
You can put this all in a loop. When
cin
returns false
you tell the user that they have entered an invalid value, ask them to re-enter and then start another iteration, getting the user to enter a value, testing whether cin
is still false
, and then continuing depending on the input. This is how you could do it: #include <iostream>using namespace std;// ...cout << "Enter the amount of cheese you have: ";int cheeseAmount;while (!(cin >> cheeseAmount)) // cin >> cheeseAmount will get input from the user and return cin - if cin returns false, execute loop body, else skip{ cin.clear(); while (cin.get() != '\n') continue; cerr << "Error: You have entered invalid input.\n"; cout << " Please re-enter: "; // now back to loop condition, which will get another value from the user and test to see whether it is invalid}cout << "Well done! You entered an integer - have a medal!\n\n";// ...
It is, infact, very simple; it just may look a little messy because of the comments. It is very easy and neat though.
Hope this helped a bit. Good luck.
[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on February 14, 2003 11:29:03 AM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement