I do not jump the error when I enter a negative error

1
if ((_base||_altura) <= 0){
   throw string {"ERROR: la base y la altura tienen que ser mayor que 0 "};
}
else {
   base = _base;
   altura = _altura;
}

This is the code that I have done and when I put a negative value I do not jump error but if when I put 0

    
asked by CarlosM09 28.09.2018 в 13:01
source

1 answer

5

The problem is how you are putting the parentheses in the if :

if ((_base||_altura) <= 0){

As it is right now, you are first processing (_base||_altura) and the result you are comparing it with <= 0 . So the problem is that if either of the two numbers is different from zero, the OR will return 1 which is a value greater than or equal to zero and will enter the if .

The solution would be to compare each of the values with zero individually:

if (_base <= 0 || _altura <= 0){
   throw string {"ERROR: la base y la altura tienen que ser mayor que 0 "};
}
else {
   base = _base;
   altura = _altura;
}
    
answered by 28.09.2018 в 15:06