cin>>

the input: 0.23

1
2
3
4
double i;
int pos;
cin>>i;
pos = cin.tellg();


Why the value of pos is -1?
How can I do to set the pos be the value I want, I mean 4.
I need the value to indicate the position where error occurs

ps: I am doing a expression parser, and I need the pos to tell the user where is the error.
For example:
the input:
>sin(3

output should be:

>sin(3
      ^
Error: ) is expected


but now it always be like this:

>sin(3
^
Error: ) is expected

If you are writing a lexer, you can keep track of the number of characters entered in the current line

http://www.cplusplus.com/reference/iostream/istream/tellg/ :
Return Value
An integral value of type streampos with the number of characters between the beginning of the input sequence and the current position.

Failure is indicated by returning a value of -1.
On Windows, tell/seek don't apply to stdin. I can't think of a good reason why this is the case. It could be generally true, but I don't know.

You get the same result at any level, C++, C buffered or unbuffered io, or direct os calls.
Last edited on
seekg works fine.

Many thanks to Bazzy and kbw
Topic archived. No new replies allowed.