C++ is a statically typed language, that means you can not check for data type.
You could accept a string whilst asking for a number, and then parse it starting from the first element in the string, into a number.
see std::atoi and std::strtol for more info.
If you mean how to check if input is number or string you could use the isdigit() function
1 2 3 4 5 6 7 8 9
|
template <typename T>
inline void validated_input(T& value)
{
while( !(std::cin >> value) ) {
std::cout << "Wrong input, try again: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
|
Usage:
int x; validated_input(x);
or
int x; validated_input<int>(x);
Last edited on