Peter87 wrote: |
---|
If the user enters "123;.45" the gross variable will be set to 123 and the loop will not run, so ";.45" still remains inside the stream. If you want to remove the rest of the line you need to call ignore after >> has succeeded as well. |
In these cases it would probably be best to clear the input and re-prompt the user for the correct input. This why was I tried replacing
1 2 3 4
|
cin.ignore(numeric_limits<streamsize>::max(), '\n');
[code]
with just [code]cin.ignore()
|
.
I'm sorry. I should have made that clearer. So there are currently 3 error conditions I have tested with this code.
Condition 1: The user inputs only non-numeric characters. (eg. "a" or "abc")
Condition 2: The user inputs non-numeric characters first followed by the correct numeric value. (eg. "a123.45" or "abc123.45")
Condition 3: The user starts to enter a numeric value but typos non-numeric characters after entering at least 1 numeric value. (eg. "123a.45" or "123.45a")
Condition 1 results in prompting the user to input a correct value.
Condition 2 results in storing the value that proceeds the non-numeric character
Condition 3 results in storing only the numeric value that precedes the first non-numeric character and leaves the remaining input on the cin buffer.
For this situation, it would be best if all three conditions responded the way that Condition 1 responds.
Adding cin.ignore(); after the while loop also does not clear the remaining items in the input buffer.