What am I not understanding about getline+strings?

The reason it’s appearing to skip the first iteration is because when you do

cin >> size1;

You enter a number and hit the Enter key. cin reads the integer and leaves the newline character unread on the buffer, so that when you call getline, it’s as if you immediately hit the enter key, and getline reads nothing (because it stops before reading the newline character), discards the newline, and puts the empty string in quest1[0]. And that’s why the rest of the getlines work “correctly”.

Add cin.ignore('\n') above your loop to get rid of the lingering '\n', and that should make it work, barring other errors in your code.

And don’t forget to change x = x++ to just x++ to avoid UB.

Leave a Comment