I understand (!cin) but I need to check it for multiple user input variables, how do I do this?
And I also don't understand cin clear and ignore, so if the user doesn't meet the requirements how do I cout an error message and then reroute the user to reanswer the question.
double cinCheck ()
{
double amount;
bool loopEnd;
do
{
cin >> amount;
if (cin.fail()) // make sure user does not enter a letter
{
cout << "Error. Entry must be a number. \n: ";
cin.clear();
cin.ignore(INT_MAX, '\n');
loopEnd = false;
}
else
{
loopEnd = true;
}
}while(!loopEnd);
return amount;
}
> I understand (!cin) but I need to check it for multiple user input variables
chain cin >> a >> b >> c
if one of the input fails, the ones to the right also fail.
> And I also don't understand cin clear and ignore
if the input fails, cin remains in a failed state and the next input operation will also fail (see above)
so you need to clear the failed state, and for that you may use clear()
given that the input failed, nothing was read, so whatever the user entered remains to be read. perhaps you want to discard that invalid input that make it fail, ignore a couple of characters and continue with the rest, and for that you may use ignore()