I'm new to c++, I'm writing a program that reads code from a file, and classifies each part of it as an identifier, bracket, keyword, etc..
I'm doing this using if else statements, it works fine except with brackets and semicolons.
for example if(a== "=" ) cout << "a is the equal operator"
works, but if(a== ";" ) cout << "a is a semicolon"
doesn't. I also tried using the compare method, it didn't work either.
Can someone please tell me why that's happening?
Thanks
void checkString(string a)
if(a=="("){
cout << "RPAR: " << a + "\n";
}
else if(a==")"){
cout << "LPAR: " << a + "\n";
}
else if(a.compare("{") == 0){
cout << "LBRAC: " << a + "\n";
}
else if(a=="}"){
cout << "RBRAC: " << a + "\n";
}
else{
cout << "IDENTIFIER: " << a + "\n";
}
}
int main (){
std::vector<string> STRINGS;
string STRING;
ifstream infile;
infile.open("m.c");
while(getline(infile,STRING,' ')){
STRINGS.push_back(STRING);
}
infile.close();
for(int i=0; i<STRINGS.size(); i++){
checkString(STRINGS[i]);
}
return 0;
}
If a is a bracket or a semicolon the program prints IDENTIFIER: {.