C语言高手进,在线等

来源:百度知道 编辑:UC知道 时间:2024/05/28 15:02:31
要求是输入学号、姓名、成绩、求出平均成绩并保存,下面是我写的,不知道哪错了,帮忙修改一下。
# include <stdio.h>
struct student_type
{
int num;
char name[10];
int score1;
int score2;
int score3;
int average;
}student[5];
void save()
{FILE *fp;
int i;
if ((fp=fopen("stud","wb"))==NULL)
{printf("cannot open file\n");
return;
}
for(i=0;i<5;i++)
if(fwrite(&student[i],sizeof(struct student_type),1,fp)!=1)
printf("file write error\n");
fclose(fp)
}
void main()
int i;
for(i=0;i<5;i++)
scanf("%d,%s,%d,%d,%d,%d",&student[i].num,student[i].name,&student[i].score1,&student[i].score2,&student[i].score3,&student[i].average);
student[i].average=(student[i].score1+student[i].score2+student[i].score3)/3;
save()
}
那个是手误,我是平均成绩出来是乱的数字 ,照三楼说的做过了,还是乱的。

主要像楼上所说,少了左花括号和分号。

但至于你说的平均成绩是乱数字,那是因为语句:

student[i].average=(student[i].score1+student[i].score2+student[i].score3)/3;

不包括在循环中,你那样写最后一个的平均成绩应该是对的
如果想计算所有人的成绩,应该将语句包括在循环中,也就是这样写:
for(i=0;i<5;i++) {
scanf("%d,%s,%d,%d,%d,%d",&student[i].num,student[i].name,&student[i].score1,&student[i].score2,&student[i].score3,&student[i].average);
student[i].average=(student[i].score1+student[i].score2+student[i].score3)/3;
}

void main() 后缺{

save() 调用的时候没;

void main() 后缺{
fclse(fp)没;
save()没;