Reputation: 11
The code below compiles, runs, and takes input, but it does not output true or false as intended. I'm not sure what I am doing wrong.
#include <stdio.h>
#include <cs50.h>
//int add_two_ints(int a, int b);
bool valid_triangle (float a, float b, float c);
int main (void)
{
float a = get_float("Enter Side 1:");
float b = get_float("Enter Side 2:");
float c = get_float("Enter Side 3:");
}
bool valid_triangle (float a, float b, float c)
{ //checks if two sides are greater than one
if ((a + b <= c) || (a + c <= b) || (b + c <= a))
{
return false;
}
//checks for positive sides
if (a <= 0 || b <= 0 || c <= 0 )
{
return false;
}
//if conditions are met print true
else
return true;
}
Upvotes: 0
Views: 118
Reputation: 479
Nothing in your code will run unless it is called in the main
function. main
is what runs when you start your code.
Your valid_triangle
function has only been defined, telling the rest of the code how it works and what kinds of values it accepts, but it will not run unless called. Add a call to your valid_triangle
function within main
, and it will execute the function based on its definition.
int main (void)
{
float a = get_float("Enter Side 1:");
float b = get_float("Enter Side 2:");
float c = get_float("Enter Side 3:");
bool isValid = valid_triangle(a, b, c);
}
By adding this final line into main, the program will run your function with the three triangle sides and collect the output in a boolean
value. How you use the value is up to you. Just make sure it is written in main
, or is called by main
.
EDIT: Your printf
function is using the %d
identifier, which means take the next argument (result
) and treat it like an integer (0 for false, 1 for true). C does not have a similar identifier for boolean values because they are typically used to control portions of the code, and not be the direct output.
There are many ways to print out true or false, but here is a more beginner-friendly version:
// Check if result is true
if (result)
{
printf("%s", "true");
}
else
{
printf("%s", "false");
}
Depending on the boolean value of result
, the code will print a different string to correspond to that value. To see the most condensed form that accomplishes this, see What is the printf format specifier for bool?.
Upvotes: 1
Reputation: 24
I think from what i see in your code that you didn't call the function " valid_triangle " to consulte the output of it ..
Upvotes: 1