I have a problem with being able to take care of run-time errors where the user types in data different than the one expected. For example, an integer is expected, but (maybe) the user enters a string. In my console programs, when this happens, I just get a whole lot of junk printed on my screen, it goes into infinite loop. How can I take care of this, such that an error message is displayed when this happens, instead of the whole program going to trash?
int x = 0;
cout << "\n\nEnter a number(1-9): ";
cin >> x;
if(x<1 || x>9)
{
cout<<"\nThe place you entered is invalid. Please enter the correct place number";
}else{
cout<<"Correct";
}
If i entered a number above than 2^31(i know its the maximum value for int datatype) or a char value it goes into a infinite loop. How can i catch solve this problme