Louisdemof
Louisdemof

Reputation: 1

Trying to create a function to see if a triangle is valid, getting an error message on my function

Can someone help me on this error, when I compile my code in C, I get the following error messages:

error: too few arguments to function call, at least argument 'format' must be specified

how can I add a format to my argument ? Which format should my argument have?

Below is my code:

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

bool valid_triangle(float a, float b, float c);
int main(void)
{
    printf("give me the first side of the triangle: \n");
        float a = get_float();
    printf("give me the second side of the triangle: \n");
        float b = get_float();
    printf("give me the third side of the triangle: \n");
        float c = get_float();
     bool i = valid_triangle ( a, b, c);

    if (i == true);
    {
        printf("triangle is true: \n");
    }
     if (i == false);
    {
        printf("triangle is false: \n");
    }
}
     bool valid_triangle(float a, float b, float c)
    {
        if (a<=0 || b<=0 || c<=0) // check for all positive sides
        {
            return false;
        }
        if ((a+b<=c || a+c<=b || b+c<=a)) // check that the sum of any two length is greater than the length of the third one
        {
            return false;
        }

    return true; //if both tests are negative, the result is true
    }

Upvotes: 0

Views: 1384

Answers (2)

mich-ish
mich-ish

Reputation: 48

It looks like you've called the function valid_triangle before you defined it.

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

Also, you have not defined the get_float function. You can use the following:

float a;
printf("give me the first side of the triangle: ");
scanf("%f", &a);
printf("First = %f\n",a); 

The program works only when the order is correct. Import the relevant modules and define any functions you need:

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

bool valid_triangle(float a, float b, float c)
{
    if (a<=0 || b<=0 || c<=0) // check for all positive sides
    {
        return false;
    }
    else if ((a+b<=c || a+c<=b || b+c<=a)) // check that the sum of any two length is greater than the length of the third one
    {
        return false;
    }
    else
    {
        return true;
    }
}

Then call the main function:

int main(void)
{
    float a;
    printf("give me the first side of the triangle: ");
    scanf("%f", &a);
    printf("First = %f\n",a);

    float b;
    printf("give me the second side of the triangle: ");
    scanf("%f", &b);
    printf("Second = %f\n",b);


    float c;
    printf("give me the third side of the triangle: ");
    scanf("%f", &c);
    printf("Third = %f\n",c);

    bool validity = valid_triangle(a, b, c);
    {
        if (validity == true)
        {
            printf("Valid triangle\n");
        }
        else
        {
            printf("Invalid triangle\n");
        }
    }
return 0;
}

Upvotes: 0

john
john

Reputation: 87959

bool i = valid_triangle ( a, b, c);
if (i == true);
{
    printf("triangle is true: \n");
}
if (i == false);
{
    printf("triangle is false: \n");
}

Many problems here. Firstly you have semi-colons after your if conditions, this ends the if statement earlier than you intended and so the printf statements are not part of your if statements.

Secondly these two are alternatives so you should write one if ... else ... statement not two if ... statements.

Thirdly you have the very common newbie misunderstanding that bool variables need to be compared with true (or false) but it's not so. Boolean variables are true or false by themselves, they don't need to be compared with true or false. It's not an error to do so, it works, but it shows a lack of understanding.

Fourthly i is a really terrible name for this variable, how about valid?

Putting all that together and we get

bool valid = valid_triangle(a, b, c);
if (valid)
{
    printf("triangle is valid: \n");
}
else
{
    printf("triangle is invalid: \n");
}

And to be honest I probably wouldn't even use a variable here. This code is also correct

if (valid_triangle(a, b, c))
{
    printf("triangle is valid: \n");
}
else
{
    printf("triangle is invalid: \n");
}

None of this is the problem you actually complained about (which appears not to be reproducible), but hopefully it's helpful.

Upvotes: 0

Related Questions