求解error: syntax error before "int"

Gaunny 2018-01-12 10:15:41
这个代码总是在红色行出现error: syntax error before "int",是怎么回事?
#include <stdio.h>
struct stu
{
char name[100];
int n;
int physics;
int math;
};
int main()
{
int i,t,sum,ave,max,total;//i循环变量,t代换求分段人数 sum分数求和 ave分数平均 max 最大值 total选课人数,temp四舍五入用
float temp;
int a[5]={0},b[5]={0};
struct stu student[10];
for(i=0;i<10;i++){
scanf("%s %d %d %d",student[i].name,&student[i].n,&student[i].physics,&student[i].math);
}
sum=0;
max=0;
total=0;
for(i=0;i<10;i++){
t=student[i].physics/10;
if(t>0){
sum += student[i].physics;
switch(t){
case 10:a[0]+=1;break;
case 9:a[0]+=1;break;
case 8:a[1]+=1;break;
case 7:a[2]+=1;break;
case 6:a[3]+=1;break;
default :a[4]+=1;break;
}
}
if(max<student[i].physics){
max=student[i].physics;
}
}
total=a[0]+a[1]+a[2]+a[3]+a[4];
temp=(float)sum/total;
ave=((int)(temp+0.5)==(int)temp) ? (int)temp:int(temp+0.5);
printf("physics:%d(%.2f);%d(%.2f);%d(%.2f);%d(%.2f);%d(%.2f);%d;\n",a[0],(float)a[0]/total,a[1],(float)a[1]/total,a[2],(float)a[2]/total,a[3],(float)a[3]/total,a[4],(float)a[4]/total,ave);
for(i=0;i<10;i++){
if(student[i].physics==max)
printf("physics:%s;%d;%d;\n",student[i].name,student[i].n,student[i].physics);
}
sum=0;
max=0;
total=0;
for(i=0;i<10;i++){
t=student[i].math/10;
if(t>0){
sum+=student[i].math;
switch(t){
case 10:b[0]+=1;break;
case 9:b[0]+=1;break;
case 8:b[1]+=1;break;
case 7:b[2]+=1;break;
case 6:b[3]+=1;break;
default :b[4]+=1;break;
}
}
if(max<student[i].math){
max=student[i].math;
}
}
total=b[0]+b[1]+b[2]+b[3]+b[4];
temp=(float)sum/total;
ave=((int)(temp+0.5)==(int)temp) ? (int)temp:int(temp+0.5);
printf("math:%d(%.2lf);%d(%.2lf);%d(%.2lf);%d(%.2lf);%d(%.2lf);%d;\n",b[0],(float)b[0]/total,b[1],(float)b[1]/total,b[2],(float)b[2]/total,b[3],(float)b[3]/total,b[4],(float)b[4]/total,ave);
for(i=0;i<10;i++){
if(student[i].math==max)
printf("math:%s;%d;%d;\n",student[i].name,student[i].n,student[i].math);
}
return 0;
}
...全文
1483 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzgoucx 2018-01-12
  • 打赏
  • 举报
回复
您好!您的问题在于下面这段代码: int i,t,sum,ave,max,total;//i循环变量,t代换求分段人数 sum分数求和 ave分数平均 max 最大值 total选课人数,temp四舍五入用 是在主函数main{}里面,而你的ave等是主函数之外,导致您的ave等·因不是全局变量,再强制转换时出错。建议:把ave改为全局变量,然后再把语句写为: (int)ave=((int)(temp+0.5)==(int)temp) ? (int)temp:(int)(temp+0.5);
Gaunny 2018-01-12
  • 打赏
  • 举报
回复
改了一下现在确实好了
引用 4 楼 cfjtaishan 的回复:
[quote=引用 1 楼 destory27 的回复:] 并没有问题啊
C++编译器没问题,但C语言的编译器就会出现error,主要是强制类型转换的格式不一样。 比如将int b强制类型转换为double类型,那么C语言必须:

int b = 5;
double a = (double)b;
C++是偏向面向对象的。可以用

a = double(b);
[/quote]
自信男孩 2018-01-12
  • 打赏
  • 举报
回复
引用 1 楼 destory27 的回复:
并没有问题啊
C++编译器没问题,但C语言的编译器就会出现error,主要是强制类型转换的格式不一样。 比如将int b强制类型转换为double类型,那么C语言必须:

int b = 5;
double a = (double)b;
C++是偏向面向对象的。可以用

a = double(b);
自信男孩 2018-01-12
  • 打赏
  • 举报
回复
ave=((int)(temp+0.5)==(int)temp) ? (int)temp:int(temp+0.5);
改成:
ave=((int)(temp+0.5)==(int)temp) ? (int)temp:(int)(temp+0.5);
注意将int用括号括起来。 然后再编译一下试试 个人建议下面的也可以改一下:
temp=(float)sum/total;
改成
temp= sum * 1.0 /total;
虽然没提error,但是这样改也是没问题的。
Gaunny 2018-01-12
  • 打赏
  • 举报
回复
很奇怪,运行不了,难不成是我编译系统的问题?
destory27 2018-01-12
  • 打赏
  • 举报
回复
并没有问题啊
zzgoucx 2018-01-12
  • 打赏
  • 举报
回复
1、建议:这句代码“int i,t,sum,ave,max,total;//i循环变量,t代换求分段人数 sum分数求和 ave分数平均 max 最大值 total选课人数,temp四舍五入用 ”, 是在主函数main{}里面,而你的ave等是主函数之外,导致您的ave等因不是全局变量,再强制转换时出错; 改为:把ave改为全局变量。 2、建议:temp=(float)sum/total(数据会溢出,因为total是int型,sum改为float型); 改为:temp=(float)sum/(float)total;以防数据溢出。 3、建议:ave=((int)(temp+0.5)==(int)temp) ? (int)temp:int(temp+0.5); 改为ave = (int)( ( (temp +0.5 )==temp ) ? temp:(temp+0.5) ),可以进一步减少数据溢出,保证不报错
自信男孩 2018-01-12
  • 打赏
  • 举报
回复
引用 6 楼 zzgoucx 的回复:
您好!您的问题在于下面这段代码: int i,t,sum,ave,max,total;//i循环变量,t代换求分段人数 sum分数求和 ave分数平均 max 最大值 total选课人数,temp四舍五入用 是在主函数main{}里面,而你的ave等是主函数之外,导致您的ave等·因不是全局变量,再强制转换时出错。建议:把ave改为全局变量,然后再把语句写为: (int)ave=((int)(temp+0.5)==(int)temp) ? (int)temp:(int)(temp+0.5);
还可以对等号左边的ave加(int)吗?第一看到。你的意思应该是把int放在等号右边吧。
ave=(int)((int)(temp+0.5)==(int)temp) ? (int)temp:(int)(temp+0.5);

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧