【发布时间】:2017-04-18 02:08:01
【问题描述】:
我有一个程序,它在一个结构数组中接收有关 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;
}
错误:
||=== 构建:在 f 中调试(编译器:GNU GCC 编译器)===|警告: 'struct student' 在参数列表中声明|警告:'结构 在参数列表中声明的学生|
|在函数'main'中:|
警告:从不兼容的指针传递“getInput”的参数 1 类型|
注意:预期为 'struct student ' 但参数的类型为 'struct 学生 ()[5]'|
警告:未使用的变量“名称”[-Wunused-variable]|
在函数“计算器”中:|警告:变量“unitsEnrolledDiscount” 设置但未使用 [-Wunused-but-set-variable]|
错误:'printOutput' 的类型冲突|
注意:'printOutput' 的先前声明在这里|
错误:“getInput”的类型冲突|
注意:'getInput' 的先前声明在这里|
在函数'getInput'中:|
error: 'iter' undeclared (第一次在这个函数中使用)|
注意:每个未声明的标识符仅报告一次 它出现的函数|
警告:从不兼容的指针类型传递“gets”的参数 1|
注意:预期为 'char ' 但参数类型为 'char ()[60]'|
错误:赋值给数组类型的表达式|
警告:格式 '%c' 需要 'char *' 类型的参数,但参数 2 具有类型“int”[-Wformat=]|
警告:格式“%d”需要“int *”类型的参数,但参数 2 有类型'int' [-Wformat=]| ||=== 构建失败:4 个错误,9 个 警告(0 分钟,0 秒)===|
请大家帮忙
【问题讨论】:
-
花时间真正地 阅读错误/警告日志。比如
getInput(&user);很明显你传入了错误的类型,把&user改成user[loopControl] -
@artm:
&user[loopControl] -
在 C 中,您阅读了第一个警告/错误,修复它,然后重试。在您的情况下,第一个警告说您在参数列表中声明了
struct student。果然,struct student第一次出现在你的代码中是在getInput的函数原型中。因此,将函数原型移至结构定义下方,然后重试。 -
@KeineLust 你是对的。Lumii - 将其更改为 Keinlust 建议的内容,我刚刚浏览了你的代码,但重点仍然是检查日志。
-
非常感谢你们。很明显我是编程新手,所以我仍然很难解释日志。