0

Why does the error do not come out even though I input 0 or 5 and above? Someone? Can you explain how to use &&?

#include <iostream> using namespace std; int main() { int x, operation, y; cout<< "Enter a Number"<< endl ; cin>> x; cin>> operation; cin>> y; if (operation<=0 && operation>=5) { cout<< "error" ; } if ( operation==1) { cout<< x+y ; } if ( operation==2) { cout<< x-y ; } if ( operation==3) { cout<< x*y ; } if ( operation==4) { cout<< x/y ; } return 0; }

21st Mar 2022, 12:29 PM
Sterben
Sterben - avatar
4 odpowiedzi
+ 5
&& means AND. A number cannot be <= 0 and >= 5 at the same time
21st Mar 2022, 12:39 PM
Lisa
Lisa - avatar
+ 5
You can use logical or operator instead.
21st Mar 2022, 12:40 PM
Simba
Simba - avatar
+ 2
I think at the line if(operation <=0 && operation>=5).. Wont work because a can not be less than or equal to 0 and also be greater than or equal to 5 So i think a work around could be using “OR” instead of “AND” So changing && —> ||
21st Mar 2022, 1:28 PM
Faisal Issaka
Faisal Issaka - avatar