0

I tried to create a solution involing some of the new things learned in the weeks lecture such as arrays.

How can I improve on this and why is my do loop to obtain a positive float value for the sides: (sides <= 0) not working?(it accepts negative values)

//Check if a triangle is valid or not based on user input side lengths
#include <stdio.h>
#include <cs50.h>

bool valid_triangle(float a, float b, float c);

int main(void)
{
    // Get length of sides of triangle
    float sides[3];
    for (int i = 0; i < 3; i++)
    {
        do
        {
            sides[i] = get_float("Side length %i: ", i + 1);
        }
        while (sides <= 0);
    }

    bool triangle = (valid_triangle(sides[0], sides[1], sides[2]));

    // Check if Bool true or false
    if (triangle == true)
    {
        printf("Valid triangle\n");
    }

    else
    {
        printf("Not a valid triangle\n");
    }
}

bool valid_triangle( float a, float b, float c)
{
    // Check condition 
    if (a + b <= c || a + c <= b || b + c <= a) 
    {
        return false;
    }
    return true;
}


1 Answer 1

1

What I think you are doing wrong is, you are checking if an array is less than or equal to 0. What you should be doing is indexing into the array. Then it would be checking the float values.

while (sides[i] <= 0);

:)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .