user13611213
user13611213

Reputation:

How to get my function to print true or false

How can I get my program to print true or false like my function calls for? After entering the three sides nothing happens. Thanks.

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

//declare functions
bool valid_triangle(float x, float y, float z);


int main(void)
{
    float x = get_float("Side 1 length:  ");
    float y = get_float("Side 2 length:  ");
    float z = get_float("side 3 length:  ");
    
    bool valid_triangle(float x, float y, float z);
}


bool valid_triangle(float x, float y, float z)
{
    if (x + y > z && y + z > x && x + z > y)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Upvotes: 0

Views: 393

Answers (4)

Itati
Itati

Reputation: 193

printf(valid_triangle(x, y,z)?"true":"false");

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182829

You never call the function. You also have no code to print "true" or "false". You probably want something like:

if (valid_triangle (x, y, z))
    puts("true\n");
else
    puts("false\n");

Upvotes: 1

Amal K
Amal K

Reputation: 4919

You have never called your function. The syntax that appears in your main function to call the function actually is the declaration syntax. A function call statement should not include type information. You should call your function as follows and then decide to print true or false based on your function:

int main(void)
{
    float x = get_float("Side 1 length:  ");
    float y = get_float("Side 2 length:  ");
    float z = get_float("side 3 length:  ");
    
   if(valid_triangle(x, y, z))
          printf("True");
   else
          printf("False");
}

Your validation function can be written more tersely as follows:


bool valid_triangle(float x, float y, float z)
{
    return (x + y > z && y + z > x && x + z > y);
}

This is because the validation expression evaluates to a boolean expression already. So you don't need the if-else.

Upvotes: 1

vincent
vincent

Reputation: 81

int main(void)
{
    float x = get_float("Side 1 length:  ");
    float y = get_float("Side 2 length:  ");
    float z = get_float("side 3 length:  ");
    
    valid_triangle(x, y, z);
}

Upvotes: 0

Related Questions