# This code of triangle problem always give me "true".I can,t find bug in this please find the bug

``````    #include <cs50.h>
#include <stdio.h>

bool triangle (float a, float b, float c); //Declaration of function

int main (void)

{

printf ("Give me three sides of triangle!\n");

float x = GetFloat ();
float y = GetFloat ();
float z = GetFloat ();
bool tf= triangle (x,y,z); // Calling function
if (tf==true)
{
printf ("true\n");
}
if (tf==false)
{
printf ("False\n");
}

}
bool triangle (float a,float b, float c) // Defining function
{
if ((a<1) || (b<1) || (c<1)) //Condition
return false;

else if ((a+b<=c)&&(a+c<=b)&&(b+c<=a)) // Second condition
return false;

else
{
return true;// if both conditions are false then return true
}
}
``````

This is a case where the code is doing exactly what is written, but maybe not what you want. Can you specify what it is supposed to be doing? I don't remember where this comes from in the course, (can you please state the source?) but the code appears to be doing what it is designed to do. If you test with any three numbers the following happens:

If any of the three is less than 1, then it returns false. If all three are greater than or equal to 1, it will continue to the second test.

As for the second condition, if the first condition is not met (false is not returned), neither is the second. You need to look at it from a mathematical point of view. Given any three numbers greater than or equal to one, there will always be one condition, maybe two, maybe all three, where the sum of two of the numbers must be greater than the third.

Don't believe me? Pick three numbers >=1. Now pick the smallest of them. The other two are larger, therefore the sum of the other two is also larger. Even if they are all the same, any two will add together to be larger than the third.

The only numbers (as far as I can tell) that will produce a true from the second condition would be 0, 0, 0. But then, they're all < 1, so the first condition would return false.

Perhaps there is a different condition that you should be looking at? As I said, I don't remember this problem. Can you state where it is found?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance.

The problem was found in the code. First it was how to take bool value which is answered by metthew knowler that in c we can,t take bool value rather code can be written as if (tf==true); //do following; else //do following. Second problem was here else if (a+b<=c)&&(a+c<=b)&&(b+c<=a); this should be written as else if (a+b

Unfortunately, you can't simply print a bool variable in C. You'll have to check the value with an if statement and have a printf statement for the appropriate value.

``````if (tf == true) {
printf("True!");
} else {
printf("False!");
}
``````
• You are right but by doing this this program always give me true, there might be bug which i can,t find. Jul 31 '16 at 4:37
• The first if statement in the triangle function is kind of wrong. You need to check if the numbers are less than or equal to zero, not one because you can definitely have a triangle with sides of length between 0 and 1 (but not equal to or less than zero). if (a <= 0 || b <= 0 || c <= 0) Jul 31 '16 at 23:11