A good solution is usually to read the input into a string and then call a function which validates that string. This way you can define the behavior for valid and invalid input and you don't have to rely on cin, which is annoying at best. Here's a somewhat naive implementation of that method:
Code:
#include <iostream>
#include <string>
#include <cctype>
bool validate ( std::string &chk )
{
int i;
for ( i = 0; i < chk.length(); i++ )
if ( !isdigit ( chk[i] ) )
return false;
return true;
}
int main()
{
std::string input;
std::getline ( std::cin, input );
if ( validate ( input ) )
std::cout<<"Everybody is happy!\n";
else
std::cout<<"Bugger, invalid input.\n";
return 0;
}
-Prelude