vendredi 28 décembre 2018

c++ if statement malfunction

why are my else if loops not working the code when ran skips over the else statement and executes the if statement even if it is not true

   #include<iostream>
    using namespace std;
    int a;
    int b;
    char operation;
    int sum;
    int sub;
    int mul;
    int divide;
    int mod;
    int main()
    {
    cout<< "enter a number \n";
    cin >> a>>operation>>b;
    if (operation= '')
    {
        sum=a+b;
        cout << a<<operation<<b<<sum;
    }
    else if (operation= '-')
    {
        sub=a-b;
        cout << a<<operation<<b<<sub;
    }
    else if (operation= '/')
    { if(b==0){
        cout<<"You cannot divide by zero";
    }
        else{divide=a/b;
        cout << a<<operation<<b<<divide;}
    }else if (operation= '*')
    {
        mul=a+b;
        cout << a<<operation<<b<<mul;
    }
    else if (operation= '%')
    {
        mod=a%b;
        cout << a<<operation<<b<<mod;
    }
    else{
        cout<<"Invalid input";
    }

        return 0;
    }

the output is always just adding the numbers no matter what sign the input has it wont go over my else if statements even if the operation isnt + infact even if i use a wrong operation it just adds the two numbers

Aucun commentaire:

Enregistrer un commentaire