lundi 23 juillet 2018

If statements will always return false in c++ when comparing substrings

else if(ui.find('@') != string::npos)
    {           
        int comma = ui.find(" ");
        string sop_1 = ui.substr(1, comma);
        double op_1,op_2;
        if(sop_1 == "@")
        {
            cout << "op_1 = counter" << "\n";           
            op_1 = counter;
        }
        else
        {
        istringstream(sop_1) >> op_1;
        }
        string sop_2 = ui.substr(comma+1, ui.length());
        if(sop_2 == "@")
        {
            op_2 = counter;
        }
        else
        {
            istringstream(sop_2) >> op_2;
        }
        if(ui.find('+') != string::npos)
        {
            counter = op_1 + op_2;
        }
        else if(ui.find('-') != string::npos)
        {
            counter = op_1 - op_2;
        }
        else if(ui.find('*') != string::npos)
        {
            counter = op_1 * op_2;
        }
        else if(ui.find('/') != string::npos)
        {
            counter = op_1 / op_2;
        }
        cout << counter << "\n";
    }

This code above is part of a calculator program I'm writing (similar to dc for all you *nix users). The code checks for an @ in input, for example +@ 3, this will add 3 to @ or the current counter and setting the result as the new counter. The problem is the lines if(sop_1 == "@") and if(sop_1 == "@") sop_1 and sop_2 are substrs of ui. Using an if statement on them won't work

Aucun commentaire:

Enregistrer un commentaire