真搞不懂IDE为什么总是输出"Invalid operator!"

B2052 简单计算器

caojiaming @ 2022-10-09 14:57:49

#include <iostream>
#include <cstdio>
using namespace std;
char a;
bool Error=1;
int b,c;
int main()
{
    cin>>b>>c;
    scanf("%c",&a);
    if(a=='/')
    {
        if(c==0)
        {
            cout<<"Divided by zero!";
            return 0;
        }
        cout<<b/c;
    }
    switch(a)
    {
        case '+':cout<<b+c;Error=0;break;
        case '-':cout<<b-c;Error=0;break;
        case '*':cout<<b*c;Error=0;break;
        case '/':Error=0;break;
    }
    if(Error)
    {
        cout<<"Invalid operator!";
    }
    return 0;
}

IDE错了 洛谷评测也错了


by HPY_xiaxii @ 2022-10-09 15:02:11

洛谷炸了?

我的也这样(


by caojiaming @ 2022-10-09 15:22:53

我在DEVC++也出现了这种情况


by caojiaming @ 2022-10-09 15:30:11

cin读入a就没问题了


by 编码落寞 @ 2022-10-09 16:49:42

你读的是字符啊,你把操作符前面的空格读进来了。打印下a值,不就知道了。


by 123uuu @ 2022-10-22 17:35:15

代码是:


using namespace std;
int main () {
    int a,b,c;
    char f;
    cin>>a>>b>>f;
    if(f=='+'){
        cout<<a+b;
    }
    else if(f=='-'){
        cout<<a-b;
    }
    else if(f=='*'){
        cout<<a*b;
    }
    else if(f=='/'){
        if(b==0){
            cout<<"Divided by zero!";
        }
        else cout<<a/b;
    }
    else cout<<"Invalid operator!";
return 0; 
}

|