The condition you have in the
while()
is not working.
But why not? Maybe an explanation is in order so the error does not happen again...
while(cin)
{
getline(cin, str);
s_v.push_back(str);
}
In this code, the loop exits after cin
is in an error condition. After reading the last line, there is no error.
You now attempt to read the 4th line, which causes an error. But you don't check the error. str still has line 3 in it because nothing was read this time. So you now push back line 3 again. cin
has an error now and the loop exits, but the damage is done. It's simply a case of not testing for the error at the proper time.