首页 > 解决方案 > 如何解决:没有运算符匹配这些操作数

问题描述

图片

我不明白为什么操作数存在这些错误。我也很好奇,当我打印出来的时候"Your bill is...",怎么让电脑帮我计算账单呢?

标签: c++error-handlingoperands

解决方案


您的错误是因为您试图将 aint与 a进行比较string,这是行不通的,该比较未定义。此外,string无论如何,你的都是空的。

另外,您的结尾if有错误;。事实上,你的整体if毫无意义,应该被删除。

试试这个:

#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;
}

更新:话虽这么说,你可能打算做更多这样的事情:

#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;
}

或者:

#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;
}

取决于您希望用户输入的格式TDU


推荐阅读