473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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
Jul 19 '05 #1
1 11370
In article <bm************@ID-183738.news.uni-berlin.de>,
da***@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.
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: wizofaus | last post by:
Is the any reason according to the standard that calling tellg() on an std::ifstream after a call to peek() could place the filebuf in an inconsistent state? I think it's a bug in the VC7...
7
by: Hamburgpear | last post by:
Dear All, Is it possible to reset the value of xxx.peek() after it reachs EOF ? Regards HP
1
by: Dan | last post by:
Hi, I'm having a problem with StreamReader.Peek(). Let's say I open a file and read it to end; then I'd want to move its stream pointer back to the file beginning: I can call BaseStream Seek method...
1
by: Shawn | last post by:
Hi. I'm using this code to loop through all the lines in a text field: While myStreamReader.Peek() > -1 myStreamReader.ReadLine() i = i + 1 End While Now, what I need to do is to loop through...
5
by: Avi Kak | last post by:
Hello: Does Python support a peek like method for its file objects? I'd like to be able to look at the next byte in a disk file before deciding whether I should read it with, say, the read()...
4
by: Manfred Braun | last post by:
Hi All ! I think, there is a bug in the System.Console class related to use the STDIO streams. I am doing a very simple thing in a console-based program named CS Console.In.Peek(); and...
0
boxfish
by: boxfish | last post by:
Hello, I'm trying to make a kind of complicated overloaded input operator in C++, and I need to look ahead a couple of characters on the input stream, and not remove them from the stream until I know...
14
by: Bob Nelson | last post by:
After completing a good book on C (KNK's 2nd edition), I dusted off an oldie for a good laugh or two. It's Traister's ``Mastering C Pointers'' and I am aware of just how bad this book it. See my...
2
by: Terry Reedy | last post by:
Luis Zarrabeitia wrote: Interesting observation. Iterators are intended for 'iterate through once and discard' usages. To zip a long sequence with several short sequences, either use...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.