Home > Software design >  valid triangle Harvard cs50 practice problem
valid triangle Harvard cs50 practice problem

Time:11-05

I am trying to create a program with a function that takes in three side lengths of a triangle and outputs if they create a valid triangle or not.

I keep getting this error:

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

This is my code:

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

bool valid_triangle(int length_a, int length_b, int length_c);

int main(void)
{

    int length_a = get_int("side a: ");
    int legnth_b = get_int("side b: ");
    int legnth_c = get_int("side c: ");

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


bool valid_triangle(int length_a, int length_b, int length_c)
    {
        if(length_a <= 0 || length_b <= 0 || length_c <= 0)
        {
            return false;
        }
        if(length_a   length_b < length_c || length_a   length_c < length_b || length_b   length_c < length_a)
        {
            return false;
        }
        return true;
}

CodePudding user response:

valid_triangle is a function so when you call it you need to pass in the parameters. You also don't need to check for true and false since it must be one or the other. Try this:

if(valid_triangle(length_a,length_b,length_c) == true)
{
    printf("This is a triangle\n");
}
else
{
    printf("This is not a triangle\n");
}
  • Related