Reputation: 1033
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?
Upvotes: 1
Views: 149
Reputation: 60275
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.
Upvotes: 4
Reputation: 90316
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.
Upvotes: 0