Greetings, I am working on trying to get this simple program to catch all errors in the 3 inputs. The user is suppose to put in the integer and the program is suppose to check to see if it is any integer. If it's not an integer display an error and ask the user to input a correct value. I have this thing working pretty good but not what I consider perfect. If I input a letter the correct display shows. Same for symbols. But if I enter a integer followed by a symbol or letter the program will accept it and then for the second input shows a blank input followed by the displayed error message that is suppose to display. Can anyone point me in the right direction? Oh, I've used the isdigit() but did not have good results. I need help. Here is my code:
void main()
{
int NumOfSides = 3;
double Side[3];
for (int i = 0; i < NumOfSides; i++)
{
cout << "\nPlease enter the length of side " << i+1 << ": ";
while(!(cin >> Side[i]))
{
cin.clear();
cin.ignore(255, '\n');
cout << "\nPlease enter a valid length of side " << i+1 << ": ";
}
}
for (int i = 0; i < NumOfSides; i++)
cout << Side[i] << endl;
system("pause");
}
The program shows me the integer value without the symbol or letter but I don't want it to accept it if it's not an integer from the beginning.
I recommend retrieving your input into a std::string, then check that your string only contains valid characters before converting the string to your number.