简体   繁体   中英

how to fix: no operator matches these operands

图片

I do not understand why these errors exist about the operands. I also am curious, when I print out "Your bill is..." , how do I allow the computer to calculate the bill for me?

Your error is because you are trying to compare an int to a string , which won't work, that comparison is not defined. Besides, your string is empty anyway.

Also, your if has an erroneous ; on the end of it. In fact, your whole if makes no sense and should be removed.

Try this instead:

#inslude <iostream>
using namespace std;

int main()
{
    int TDU, kWh;
    cout << "Who is your TDU?\n";
    cin >> TDU;
    cout << "How many kWh did you use for the month you would like to calculate?\n";
    cin >> kWh;
    cout << "Your bill is " << 3.42 + (0.0384470 * kWh);
    return 0;
}

UPDATE : That being said, you probably meant to do something more like this instead:

#inslude <iostream>
using namespace std;

static const int ONCOR = ...; // <-- for you to fill in as needed...

int main()
{
    int TDU, kWh;
    cout << "Who is your TDU?\n";
    cin >> TDU;
    cout << "How many kWh did you use for the month you would like to calculate?\n";
    cin >> kWh;
    if (TDU == ONCOR)
        cout << "Your bill is " << 3.42 + (0.0384470 * kWh);

    return 0;
}

Alternatively:

#inslude <iostream>
using namespace std;

int main()
{
    string TDU;
    int kWh;
    cout << "Who is your TDU?\n";
    cin >> TDU;
    cout << "How many kWh did you use for the month you would like to calculate?\n";
    cin >> kWh;
    if (TDU == "ONCOR")
        cout << "Your bill is " << 3.42 + (0.0384470 * kWh);

    return 0;
}

Depending on what format you want the user to enter the TDU as.

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