使用cin时字符串上的右侧操作数错误

时间:2015-09-29 00:57:12

标签: c++ if-statement switch-statement calculator cin

我正在尝试制作一个使用do-while循环的基本计算器,并提示用户回答用户是否希望从头开始重新运行计算器。

我在cin遇到yesno的字符串文字答案时遇到以下错误:

<Error C2679   binary '>>': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)>

我该如何解决这个问题?

int main()
{
    double x;
    double z;
    char o;
    string a;
    char Y, y;
    do
    {
        cout << "Please input a value for x: " << endl;
        cin >> x;
        cout << "Please input a value for z: " << endl;
        cin >> z;
        cout << "Please pick an operation to do: * / + -" << endl;
        cin >> o;

        switch (o) {
            case '+':
                cout << x << " + " << z << " = " << x + z << endl;
                break;
            case '-':
                cout << x << " - " << z << " = " << x - z << endl;
                break;
            case '*':
                cout << x << " * " << z << " = " << x*z << endl;
                break;
            case '/':
                if (z != 0)
                {
                    x / z;
                    cout << x << " / " << z << " = " << x / z << endl;
                }
                else
                {
                    cout << "Can not divide by zero! Nice try, Pedersen!" << endl;
                }
                break;
            default:
                cout << "/n/n/tThank you for using my calculator!" << endl << endl;
        }
        system("cls");
        cout << "/n/n/tDid you want to run the calculator again?" << endl << endl;
        cin >> a;
    }while (a == "Yes" || a == "yes");
    system("pause");
    return 0;

1 个答案:

答案 0 :(得分:0)