0

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)

Jonathan Taws
  • 1,168
  • 11
  • 24
  • I had a look at this [thread](http://stackoverflow.com/questions/6378662/getline-problem) and I shouldn't have the same problem as I didn't use any cin >> in my program, right ? – Jonathan Taws Nov 05 '14 at 22:03
  • 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. – Jonathan Taws Nov 05 '14 at 22:15
  • 1
    `ss.clear()` should reset any error on the stringstream. No reason to new it all the time. If you need to clear out any remaining data, `ss.str("");` would do that. – Retired Ninja Nov 05 '14 at 22:25
  • I did a mix of @RetiredNinja and RobinHsu answer, which is using only getline with the "\n" added to the stringstream, and using ss.clear() ! Thanks guys. – Jonathan Taws Nov 06 '14 at 19:44

1 Answers1

1

I think your ss does not include an endl and is confused. Another thing strange is that the instruction ss.str(lecture); has no effect on my machine. It's probably a library bug?? Anyway, I use >> instead of getline on ss, since ss can be streamed in/out:

string lecture, command;
getline(cin, lecture);
stringstream ss;
ss << lecture << endl;
ss >> command;

while(command.compare("EXIT") != 0) {
    if(command.compare("ADD") == 0) {
        string id;
        ss >> id;
        cout << id << endl;
    }
    lecture = "";
    command = "";
    getline(cin, lecture);
    ss << lecture << endl;
    ss >> command;
}

Alternatively, to use getline(), I also make it work, with the limitation that typing "EXIT" won't exit, you need to type "EXIT ", i.e. with a space after "EXIT" since it is looking for the space after a command every time. (A bug from your program). Anyway, this is the getline() version:

string lecture, command;
getline(cin, lecture);
stringstream ss(lecture + "\n");
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 + "\n");
    getline(ss, command, ' ');
}
Robin Hsu
  • 4,164
  • 3
  • 20
  • 37
  • In fact, `ss` is not necessary. `cin` itself is a stream. Thus just use `cin >> command` and `cin >> id` in replace of what ss do, for the first solution of mine. – Robin Hsu Nov 07 '14 at 02:59