The code you found is more eloquent but perhaps mine explains more. The cin input stream is a little tricky to do type checking with, so if you notice, both your example and mine use cin.fail() to check if the data types are correct. For more info check this thread: http://www.cplusplus.com/forum/beginner/2957/
int main()
{
int num;
bool valid = false;
while (!valid)
{
valid = true; //Assume the cin will be an integer.
cout << "Enter an integer value: " << endl;
cin >> num;
if(cin.fail()) //cin.fail() checks to see if the value in the cin
//stream is the correct type, if not it returns true,
//false otherwise.
{
cin.clear(); //This corrects the stream.
cin.ignore(); //This skips the left over stream data.
cout << "Please enter an Integer only." << endl;
valid = false; //The cin was not an integer so try again.
}
}
cout << "You entered: " << num << endl;
system("PAUSE");
return 0;
}
My apologies if I am incorrect about something, I'm still new as well and am not entirely familiar with the cin stream.