Home > front end >  how to run Bool function if if statement is true
how to run Bool function if if statement is true

Time:11-17

bool isTriangle(double sideA, double sideB, double sideC){
if(sideA   sideB > sideC && sideA   sideC > sideB && sideB   sideC > sideA){
    return true;
}else{
    return false;
}
}

int main() {
double sideA, sideB, sideC;

cout << "Enter the lengths of the three sides of a triangle -- ";
cin >> sideA >> sideB >> sideC;

if (&isTriangle){
    if(sideA != sideB && sideB != sideC){
        cout << "This is a scalene";
    }
    isEquilateral(sideA, sideB, sideC);
    isIsosceles(sideA, sideB, sideC);
    isRight(sideA,sideB,sideC);
    
    cout << "Its perimeter is " << per << endl;
}
if(!&isTriangle){
    cout << "This is not a triangle" << endl;
}

}

How can I run code when an if statement inside the bool function is true.I have my bool function and it test the triangle sides and checks if it is a triangle, if it is, it returns true, if not it returns false. If it's true I want to run my other functions, if it's not a triangle I just want to display a message.

CodePudding user response:

This

if (&isTriangle){

should be this

if (isTriangle(sideA, sideB, sideC)){

When you call a function, you use the name of that function followed by parentheses () and you put the parameters that the function requires inside the parentheses separated by commas.

Some other suggestions.

If you want to test something and do one thing if it is true and another thing if it is false. Then the way to do this is with if ... else .... Like this

 if (isTriangle(sideA, sideB, sideC)) {
     ...
 }
 else {
     ...
 }

It's not so good to use two if ... and repeat the test, like you are doing

 if (isTriangle(sideA, sideB, sideC)) {
     ...
 }
 if (!isTriangle(sideA, sideB, sideC)) { // repeated test
     ...
 }

When you have code like this if (something) return true; else return false; you can skip the if statement and just say return something;. In other words there's no need to say 'if something is true then return true and if something is false return false' you can just say 'return something'. So this code

if(sideA   sideB > sideC && sideA   sideC > sideB && sideB   sideC > sideA){
    return true;
}else{
    return false;
}

can be rewritten more simply as

return sideA   sideB > sideC && sideA   sideC > sideB && sideB   sideC > sideA;
  •  Tags:  
  • c
  • Related