-2

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: {.

Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
  • 1
    Which type is `a`? Show the declaration. – imreal Feb 05 '13 at 19:49
  • How can we possibly help you based on what you've posted? We don't know what `a` is or what value it contains. All you've posted are two *extremely* trivial if statements which are syntactically valid. – user229044 Feb 05 '13 at 19:50
  • ... or what "doesn't work" means... or what "the compare method" is... – tenfour Feb 05 '13 at 19:51
  • a is a string, my code is inside a method checkString(string a). – Iyad Abilmona Feb 05 '13 at 19:51
  • 5
    Welcome to Stack Overflow. Please post a **complete**, **short** program that demonstrates the problem you are having. For more information about this debugging technique, see http://SSCCE.ORG/. – Robᵩ Feb 05 '13 at 19:51
  • @IyadAbilmona: Are you sure you are using double quotes or single quotes (`"` vs `'`) consistently in both cases? – Andy Prowl Feb 05 '13 at 19:52
  • As you can see [here](http://ideone.com/vbZz9O), the program you describe works correctly. Rather than *describe* a program to us, please *show* us a program (preferably a short one). – Robᵩ Feb 05 '13 at 19:55
  • I edited the question to include the code. – Iyad Abilmona Feb 05 '13 at 19:58
  • At the risk of repeating myself, please post a **short**, **complete** program that demonstrates your problem. Your code was neither short nor complete. As you can see [here](http://ideone.com/CJqmJq) it also doesn't demonstrate the problem you describe. If you are having trouble reducing your program to its essential bits, see http://SSCCE.ORG/. – Robᵩ Feb 05 '13 at 20:03
  • Well i added the main method to it, now its the whole code. I removed unnecessary if statements to make it shorter. – Iyad Abilmona Feb 05 '13 at 20:15

2 Answers2

1

If a is a c string then I would look into using the strcmp method. If they are strings under the #include <string> header. I would look into string::compare

CoderDake
  • 1,497
  • 2
  • 15
  • 30
0

It looks like the problem is with getline(infile,STRING,' ').

What happens if your code hits a \n, \t, etc? It will be extracted together with your token and you will end up with a string such as ";\n", which is not the same as ";"

Either change the logic that you use to tokenize your file:

std::ifstream fin("m.c");
while (fin >> STRING ){
   STRINGS.push_back(STRING);
}
fin.close();

Or trim your string.


Update:

And keep in mind that this kind of tokenizer will only work if your tokens are separated by whitespaces (return 0 ; will work as expected, return 0; will be tokenized to return and 0;)

Community
  • 1
  • 1
Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118