我有一个程序,它在一个结构数组中接收有关 5 个学生的信息,然后使用该信息进行计算以确定学费。现在,我遇到了很多错误,我不知道如何修复。 例如,它说
#include <stdio.h>
#include <stdlib.h>
#define UnitCost 100
#define MAX 60
int calculator(int x, char y);
void getInput(struct student* memb);
void printOutput(struct student * memb);
typedef struct student{
char name[MAX];
char housing;
int total;
int units;
} student;
int main()
{
struct student user[5];
int total[5] = {0,0,0,0,0};
int loopControl;
int i;
char name[5][MAX];
int units;
char housing;
for (loopControl = 0; loopControl <= 4; loopControl++){
getInput(&user);
total[loopControl] = calculator(units, housing);
}
for(i = 0; i < 5; i++){
printf("\nStudent Name: %s", user[i].name);
printf("\nAmount due: $%d\n", user[i].total);
}
printf("\nAverage: %d\n", (total[0] + total[1] + total[2] + total[3] + total[4])/5);
return 0;
}
// -----------------------------------------------------------------------------
int calculator(int x, char y){
int onCampusCost = 0;
int unitCostTotal = 0;
int unitsEnrolledDiscount = 0;
if (x > 12){
unitsEnrolledDiscount = (x - 12) * 10;
}
if (y == 'n'){
onCampusCost = 0;
}
else if (y == 'y'){
(onCampusCost = 1000);
}
if (x >12){
unitCostTotal = (x * 100) - ((x - 12) * 10);
}
else{
unitCostTotal = x * 100;
}
return onCampusCost + unitCostTotal;
}
void printOutput(struct student * memb){
}
void getInput(struct student* memb){
char name[5][MAX];
char house;
int uns;
printf("Enter student name: ");
if (iter > 0) getchar();
gets(name);
memb->name = name;
printf("Do you live on campus?\ny/n: ");
scanf("%c", house);
memb->housing = house;
printf("How many units are you enrolled in?: ");
scanf("%d", uns);
memb->units = uns;
}
错误:
||=== Build: Debug in f (compiler: GNU GCC Compiler) ===| warning: 'struct student' declared inside parameter list| warning: 'struct student' declared inside parameter list|
|In function 'main':|
warning: passing argument 1 of 'getInput' from incompatible pointer type|
note: expected 'struct student ' but argument is of type 'struct student ()[5]'|
warning: unused variable 'name' [-Wunused-variable]|
In function 'calculator':| warning: variable 'unitsEnrolledDiscount' set but not used [-Wunused-but-set-variable]|
error: conflicting types for 'printOutput'|
note: previous declaration of 'printOutput' was here|
error: conflicting types for 'getInput'|
note: previous declaration of 'getInput' was here|
In function 'getInput':|
error: 'iter' undeclared (first use in this function)|
note: each undeclared identifier is reported only once for each function it appears in|
warning: passing argument 1 of 'gets' from incompatible pointer type|
note: expected 'char ' but argument is of type 'char ()[60]'|
error: assignment to expression with array type|
warning: format '%c' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]|
warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]| ||=== Build failed: 4 error(s), 9 warning(s) (0 minute(s), 0 second(s)) ===|
请大家帮忙
最佳答案
@Lumii 问题出在您的代码中:-
- 您已经定义了函数声明,其中在结构定义之前将 (struct Student *) 作为参数传递。因为结构是用户定义的数据类型,这就是编译器给你错误的原因,因为编译器没有得到结构的定义。因此,要么将所有函数声明写在结构定义之后,要么将结构名称写在函数声明之前。喜欢:-
结构学生;
int 计算器(int x, char y);
void getInput(struct Student* memb);
void printOutput(struct Student * memb);
关于函数类型冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40944485/