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

Right-hand operand error on string when using cin

本文关键字:操作数 错误 字符串 cin 使用      更新时间:2023-10-16

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

对于yesno的字符串字面值答案,我在cin上遇到以下错误:

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

您的问题的解决方案可以找到@来源:错误C2678:二进制'>>'std::istream'(或者没有可接受的转换)

你需要#include <string>