简体   繁体   中英

C++ char comparison not working

It is probably better to say I am doing something wrong rather then say the comparison isn't working. But, I have been going over this code for some time now.

I have a recursive function. Most of it is working well, so I will only put the part that isn't working:

//In main
string C[] = {"S=>bS",
                    "S=>aaT",
                    "T=>aT",
                    "T=>bU",
                    "U=>Ua",
                    "U=>aa"};
CFG CFG1(C);

...

string *code;
char startNT;
//The CFG constructor 
CFG::CFG(string C[])
{
    code = C;
    startNT = code[0][0];
}

...

//Inside of processData recursive function
for(unsigned int i = 0; i < code->size(); i++)
{
    if(code[i][0] == startNT)
    {
        string newStr = code[i].substr(code[i].find(">")+1);
        string old = wkString;
        //This is the recursive call
        if(processData(inString, wkString.replace(wkString.find_first_of(startNT), wkString.find_first_of(startNT)+1, newStr)))
        {
            return true;
        }
        cout << wkString << endl;
        wkString = old;

    }
}

The comparison that isn't working is code[i][0] == startNT . Well... I should say, isn't working 100% of the time. It works great until half way through the recursive function, code[i][0] becomes 'S' and startNT becomes 'T' (after already proving that it can compare 'S' and 'T' properly somewhere during the call), and it still evaluates to true, which causes the wkString.replace() to break since it can't find the 'T'.

It has been awhile since I have used C++, so I am probably making a stupid mistake. Thanks for any help.

code is a pointer to the first string in an array of strings. So when you say code->size() , that's the size(number of characters) of the first string(5 in the example you gave). I'm pretty sure you're trying to iterate over the string array, not the characters in the first string. So that's wrong.

Unfortunately, since you store a pointer in the class, and not an array, the size of the array is unknown to the class. So you can't iterate over it properly. You'll need to restructure your code somehow. Without being able to see it more fully, I cannot make any concrete suggestions.

What you probably want to do is store a collection of strings in the actual class. By default, I recommend a vector. Your code might look something like this then:

// in class CFG
std::vector<std::string> code;
char startNT;

CFG(const string * C, int N)
    :code(C, C+N),
    startNT(code[0][0]) // strong assumption made here
{}
...
// processData
for(unsigned int i = 0; i < code.size(); i++) // note, dot instead of arrow
{
...
// in main
std::string C[] = {
    "S=>bS",
    "S=>aaT",
    "T=>aT",
    "T=>bU",
    "U=>Ua",
    "U=>aa"
};

CFG CFG1(C, sizeof(C)/sizeof(*C));

You're using,' i < code->size(); i++', to control the loop, but code is a pointer to a string, so I'm thinking it's telling you the size of the first element of code, not the number of code strings.

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