编译失败

B2052 简单计算器

Cary1075 @ 2023-07-20 15:07:07

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

编译失败


by FengYuXinMing @ 2023-07-20 15:07:46

else没加分号


by sort_12470 @ 2023-07-20 15:10:18

cout<<"Invalid operator!" 这句话没加分号


by Cary1075 @ 2023-07-20 15:12:10

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

改完之后为什么还是不行?


by LittleAcbg @ 2023-07-20 15:12:46

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    char c;
    cin>>a>>b>>c;
    if(c=='+')
        cout<<a+b;
    else if(c=='-')
        cout<<a-b;
    else if(c=='*')
        cout<<a*b;
    else if(c=='/')
        if(b==0)
            cout<<"Divided by zero!"<<endl;
            return 0;
        else
            cout<<a/b;
    else
        cout<<"Invalid operator!";//你的代码里这个分号没有
    return 0;
}

by FengYuXinMing @ 2023-07-20 15:20:41

if语句不加大括号只能括一个语句,你看看你的那个if,里面有一个return 0,所以这个地方报错了,改一下去


by Cary1075 @ 2023-07-20 15:20:53

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

这样好像可以了


by Cary1075 @ 2023-07-20 15:21:46

谢谢


by FengYuXinMing @ 2023-07-20 15:23:09

@Cary1075 你家伙直接copy别人的代码是吧


by xiechengxuan1061862 @ 2023-10-02 14:57:03

输出语句没加分号


|