You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++简易计算器编译报错:std::istream与int无匹配operator<<

解决C++简易计算器的operator<<匹配错误

嘿,这个错误太常见啦!你看报错的那行cin<<anotherQ;,这里犯了个基础的流操作符搞混的问题:

  • cin是输入流,用来读取用户输入,对应的操作符是>>(提取运算符)
  • <<是输出流(比如cout)用的插入运算符,cin根本不支持这个操作,所以编译器才会抛出no match for ‘operator<<’的错误。

只要把那行代码改成cin>>anotherQ;就能解决这个核心问题啦!另外我把修正后的完整代码贴出来,你可以直接用:

using namespace std;
int main() {
    int running = 0;
    while(running != 1) {
        float num1;
        float num2;
        char op;
        int anotherQ;
        cout<<"Enter 1 for '+', 2 for '-', 3 for '*', 4 for '/': ";
        cin>>op;
        cout<<"\n";
        cout<<"Enter a number: ";
        cin>>num1;
        cout<<"\n";
        cout<<"Enter another number: ";
        cin>>num2;
        cout<<"\n";
        if (op=='1') {
            cout<<"The answer is "<<num1 + num2<<endl;
        } else if(op == '2') {
            cout<<"The answer is "<<num1 - num2<<endl;
        } else if(op=='3') {
            cout<<"The answer is "<<num1 * num2<<endl;
        } else if(op=='4') {
            cout<<"The answer is "<<num1 / num2<<endl;
        } else {
            cout<<"Wrong operator"<<endl;
            running = 1;
        }
        cout<<"Do you want to do another sum 0 for Yes and 1 for No "<<endl;
        // 这里把<<改成>>,修复输入操作符错误
        cin>>anotherQ;
        if (anotherQ == 0) {
            cout<<"Hmm.., Think u like maths well let's do another sum"<<endl;
        } else if (anotherQ == 1) {
            cout<<"Ok well byee!"<<endl;
            running = 1;
        } else {
            cout<<"crrrcrr u have given a wrong command byeeeeeeeeeeeeeeeee! I was not desinged for handling such complex commands as I was made by a 9 year old boy u will pay for it"<<endl;
            running = 1;
        }
    }
    return 0;
}

顺带提个小建议:你现在用char op来接收运算符选择,其实改成int op会更直观,这样判断的时候就不用和字符'1'比较,直接和整数1比较就行,能少点小坑~

内容的提问来源于stack exchange,提问作者Sreshthangsu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 00:12:29