-1

I'm new to programming, and i'm doing CS50. I got stuck with my proactivity on the triangle problem (week2 - arrays - functions).

here's my code:

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

bool valid_triangle(float x, float y, float z);

int main(void)
{
    float x = get_float("Side 1: ");
    float y = get_float("Side 2: ");
    float z = get_float("Side 3: ");

    if (x <= 0 || y <=0 || z <=0)
    {
        return false;
    }

    if (valid_triangle == true)
    {
        printf("This is a valid triangle");
    }
    else
    {
        printf("This is not a valid triangle");
    }
}


bool valid_triangle(float x, float y, float z)
{
    if ((x + y <= z) || (x + z <= y) || (y + z <= x))
    {
        return false;
    }
    else
    {
        return true;
    }
}

i'm getting the error:

comparison between pointer and integer ('bool (*)(float, float, float)' and 'int') [-Werror,-Wpointer-integer-compare]
    if (valid_triangle == true)
        ~~~~~~~~~~~~~~ ^  ~~~~

can anyone helpme about what i'm doing wrong?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Piazzones
  • 19
  • 4

1 Answers1

1

thanks @Mark Benningfield

it was really only missing to call the function within main...

This way, i just added on main

bool isValid = valid_triangle(x, y, z);

to call the function, and changed

    if (isValid == true)
    {
        printf("This is a valid triangle\n");
    }
    else
    {
        printf("This is not a valid triangle\n");
    }
Piazzones
  • 19
  • 4
  • 2
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Sep 24 '21 at 21:05
  • 1
    regarding: `if (isValid == true)` any non-zero value evaulates to `TRUE` also only zero evaluates to `FALSE` Therefore, 1) TRUE must be defined as 1, which is not always the case (it might be defined as `!0` or any other non 0 value. Therefore best to not assume that TRUE is 1. therefore, best to not compare a boolean value to TRUE/ Suggest using: `!FALSE` – user3629249 Sep 25 '21 at 20:13
  • 1
    @user3629249 In C, `!FALSE` is `!0` is `1` always. `isValid` is set by `bool valid_triangle()`, so, in this case, it's either true or false. However, I agree in general that adding a redundant `== true` is a bad coding convention because it evaluates to false if a value is truthy but not exactly 1, which is a very hard thing to explain and debug. – Neil Sep 25 '21 at 20:39