How does the the cin.clear and ignore affect the digit "1" that disappeared? |
It doesn't. The digit '1' didn't disappear, it was read into char
letter
and is displayed in the resulting output as the first character.
When entering values at the keyboard, each keystroke is stored in a buffer.
at the prompt
the user types "123.456\n"
The digit '1' is stored in
letter
and the buffer now contains "23.456\n".
Next, the line
cin >> m
reads the "23.456" into the float value
m
. The buffer now contains just "\n".
next, the rest of that statement
>> n;
tries to read an integer. It consumes and discards the newline '\n' and waits for the user to enter a value.
The user types "123.456\n", so the integer part "123" is stored in the
n
, and the buffer now contains ".456\n".
Next the line
resets any error flags which may have been set for the
cin
stream (for example if the user types a non-numeric character when a number is expected, the fail flag is set), and
will read and discard up to 200 characters from the input buffer, or until a newline is read. That will consume all of the ".456\n" from the buffer so it is now empty.
Finally, the line
cin >> n;
again prompts for the integer, the user enters "123.456\n", integer n receives 123 and the buffer contains ".456\n".