After reading a lot of the threads about getline being skipped, I still can't get my program to work.
First I read the user's input. It should be something like "ADD 1". Then I display the value follwing "ADD". I start reading the user's input once again, and the following getline just doesn't want to read from 'ss' for some reasons, and leaves command empty.
Here's the code :
string lecture, command;
getline(cin, lecture);
stringstream ss(lecture);
getline(ss, command, ' ');
while(command.compare("EXIT") != 0) {
if(command.compare("ADD") == 0) {
string id;
getline(ss, id);
cout << id << endl;
}
lecture = "";
command = "";
getline(cin, lecture);
ss.str(lecture);
getline(ss, command, ' ');
}
And the input/output (output with ">" to distinguish) :
ADD 1
>1
ADD 2
(From here -> Goes back to getline(cin, lecture))
I don't understand what I'm doing wrong ? The first getline out of the loop works well, but then it just goes wrong. It's obvious that command stays empty, even after getline. But I can't see why there would be any trailing '\n' when I am at the line getline(ss, command, ' ') as getline discards the '\n', so command should have the new value.
Thanks !
EDIT : Someone commented saying stringstream.str doesn't reset correctly or something, and he was right (why did you delete your answer ?!) ! I know recreate the object stringstream at each loop and it works. I'll leave the thread opened in case there is a nicer solution than recreating the object.
Basically now ss is a pointer, and at each iteration of the loop I do : ss = new stringstream(lecture)