cin.peek()



 DEVELOP > c-Plus-Plus > cin.peek()

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "kill sunday"
Date: 19 Oct 2003 11:17:54 AM
Object: cin.peek()
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
.

User: "Jerry Coffin"

Title: Re: cin.peek() 21 Oct 2003 11:05:37 AM
In article <bmudiu$qnedv$1@ID-183738.news.uni-berlin.de>,
david@manicelement.com says...

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).
--
Later,
Jerry.
The universe is a figment of its own imagination.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER