首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

书下的例子,麻烦修改上

2012-09-03 
书上的例子,麻烦修改下#includestdio.hstruct student{ char *nameint numfloat score}main(){float

书上的例子,麻烦修改下
#include<stdio.h>
struct student
{ char *name;
  int num;
  float score;
};
main()
{float ave(struct student *per);
 struct student stu[5],*ps;
 ps=stu;
 int i; 
 float aver,s=0;
 for(i=0;i<5;i++)
 {printf("enter date of students\n");
  scanf("%s%d%f",stu[i].name,&stu[i].num,&stu[i].score);
 }
 s=ave(ps);
 aver=s/5;
 printf("average=%f\n",aver);
}
float ave(struct student *per)
{
int i;
float sum=0;
for(i=0;i<5;i++,per++)
sum+=per->score;
return sum;
}

--------------------Configuration: helloc - Win32 Debug--------------------
Compiling...
hello.c
D:\VC\helloc\hello.c(11) : error C2143: syntax error : missing ';' before 'type'
D:\VC\helloc\hello.c(12) : error C2143: syntax error : missing ';' before 'type'
D:\VC\helloc\hello.c(13) : error C2065: 'i' : undeclared identifier
D:\VC\helloc\hello.c(17) : error C2065: 's' : undeclared identifier
D:\VC\helloc\hello.c(17) : warning C4244: '=' : conversion from 'float ' to 'int ', possible loss of data
D:\VC\helloc\hello.c(18) : error C2065: 'aver' : undeclared identifier
执行 cl.exe 时出错.

hello.obj - 1 error(s), 0 warning(s)




[解决办法]
编译器问题,将
 int i;
 float aver,s=0;
放在最前面。
float ave(struct student *per);放在main上面
[解决办法]
在c代码块中,变量的定义要在代码块的开头(如i,s,aver都要写在代码块的开头),不能像c++一样只要在变量使用前定义就可以了.上面还有错误就是输入的分号不是英文半角的,代码都要使用英文半角的格式输入

热点排行