← Quiz: Strings in C++: all 10 questions
Quiz: Strings in C++ · Question 4 of 10
After cin n;, why does getline(cin, line); often return an empty string?
After cin >> n;, why does getline(cin, line); often return an empty string?
- A.getline cannot read after cin
- B.The newline left after the number is still in the input, and getline reads up to it
- C.n was not initialised
- D.getline needs a char array
Answer & explanation
Correct answer: The newline left after the number is still in the input, and getline reads up to it
Skip it first with cin.ignore() or getline(cin >> ws, line).
Can you get the rest right without peeking?
Take the timed Quiz: Strings in C++ quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →