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?