Good input validation loop using cin - C++
- by Alex
Hi there,
I'm in my second OOP class, and my first class was taught in C#, so I'm new to C++ and currently I am practicing input validation using cin. So here's my question:
Is this loop I constructed a pretty good way of validating input? Or is there a more common/accepted way of doing it?
Thanks!
Code:
int taxableIncome;
int error;
// input validation loop
do
{
error = 0;
cout << "Please enter in your taxable income: ";
cin >> taxableIncome;
if (cin.fail())
{
cout << "Please enter a valid integer" << endl;
error = 1;
cin.clear();
cin.ignore(80, '\n');
}
}while(error == 1);