1

For one of my C++ assignments, I have to parse sentences like the following:

SET a = 10
SET b = a
PRINT b

To parser these statements, I used the following code:

vector<string> stringSplitter(istringstream& tmp) {
vector<string> tokens;  
string str;
while(tmp.good()) {
    tmp >> str;
    tokens.push_back(str);              
}
return tokens;
}

For error checking I want to ensure the SET command has only 4 tokens and PRINT statements have only 2 tokens. So corresponding conditions that I have when I check a SET command and a PRINT command:

if (tokens.size() != 4) {
cerr << "Error in Line "<< lineNumber <<":Invalid format\n";
return -1;
}

and

if (tokens.size() != 2) {
cerr << "Error in Line "<< lineNumber <<":Invalid format\n";
return -1;
}

The problem I am having is that it works for "SET a = 10" and "PRINT a" Where as it doesn't work if have have a white space at the end of the sentence like "SET a = 10 " and "PRINT a "

Can anyone help with this?

Bart
  • 19,692
  • 7
  • 68
  • 77
ueg1990
  • 1,003
  • 2
  • 19
  • 39
  • there is a SO post on trimming whitepace from std::string here: http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring – David Hope Mar 01 '13 at 16:22

2 Answers2

4

Use

while ( tmp >> str )

tmp.good() only says whether there's anything left at all. You want to know whether getting another string out of it worked.

jthill
  • 55,082
  • 5
  • 77
  • 137
0

The >> operator uses spaces for tokenizing, so you don't get what you want if spaces are ommitted around =.

Instead use a more advanced function to split.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 2
    This is probably correct in the general case, but it appears the OP's grammar is pretty strict. The OP also appears to *want* the tokenizing features of `>>`. – user7116 Mar 01 '13 at 16:27