简体   繁体   中英

Cannot compare strings in C++

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string>
#include <iostream>

FILE *pfile;
using namespace std;

string temp_string;
string reserved[25] = {"AND", "CALL", "DECLARE", "DO", "ELSE", "ENDDECLARE", "ENDFUNCTION", "ENDIF", "ENDPROCEDURE", "ENDPROGRAM", "EXIT", "FALSE", "FOR", "FUNCTION", "IF", "IN", "INOUT", "NOT","OR", "PROCEDURE", "PROGRAM", "RETURN", "THEN", "TRUE", "WHILE"};


int main(void)
{
    pfile = fopen("hello.cel", "r");
    char cha, temp_token[30], temp;
    int count = 0, check = 1, i;
    cha = fgetc(pfile);
    while (cha != EOF)
    {
        if(isalpha(cha) || cha == '_')
        {
            temp_token[0] = cha;
            count = 1;
            cha = fgetc(pfile);
            while(isdigit(cha) || isalpha(cha) || cha == '_')
            {
                if(count < 30)
                {
                    temp_token[count] = cha;
                    count++;
                }
                cha = fgetc(pfile);         
            }
            count--;
            for(i = 0; i <= count; i++)
            {
                temp_string += temp_token[i];
            }
            cout << temp_string;
            for(i = 0; i < 25; i++)
            {
                if(temp_string == reserved[i])
                {
                    printf(": RESERVED\n");
                }
                else
                {
                    printf(": ALPHA\n");
                }
            }

            cha = ungetc(cha, pfile);
            count = 0;
        }
        fclose(pfile);
}

I have a problem with the comparison statement between the reserved[i] and temp_string strings. I cannot succeed printing "RESERVED", it always print "ALPHA". To your knowledge, this is a program that gets each character from a file (hello.cel) and prints the type of each token.

EDIT: temp_token is a string were I temporary store words. This words have been made by adding characters at this line temp_string += temp_token[i];

temp_string is not declared.

Have you declared temp_string as string? For me it prints Reserved for keywords.

The end of the loop gets a bit sketchy; you've got a missing } , and ungetc() sounds like completely the wrong thing to do. You need to change

            cha = ungetc(cha, pfile);
            count = 0;
        }
        fclose(pfile);
}

to

        }
        cha = fgetc(pfile);
    }
    fclose(pfile);
}

Also declare temp_string just before the loop that populates it (or, if you really want it to be global for some reason, call clear() at that point). Better still, initialise it from the buffer, after removing the pointless count-- :

std::string temp_string(temp_token, temp_token+count);

or even better still, get rid of the temporary buffer, and build the string as you read characters:

        std::string token(1, cha);
        cha = fgetc(pfile);
        while(isdigit(cha) || isalpha(cha) || cha == '_')
        {
            if(token.size() < 30)
            {
                token.push_back(cha);
            }
            cha = fgetc(pfile);         
        }

And finally, only print ALPHA after checking all the reserved tokens:

bool is_reserved = false;
for(i = 0; i < 25; i++)
{
    if(token == reserved[i])
    {
        is_reserved = true;
        break;
    }
}
printf(": %s\n", is_reserved ? "RESERVED" : "ALPHA");

Here is a less broken version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM