How do formatted extraction and std::getline interact, and how should code handle leftover delimiters, EOF, parse failure, and stream recovery?
Show answer and explanation
Formatted extraction with operator>> reads a token according to the target type and normally leaves the terminating delimiter, often a newline, in the stream. A following getline can therefore read an empty remainder. If the contract moves to the next physical line, ignore through that newline before getline; input >> std::ws is shorter but also discards leading whitespace and blank lines, so it changes the accepted data. Stream conversion to bool is false when failbit or badbit is set, and eofbit records that the end was reached. For a recoverable format error, clear the state and discard the offending input before retrying or the same extraction will fail forever. Prefer reading a whole line and parsing it separately when records need precise validation, diagnostics, or recovery.
Formatted extraction with operator>> reads a token according to the target type and normally leaves the terminating delimiter, often a newline, in the stream. A following getline can therefore read an empty remainder. If the contract moves to the next physical line, ignore through that newline before getline; input >> std::ws is shorter but also discards leading whitespace and blank lines, so it changes the accepted data. Stream conversion to bool is false when failbit or badbit is set, and eofbit records that the end was reached. For a recoverable format error, clear the state and discard the offending input before retrying or the same extraction will fail forever. Prefer reading a whole line and parsing it separately when records need precise validation, diagnostics, or recovery.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.