该程序是N号学生的数据日志。它会计算谁拥有最高分,谁没有通过考试,谁有点年纪(他们至少失败了一次)。
下面有一些我无法识别的错误,作为结构的初学者。
我收到某些if语句和base [N]
的错误编译器:默认编译器
E:\Desktop\4o set\ask5.c: In function `main':
E:\Desktop\4o set\ask5.c:35: error: invalid operands to binary ||
E:\Desktop\4o set\ask5.c:35: error: syntax error before "grade"
E:\Desktop\4o set\ask5.c:50: error: `base' undeclared (first use in this function)
E:\Desktop\4o set\ask5.c:50: error: (Each undeclared identifier is reported only once
E:\Desktop\4o set\ask5.c:50: error: for each function it appears in.)
E:\Desktop\4o set\ask5.c: At top level:
E:\Desktop\4o set\ask5.c:81: error: syntax error before "return"
Execution terminated
#include <stdio.h>
struct data{
char fname[30];
char lname[30];
float grade;
int trials;
int year;
int later_year;
};
main()
{
int i,N,minyear,mostrials;
float maxgrade;
//struct data *base;
printf("Amount of students: ");
scanf("%d",&N);
//struct data *base=(struct data *)malloc(N*sizeof(struct data));
{
struct data base[N];
for(i=0;i<N;i++){
printf("\n-----------------------------");
printf("\nStudent Data(remaining %d)\n",N-i);
printf("-----------------------------\n\n");
printf("\nName ");
fgets(base[i].fname,30,stdin);
printf("\nLast Name ");
fgets(base[i].lname,30,stdin);
Error:
printf("\nGrade ");
scanf(" %f",&(base[i].grade));
if(base[i].grade<=0 || base[i]grade>=10){
printf("\nGrade-> [0,10]\n");
goto Error;
}
printf("\nNumber of attempts to pass ");
scanf(" %d",&(base[i].trials));
Error2:
printf("\nYear ");
scanf(" %d",&(base[i].year));
if((base[i].year)<=0){
printf("\nYear greater than 0");
goto Error2;
}
}
maxgrade=base[0].grade;
minyear=base[0].year;
mostrials=base[0].trials;
for(i=0;i<N;i++){
if(maxgrade<base[i].grade)
maxgrade=base[i].grade;
if(minyear>base[i].year)
minyear=base[i].year;
if(mostrials<base[i].trials)
mostrials=base[i].trials;
}
printf("\n\n|Students with max grade (%f)|\n",maxgrade);
for(i=0;i<N;i++)
if(base[i].grade==maxgrade)
printf("\n%s %s",base[i].fname,base[i].lname);
printf("\n\n|Student who failed the most (%d fores)|\n",mostrials);
for(i=0;i<N;i++)
if(base[i].trials==mostrials)
printf("\n%s %s",base[i].fname,base[i].lname);
printf("\n\n|Older students|\n");
for(i=0;i<N;i++)
if(base[i].year>=minyear)
base[i].later_year=1;
for(i=0;i<N;i++)
if(base[i].later_year)
printf("\n%s %s year(%d)",base[i].fname,base[i].lname,base[i].year);
}
return 0;
}
答案 0 :(得分:1)
main应返回int
int main()
if(base[i].grade<=0 || base[i].grade>=10