🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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?

  1. A.getline cannot read after cin
  2. B.The newline left after the number is still in the input, and getline reads up to it
  3. C.n was not initialised
  4. 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 →