PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
The input is a sequence of characters, and each input operation consumes only the characters it needs. For the running example, the stream contains the characters 1, 7, a newline, A, d, a, a space, L, o, v, e, l, a, c, e, and a final newline. The newline after 17 is a real character in the stream, not just a signal that the line looked finished on the screen.
1 7 \n A d a [space] L o v e l a c e \n
^
cursor starts hereWhen you run cin >> age, formatted extraction reads the digits 1 and 7 and uses them to set age to 17. It stops when it reaches the newline after 17, because that newline does not belong to the integer. The digits are consumed, but the newline remains as the next character available to another operation.
int age;
string fullName;
cin >> age;String extraction with cin >> fullName first skips whitespace that appears before the next value. After cin >> age, it therefore skips the pending newline. It then stores A, d, and a in fullName, and stops when it reaches the space between Ada and Lovelace.
cin >> age;
cin >> fullName;
// age is 17
// fullName is "Ada"
// the space before Lovelace is still in the streamPressing Enter does not turn every word on that line into one string value. Enter creates whitespace, and the space between Ada and Lovelace is also whitespace. The extraction stops at the first whitespace after the token, so fullName contains Ada while Lovelace and the ending newline remain available.
After cin >> age followed by cin >> fullName on the stream 17\nAda Lovelace\n, what does fullName contain?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
getline uses the newline as its boundary and stores every other character it encounters, including spaces. That means getline is sensitive to where the previous operation left the cursor. If you call it immediately after cin >> age, the next character is already the newline after 17.
cin >> age;
getline(cin, fullName);
// age is 17
// fullName is ""In this exact stream, cin.ignore() can consume that one known pending newline. After it runs, the next character is A. getline then consumes A through e, including the space between the two words, and stops at the ending newline. It stores Ada Lovelace in fullName, but it does not store the ending newline.
cin >> age;
cin.ignore();
getline(cin, fullName);
// age is 17
// fullName is "Ada Lovelace"Which statement belongs between cin >> age and getline(cin, fullName) so fullName becomes "Ada Lovelace" from the exact stream?
cin >> age;
___
getline(cin, fullName);Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Use cin >> fullName when the required value is one token whose end is whitespace. With the cursor positioned before A in the running example, it stores Ada and stops at the space. The operation does not treat the visible line as one value merely because both words were typed before Enter.
Use getline when the required value is the complete line. With the cursor positioned before A, getline stores Ada Lovelace because it keeps the space and stops only at the ending newline. If cin >> age happened immediately before this getline, cin.ignore() is appropriate for this exact input because one pending newline is known to be waiting.
The key question is where the value ends. For the running input, a token ends before the space, while a line ends at the newline after Lovelace. Choose the operation that matches that boundary, and account for the newline left by the earlier integer extraction when getline is next.