cin.peek()

K

kill sunday

I'm working on an RPN calculator, and i can't get the input right.

I have to use cin.peek() to check the next character and do whatever i need
with it. I can't get it to look for a specific type of data. i need it to
look and see if its a double, whitespace or an operator. any suggestions?

david crean
 
J

Jerry Coffin

I'm working on an RPN calculator, and i can't get the input right.

I have to use cin.peek() to check the next character and do whatever i need
with it. I can't get it to look for a specific type of data. i need it to
look and see if its a double, whitespace or an operator. any suggestions?

First, I'd advise using an iterator for the input instead of explicitly
reading from a stream -- this will allow you (for example) to parse a
string instead of a stream.

Second, you can't (for one example) determine whether something is a
double using cin.peek -- you get to peek one character ahead, which
would allow you to check whether it's a character that would be part of
a double, but that's about it.

Normally, you want to separate things into a couple of pieces (basically
a lexer and a parser), one of which reads tokens from the input, and the
other of which deals with the expression they form. At least when I've
done it, I've never used peek -- I usually just read characters in, and
when I've gone one too far, I push a character back. Clearly you could
use peek instead, but either way you'll have to deal with things its
way: what you read will always be a character. From there, determining
the 'type' is typically based on the character classification functions
from ctype (e.g. isdigit will help determine whether you're reading a
number).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top