use atoi() or some other String to Int function, and only if the function returns null (atoi() returns null upon failure) can you continue. This is not the best solution, because if the user inputs the number 0 (or NULL), It will pass the test.
You could print the numbers into an array and read them against a equal ASCII string.
Or you could use something like this.
int Game::validateStrInput( std::string * str )
{
std::cout << std::endl;
std::cin >> ( *str );
( *str );
for ( int i = 0; ( ( unsigned int )i < ( str->size( ) ) ); i++ )
{
if ( isdigit( ( *str )[ i ] ) )
{
continue;
}
else
{
std::cout << "Please Enter a valid string."; //ERROR
validateStrInput( str );
}
}
return ( guess = std::stoi( ( * str ) ) );
}
This is still clunky, because It's recursive, it will print your error message for each invalid character. It also uses both the hideous break and continue statements. You should make it better.
I would start by breaking it down into more functions and checking their single return value.
But. Don't forget this! It's better and more elegant than any of my statements above.
try
{
//...
}
catch
{
//Handle your ERROR!
}
Good luck.