I want it to not display the result of the sum if the numbers are lower or equal to 1 or 1000. I don't know if using if
is the best way, but that's what I tried using, and I don't really understand why it doesn't work. I also tried writing conditions with ||
and &&
, but those don't work either.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int sum;
int a, b, c;
int main() {
cin >> a;
cin >> b;
cin >> c;
sum = a + b + c;
if ( 1 <= a, b, c <= 1000) { //also tried ( 1 <= a || b || c <= 100) and ( a, b, c >= 1 && a, b, c <= 1000)
cout<< sum;
}
else {
cout<< "can't calculate";
}
return 0;
}
The expression in this if statement
if ( 1 <= a, b, c <= 1000)
is an expression with the comma operator. It is equivalent to
if ( ( 1 <= a ), ( b ), ( c <= 1000 ) )
and the value of the expression is the value of its last operand. That is this if statement is equivalent to
if ( ( c <= 1000 ) )
It seems you mean
if ( 1 <= a && a <= 1000 && 1 <= b && b <= 1000 && 1 <= c && c <= 1000 )
{
std::cout << a + b + c << '\n';
}
Pay attention to that there is no sense to calculate the sum
sum = a + b + c;
before the checking the values of the variables a, b and c in the if statement.