注册 登录
编程论坛 C++教室

新手!请各位大神看看错在哪?减和乘除无法运算

DDQ13 发布于 2018-10-12 22:23, 1865 次点击
#include<iostream>
using namespace std;
int main()
{
    int a,b,c;
    char i;
    cin>>a>>b>>i;
    if(i='+')
    {
        c=a+b;
        cout<<c<<endl;}
    if(i='-')
    {
    c=a-b;
    cout<<c<<endl;
    }
    else if(i='*')
    {  
        c=a*b;
        cout<<c<<endl;   
    }
    else if(i='/')
    {
        if(b==0)
        cout<<"Divided by zero!"<<endl;
        else if(b!=0)
        c=a/b;
        cout<<c<<endl;
    }
    else
    cout<<"Invalid operator!"<<endl;
    return 0;   
   
}
4 回复
#2
DDQ132018-10-12 22:32
emmm 才开始学,搞不懂。

#3
rohalloway2018-10-13 00:24
if(i='+') 这样是赋值语句了, +的ascii码是43,所以结果肯定为true
正确的写法是if(i=='+')


程序代码:

#include<iostream>
using namespace std;
int main()
{
    int a, b, c;
    char i;
    cin >> a >> b >> i;
    if (i == '+')
    {
        c = a + b;
        cout << c << endl;
    }
    else if (i == '-')
    {
        c = a - b;
        cout << c << endl;
    }
    else if (i == '*')
    {
        c = a*b;
        cout << c << endl;
    }
    else if (i == '/')
    {
        if (b == 0)
            cout << "Divided by zero!" << endl;
        else if (b != 0)
            c = a / b;
        cout << c << endl;
    }
    else
        cout << "Invalid operator!" << endl;
    return 0;
}
#4
DDQ132018-10-13 13:00
回复 3楼 rohalloway
谢谢了|ω・)
#5
d15765254812018-10-16 20:13
也没赋值怎么出来
1