0

有人可以帮我解决这个错误吗,当我在 C 中编译我的代码时,我收到以下错误消息:

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

如何为我的论点添加格式?我的论点应该采用哪种格式?

下面是我的代码:

#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
    }
4

2 回答 2

0

看起来您在定义它之前已经调用了函数 valid_triangle。

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

此外,您还没有定义 get_float 函数。您可以使用以下内容:

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

该程序仅在顺序正确时才有效。导入相关模块并定义您需要的任何功能:

#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;
    }
}

然后调用主函数:

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;
}
于 2020-04-21T09:27:11.610 回答
0
bool i = valid_triangle ( a, b, c);
if (i == true);
{
    printf("triangle is true: \n");
}
if (i == false);
{
    printf("triangle is false: \n");
}

这里有很多问题。首先,您的 if 条件后面有分号,这会比您预期的更早结束 if 语句,因此 printf 语句不是您的 if 语句的一部分。

其次,这两个是替代方案,所以你应该写一个if ... else ...语句而不是两个if ...语句。

第三,您有一个非常常见的新手误解,即bool变量需要与true(或false)进行比较,但事实并非如此。布尔变量本身就是真假,不需要与真假比较。这样做不是错误,它有效,但它表明缺乏理解。

第四i,这个变量的名字真的很糟糕,怎么样valid

把所有这些放在一起,我们得到

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

老实说,我可能甚至不会在这里使用变量。这段代码也是正确的

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

这些都不是您实际抱怨的问题(这似乎不可重现),但希望它会有所帮助。

于 2020-04-21T07:48:19.780 回答