tags:

views:

72

answers:

3

Hi, I have a problem where I get an int as input at the wrong time in my c++ program, so I need to "put it back" on cin later. However the closest thing I can find to do this is the pushback function for istreams. This sadly is only for characters and using multiple characters seems to separate them. Is there a good way to put a piece of input back on the front of cin? Thanks.

edit

The reason for this is that I am using a graph class that uses the >> operator for input, but the first piece of input has to be the number of edges. I was hoping to leave the graph class alone and just give cin the number of edges myself. So basically the only piece of input that is out of order is the first int that gets sent from cin.

A: 

Why not just 'keep' the int till you you've finished the next read from cin and then tack it on the front of the read string instead of trying to force it back into the buffer? Just a thought...

rekindleMyLoveOf
+1  A: 

If you are using a seekable input (such as a file), you can use seekg. Unfortunately, this won't work for non-seekable streams (such as a pipe or human input).

R Samuel Klatchko
A: 

Along with the comment, I'd add that you could create a wrapper or helper for the class that accepts input in the order you like it.

Liz Albin