So, I've been trying to figure it out for a while but I can't seem to find an answer. If I was trying to find the age of a person, say with cin, is there any way I could confirm that the input is in fact a number? If I declare age as int it bugs out when a person types in something that isn't a number, but if I put string it obviously opens age up to not being a number.
Is it possible to compare ints and strings, for example, or manipulate strings in some way to check if the string is a number?
#include <limits>
#include <iostream>
int main(int argc, char* argv[]) {
unsignedshort value = 0;
while(1) {
std::cout << "Enter value: ";
if(std::cin >> value) {//if std::cin can read into value successfully without raising any error flags...
break;//break out of the loop, because the user entered valid input
}
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cout << "You entered : " << value << std::endl;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
return 0;
}
Thanks! That worked and now I know I can just use an if statement for that XD I was expecting it to bug out whenever I tried to compare anything not numeric to an int.