0

problem: I'm making a triangle. side lengths will be provided by user. thats it.
only rules : any 2 side lengths of the triangle must be greater than 3rd side length. no sides can be <= 0.
if these conditions are met the code prints TRUE. if not, prints FALSE. I wrote this code. I can't return the value where it should be. I can't use the bools correctly and can't return a value.

error message: triangle.c:11: error: comparison between pointer and integer ('bool ()(float, float, float)' and 'int') [-Werror] if (valid_triangle == true) // triangle.c:13: error: format specifies type 'int' but the argument has type 'bool ()(float, float, float)' [-Werror,-Wformat] printf("triangle side lengths are %i", valid_triangle);

GitHub link of the code (triangle.c)

1 #include <cs50.h>
2 #include <stdio.h>

3 bool valid_triangle(float a, float b, float c);

4 int main(void)
5{
6     printf("give me the sides of triangle: ");
7     float a = get_float();
8     float b = get_float();
9     float c = get_float();
10    bool i = valid_triangle(a, b, c);

11    if (valid_triangle = true)
12        {
13            printf("triangle side lengths are %i", valid_triangle);
14        }                      // I tried %b, %s etc. didnt work.

15    if (valid_triangle = false)
16        {
17            printf("triangle side lengths are %i", valid_triangle);
18        }                          // I tried %b, %s etc. didnt work.

19    }

20 bool valid_triangle(float a, float b, float c)

21 {
22 if (a <= 0 || b <= 0 || c <=0)                // check for all positive sides
23    {
24        return false;
25    }

26 if (a + b <= c) || (a + c <= b) || (c + b <= a)  // check that sum of any two     sides are greater than third side.
27   {
28        return false;
29    }

30    return true;         // if we passed both tests, result is true.
31 }
2

I only see one issue in two places:

if (valid_triangle = true)

This is an assignment statement, which is always true, because it used the "=" operator. If you want to do a comparison test, it must use the "==" operator.

[EDIT: following comment] (I really need to stop answering questions in the middle of the night.)

There is another problem in the same code. "valid_triangle" isn't a var, it's a function name. Since the return value was stored in i, then i should be compared to TRUE or FALSE.

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

1
  • I made the changes. now these errors appeared. triangle.c:11: error: comparison between pointer and integer ('bool ()(float, float, float)' and 'int') [-Werror] if (valid_triangle == true) // triangle.c:13: error: format specifies type 'int' but the argument has type 'bool ()(float, float, float)' [-Werror,-Wformat] printf("triangle side lengths are %i", valid_triangle); – fastest_noob Jul 29 '17 at 8:28
0

You have some problems. The code which works is below.

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

bool valid_triangle(float a, float b, float c);
int main(void)
{
     printf("give me the first sides of triangle:\n ");
     float q = get_float();
        printf("give me the second sides of triangle:\n ");
     float w = get_float();
        printf("give me the third sides of triangle:\n ");
     float e = get_float();
    bool i = valid_triangle(q, w, e);

    if (i == true)
        {
            printf("triangle is true\n");
        }                      // I tried %b, %s etc. didnt work.

    if (i == false)
        {
            printf("triangle is false\n ");
        }                          // I tried %b, %s etc. didnt work.

    }

 bool valid_triangle(float a, float b, float c)

 {
 if (a <= 0 || b <= 0 || c <=0)                // check for all positive sides
    {
        return false;
    }

 if ((a + b <= c) || (a + c <= b) || (c + b <= a))  // check that sum of any two     sides are greater than third side.
   {
        return false;
    }

    return true;         // if we passed both tests, result is true.
 }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .