User Input cin in C++
C++
If an input variable is declared, and the value collected by std::cin
does not match, std::cin
returns false.
For example:
int input = 0;
std::cin >> input;
// Entering an integer at this point works.
// Entering a value which is not an integer and not numeric, std::cin returns false.
// Entering a real number (with a decimal point): fractional part is discarded and integer is saved.
It’s better to take input within a loop, with some input validation:
#include <iostream>
#include <typeinfo>
#include <limits>
int main()
{
int input = 0;
while (1) {
std::cout << "Enter an int:\n";
// Enter this block if taking input from cin has failed.
if (!(std::cin >> input)) {
// The error flag is set on std::cin - future attempts to
// get input will fail unless the error is cleared.
std::cin.clear();
// The failed input is in the input buffer. The default for `ignore` is
// to skip a single character. To be sure, remove the max streamsize
// number of chars up until a newline is encountered
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
continue;
}
break;
}
std::cout << "You entered: " << input << '\n';
return 0;
}
std::cin
returns false if the input can’t match the expected type (in this case, int).std::cin.ignore()
extracts and discards unwanted values.std::cin.clear()
changes the internal state of the stream - unsets the error flag.
For a working example, see here
References
comments powered by Disqus