samedi 16 mars 2019

Double negation in c++ using if?

I am doing a little excercise from my college's introduction problem set of creating a program where the user introduces four variables a,b,c,d and: if a is in (c;d) AND b isn't in (c;d) the program should print a, b and a+b otherwise it should print a, b and a-b

so this was my attempt:

#include<iostream>
using namespace std;

int main()
    {int a,b,c,d;
    cout<<"Introduza a: ";
    cin>>a;
    cout<<"Introduzca  b: ";
    cin>>b;
    cout<<"Introduzca c: ";
    cin>>c;
    cout<<"Introduzca d: ";
    cin>>d;
    if( (c<a<d) && ((b<c || d<b)))
        {cout<<"a= " <<a <<"\n";
        cout<<"b= " <<b << "\n";
        cout<<"a+b= " <<a+b <<"\n";
        }
    else
        {cout<<"a=" <<a <<"\n";
        cout<<"b= " <<b <<"\n";
        cout<<"a-b= " <<a-b <<"\n";}
        }       

I noticed that if the user introduces a,b such that BOTH statements are false it prints a+b instead of a-b. why does it happen?

Aucun commentaire:

Enregistrer un commentaire