Buffer problem with getline

Hi,

1
2
3
4
string input;
do
    getline(cin,input);
while (gtp_process_command(input));


For a program-to-program communication (GTP - http://www.lysator.liu.se/~gunnar/gtp/) protocol I thought the above inputreader would be enough. In short all commands by the the other program (the controller) come in form of a full line, and gtp_process_command is able to process those properly. All responses from my program are full lines as well.
When doing tests on a console, this also works fine... (the program discards faulty lines, properly responds to all commands, etc)

But when I connect my program to a controller, an the controller sends multiple commands at once, my program simply ignores the 2nd command.

My guess is, that the second input line from the controller comes before my program is done processing the first line, and thus getline() somehow fails to catch the second input line...
If it is this, how to fix this problem?
If this can't be the case, and above code should under all circumstances catch all lines... well... then I at least know I have to search the bug elsewhere...

edit: of course gtp_process_command always gives back true, except the quit command was called, which wasn't the case here
Last edited on
You are not checking the state of the stream getline is attached to. Which could be causing the problems you are facing. If the stream is empty, getline will not read a line, so if your other line of code after that requires a non empty string, then the program doesn't perform well.
hm?
My if there is no line to read, the program should just wait until it gets another command. so it should getline, until the controller writes another line.
And if getline reads a line my code can't handle(e.g. wrong format, like no words etc), it actually responds properly.

okay... i finally solved my problem.... it wasn't a c++ problem but a problem regarding the protocol used...
Topic archived. No new replies allowed.