gpt4 book ai didi

c - 错误: expected expression before ‘void’

转载 作者:行者123 更新时间:2023-11-30 20:10:40 25 4
gpt4 key购买 nike

我正在使用 Cloud 9 环境,这段代码困扰着我,我们得到它是为了一个作业,它显示以下编译错误,我被这些困住了

lab.c: In function ‘main’: lab.c:14:12: error: expected expression before ‘void’ getType(void);

^ lab.c:14:12: error: too many arguments to function ‘getType’ lab.c:4:7: note: declared here float getType(void);

^ lab.c: In function ‘getType’: lab.c:20:5: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘float (*)(int, int)’ [-Wformat=] printf("Total airfare is: %.2f",ticketPrice);

    //calculation cost aircost by given type

#include <stdio.h>
float getType(void);
float ticketPrice(int type,int noOfSeats);
int main(void)
{
int type,noOfSeats;

printf("Enter the type(1 or2): ");
scanf("%d", &type);
printf("Enter the no of seats: ");
scanf("%d", &noOfSeats);
getType(void);
ticketPrice(type,noOfSeats);
return 0;
}
float getType(void)
{
printf("Total airfare is: %.2f",ticketPrice);

}
float ticketPrice(int type,int noOfSeats)
{
float Tprice;

if (type=1){
printf("Economy Class\n");
type=90000;
printf("90,000\n");
Tprice=90000*noOfSeats;
printf("Total airfare is:%.2f ",Tprice);
return 0;
}
else if(type=2){
printf("Business class\n");
type=120000;
printf("120,000\n");
Tprice=120000*noOfSeats;
printf("Total airfare is :%.2f ",Tprice);
}
else if(type!=1||2){
printf("invalid type");
return 1;
}
}

最佳答案

当您在 C 中定义不带参数的函数时,参数应显式为 void。你已经做到了。错误是当您调用该函数时:

getType(void);

在这里,您必须省略void:

getType();

但是您的函数返回一个 float 并且您可能想要存储结果。你的函数实际上应该返回一个 float 。目前,它不返回任何内容,这会导致未定义的行为。

再次编译代码并打开警告。您的代码中还有许多其他错误。例如,if (type = 1)设置 type 为 1,然后输入代码块。您想要检查该值,为此您应该使用==

关于c - 错误: expected expression before ‘void’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43790144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com