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

Right-hand operand error on string when using 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;

您的问题的解决方案可以在@来源中找到:error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)

你需要#include <string>