zeion
- 455
- 1
So, I want to use cin >> to get a user input that must only be int.
How can I check the type of the input?
Thanks.
How can I check the type of the input?
Thanks.
The discussion revolves around how to ensure that user input in C++ is of the integer type when using the 'cin' input stream. Participants explore methods for validating input and handling errors when the input does not meet the expected type.
Participants generally agree on the need to handle invalid input but have differing levels of understanding regarding specific functions like 'cin.ignore()' and the implications of using 'cin >>' with different types of input.
There are unresolved questions about the exact behavior of 'cin' when encountering invalid input and the best practices for validating user input in C++. The discussion does not reach a consensus on the most effective method for input validation.
int n;
cout << "Give me an integer: ";
while (!(cin >> n))
{
cout << "Hey dummy, I said give me an integer! Try again: ";
// clears the input stream's status flag
cin.clear();
// skip past the next newline, or 1000 chars,
// whichever comes first
cin.ignore(1000,'\n');
}
cout << "You entered " << n << "." << endl;