473,695 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

istringstream syntax error question


I'm using Bjarne's book to learn C++ and am stuck on the Calc program
in Section 6. Everything works fine except when I try to use
istringstream to parse a token from the command line. I thought I
followed his instructions on page 118 correctly, but I can't get it to
compile without syntax errors in the get_token() function. The whole
program follows with the four syntax errors flagged with the text "//
SYNTAX ERROR". Thanks in advance.

#include <iostream> // I/O
#include <string> // strings
#include <map> // map
#include <cctype> // isalpha(), etc.
#include <sstream> // so that we can use "istringstr eam"
using namespace std;

istream* input; //pointer to input stream

enum Token_value {
PLUS='+', MINUS='-', MUL='*', DIV='/',
PRINT=';', ASSIGN='=', LP='(', RP=')'

Token_value curr_tok = PRINT;

//function prototypes
map<string, double> table;
double expr(bool);
double term(bool);
double prim (bool);
Token_value get_token();
double error(const string&);

double expr(bool get) //add and subtract
double left = term(get);

for (;;) //"forever"
switch (curr_tok) {
case PLUS:
left += term(true);
case MINUS:
left -= term(true);
return left;

double term(bool get) //multiply and divide
double left = prim(get);

for (;;)
switch (curr_tok) {
case MUL:
left *= prim(true);
case DIV:
if (double d = prim(true)) {
left /= d;
return error("divide by 0");
return left;

double number_value;
string string_value;

double prim (bool get) //handle primaries
if (get) get_token();

switch (curr_tok) {
case NUMBER:
{ double v = number_value;
return v;
case NAME:
{ double& v = table[string_value];
if (get_token() == ASSIGN) v = expr(true);
return v;
case MINUS: //unary minus
return -prim(true);
case LP:
{ double e = expr(true);
if (curr_tok != RP) return error("')' expected");
get_token(); //eat ')'
return e;
return error("primary expected");

Token_value get_token()
char ch;
do { //skip whitespace except '\n'
if (!*input.get(ch )) return curr_tok=END; // SYNTAX ERROR
} while (ch != '\n' && isspace(ch));

switch (ch) {
case ';':
case '\n':
return curr_tok=PRINT;
case '*':
case '/':
case '+':
case '-':
case '(':
case ')':
case '=':
return curr_tok=Token_ value(ch);
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '.':
*input.putback( ch); // SYNTAX ERROR
*input >> number_value;
return curr_tok=NUMBER ;
default: //NAME, NAME=, or error
if (isalpha(ch)) {
string_value = ch;
while (*input.get(ch) && isalnum(ch)) // SYNTAX ERROR
string_value.pu sh_back(ch);
*input.putback( ch); // SYNTAX ERROR
return curr_tok=NAME;
error("bad token");
return curr_tok=PRINT;

int no_of_errors;

double error(const string& s)
cerr << "error:" << s << '\n';
return 1;

int main(int argc, char* argv[])
switch (argc) {
case 1:
input = &cin;
case 2:
input = new istringstream(a rgv[1]);
error("too many arguments");
return 1;

table["pi"] = 3.1415926535897 932384626433832 795; //insert
predefined names
table["e"] = 2.7182818284590 452353602874713 527;

while (*input) {
if (curr_tok==END) break;
if (curr_tok==PRIN T) continue;
cout << expr(false) << '\n';
if (input != &cin) delete input;
return no_of_errors;
Jul 22 '05 #1
1 3236
On 29 Jan 2004 13:59:30 -0800 in comp.lang.c++, dc*****@rocketm ail.com
(Donald Canton) was alleged to have written:
*input.putback( ch); // SYNTAX ERROR

Precedence. Everywhere you have *input.somethin g
write (*input).someth ing or input->something

Jul 22 '05 #2

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

Similar topics

by: Samuele Armondi | last post by:
Hi everyone, Since istringstream objects are not assignable, I'm using the following code to allocate some dynamically. My question is: Is this the correct way of doing it? Am I deleting all the allocated memory correctly? Or am I missing something glaringly simple? Thanks in advance, S. Armondi std::istringstream** ArgStream;
by: bml | last post by:
Could you help and answer my questions of istringstream? Thanks a lot! 1. Reuse an "istringstream" istringstream ist; ist.str("This is FIRST test string"); ist.str("This is SECOND test string"); cout << ist.str() << endl;
by: Luther Baker | last post by:
Hi, My question is regarding std::istringstream. I am serializing data to an ostringstream and the resulting buffer turns out just fine. But, when I try the reverse, when the istringstream encounters the two byte shorts, it either thinks it has reached the null terminator? or eof and consequently stops reading values back in. It doesn't matter whether or not I use the std::ios::binary flag when opening the istringstream or the...
by: dinks | last post by:
Hi I'm really new to c++ so please forgive me if this is really basic but im stuck... I am trying to make a data class that uses istringstram and overloaded << and >> operators to input and output data. The data comes in string lines like "OREBlegQ 14854 731.818" which need to be split into a string, int and double when stored in the class. Can anyone help? This is what i have so far: /* Begin Code */ #include <sstream>
by: JustSomeGuy | last post by:
I am passing an istringstream to a function. I want that function to get a copy of the istringstream and not a refrence to it. ie when the function returns I want the istringstream to be unmodified... However when I try to pass it fn(istringstream s) // doesn't compile but fn(istringstream & s) // does.
by: Randy Yates | last post by:
Why does this: string AWord(string& line) { return line; } bool MYOBJECT::MyFunction(string &line) { int day;
by: James Aguilar | last post by:
Hello all, I am trying to use an istringstream to do some input off of cin by lines. The following snippet does not work: char buf; cin.getline(buf, 90); istringstream line1(string(buf));
by: sharmaharish | last post by:
I need a conversion function that converts values from string to a particular type. For this I have a template function that looks like this ... template<class T> T value(const string& s) { istringstream(s); T val; is >val;
by: sergey.lukoshkin | last post by:
Hello everyone! My task is in converting numbers from string to int variables. I used istringstream to perform it. So I wrote simple test function. But it doesn't work as I expected because val is not being changed while the program is running. Could you please tell me why ?? #include <iostream> #include <sstream>
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.