Reputation:
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
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
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
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