DSA SheetLesson · no judge

PROGRAMMING FUNDAMENTALSC++ FUNDAMENTALS

Taking Input in Strings: Spaces Change What You Read

Reading · 6 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 26

Input remains in the stream until an operation consumes it

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.

TEXTThe exact characters typed are 17, Enter, Ada Lovelace, and Enter.
1 7 \n A d a [space] L o v e l a c e \n
              ^
       cursor starts here

When 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.

CPPAfter this operation, age is 17 and the newline after 17 is still pending.
int age;
string fullName;

cin >> age;

cin >> fullName stops before the space in Ada Lovelace

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.

CPPThe second extraction reads one token, not the complete visible line.
cin >> age;
cin >> fullName;

// age is 17
// fullName is "Ada"
// the space before Lovelace is still in the stream

Pressing 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.

CHECKPOINT 1Not answered

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 reads Ada Lovelace only after the pending newline is consumed

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.

CPPThe immediate getline consumes the pending newline and reads no name characters.
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.

CPPThe pending newline is consumed before getline reads the complete name.
cin >> age;
cin.ignore();
getline(cin, fullName);

// age is 17
// fullName is "Ada Lovelace"
the input stream "17\nAda Lovelace\n" after cin >> ageThe stream contains 1, 7, a newline, Ada Lovelace, and a final newline. After cin >> age, 1 and 7 are consumed and the cursor is on the first newline. One path calls getline immediately, so it consumes that newline and assigns the empty string to fullName. The other path calls cin.ignore(), which consumes the pending newline, and then getline consumes Ada Lovelace plus the final newline while storing only "Ada Lovelace".17\nAdaLovelace\ndirect getline()fullName = ""stops at pending \ncin.ignore()consumes exactlypending \nAdaLovelace\nfullName = "Ada Lovelace"getlineignore firstgetline consumes namefinal \n is discardedage consumedcursor at pending \n
getline starts wherever the previous input operation leaves the cursor.
CHECKPOINT 2Not answered

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.

The boundary you need determines how you read a string

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.

Previous · StringsNext part · Quiz