cin behavior

I notice that if cin is assigning input to a float, if there's a typo at the end, it stops at the last character that makes sense.

1
2
  float number = 0;
  cin >> number;


If the input is a typo, like 3.5t, 3.5 is assigned to the variable number and I need to use cin.ignore before the next cin to disregard the 't'.

Is this behavior standard? In other words, cin doesn't take all the characters into account up to the next space?
> Is this behavior standard?

Yes.

Formatted input is handled as follows:

1. Unless explicitly specified otherwise, extractors automatically ignore all white space characters (blanks, tabulators, newlines) that precede the item to be extracted. [The classification of a character as white space depends on the character set used. The extractor takes the information from the locale's ctype facet.]

2. When the first relevant character is found, they extract characters from the input stream until they find a separator; that is, a character that does not belong to the item. White space characters in particular are separators.

3. The separator remains in the input stream and becomes the first character extracted in a subsequent extraction.
https://stdcxx.apache.org/doc/stdlibug/28-5.html
Topic archived. No new replies allowed.