Oct 18, 2014 at 3:37am UTC
It all depends whether you're asking for a single digit or larger.
If it's a single digit then make guess a char and check whether guess isdigit()
See
http://www.cplusplus.com/reference/cctype/isalpha/?kw=isalpha is gives you lot's of food to solve for more complicated scenarios.
<string> might have a similar functionality, I'm not sure what it is. In fact you could use strings and just run through each character that makes up the string to make sure each is a digit. :)
Last edited on Oct 18, 2014 at 3:42am UTC
Oct 18, 2014 at 3:55am UTC
Just check if
std::cin
failed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main()
{
int input;
std::cin >> input;
if (std::cin.fail())
{
std::cout << "Invalid input. Exiting.\n" ;
return 0;
}
std::cout << "Valid input. Still exiting.\n" ;
return 0;
}
Last edited on Oct 18, 2014 at 3:56am UTC