The problem is that operator>>
ignores whitespace (i.e. ' ', '\t', '\n'
) before a field, i.e. it reads until before the next whitespace.
getline
on the other hand reads until and including the next line break, and returns the text before the linebreak.
Consequently, if you do first operator>>
before a line-break and then getline
, the operator>>
will read until before the line-break, and getline will read only until after the line-break, returning an empty string.
Note: what you have in the input buffer after entering "20, 20, mystring" is effectively
20\n20\nmystring
Hence
- the first
operator>>
reads and returns20
- the second
operator>>
reads until after the second20
, swallows the first\n
and returns the second20
getline
reads until the second\n
and returns the text before that, i.e. nothing.