[求助]error C2059: syntax error : 'type'

龙井茶 2011-05-12 03:59:21
#include<stdio.h>
#include"string.h"

void inputdata();
void cacludata();
void statisdata();
void querydata();

char name[100][15];
float score[100][4];
int sum;
int main()
{



{
int choo;
printf("请输入实际学生人数:");
scanf("%d",&sum);
while(1)
{
printf("请选择下面的某一项任务:");
printf("\n(1)输入每个学生的姓名,平时成绩,期中成绩,期末成绩");
printf("\n(2)计算并输出每个学生的总评成绩。");
printf("\n(3)根据总评成绩对学生分类统计,输出各分数段的学生数。") ;
printf("\n(4)根据根据输入的学生姓名,查找并打印该学生的各项成绩") ;
printf("\n(5)结束程序运行");
printf("\n请输入你的选择(1或2或3或4或5):") ;
scanf("%d",&choo);
if(choo>=5)break;
switch(choo)
{
case 1 : inputdata(); break;
case 2 : cacludata(); break;
case 3 : statisdata(); break;
case 4 : querydata(); break;
}
}
printf("\n程序运行结束,再见。");
}
}
void inputdata()
{
int i;
for(i=0;i<sum;i++)
{
printf("\n请输学生的姓名:");
gets(name[i]);
printf("请输入学生平时,期中,期末成绩(例如98,95,96):");
scanf("%f,%f,%f",&score[i][0],&score[i][1],&score[i][2]) ;
}
}
void calculdata()
{
int i;
for(i=0;i<sum;i++)
{
score[i][3]=float(0.1*score[i][0]+0.3*score[i][1]+0.6*score[i][2]);
}
printf("\n每个学生总评成绩如下:\n");
for(i=0;i<sum;i++)
{
printf("%s:%f\n",name[i],score[i][3]);
}
}
void statisdata()
{
int i,d;
int grade[5]={0};
for(i=0;i<sum;i++)
{
d=(int)(score[i][3]/10);
switch(d)
{
case 10 : grade[4]=grade[4]+1; break;
case 9 : grade[3]=grade[3]+1; break;
case 8 : grade[2]=grade[2]+1; break;
case 7 : grade[1]=grade[1]+1; break;
case 6 : grade[0]=grade[0]+1; break;
default : grade[0]=grade[0]+1;
}
}
printf("\n总评成绩>90的人数是:%d",grade[4]);
printf("\n总评成绩>80且<90的人数是: %d",grade[3]);
printf("\n总评成绩>70且<80的人数是: %d",grade[2]);
printf("\n总评成绩>60且< 70的人数是:%d",grade[1]);
printf("\n总评成绩<60的人数是: %d",grade[0]);
printf("\n");
}
void querydata()
{
char lookname[20];
int i;
printf("\n请输入姓名:");
gets(lookname);
for(i=0;i<sum;i++)
{
if(strcmp(name[i],lookname)==0)
{
printf("平时成绩%f,期中成绩%f,期末成绩%f,总评成绩%f\n",score[i][0],score[i][1],score[i][2],score[i][3] );
}
if(i==sum) printf("没有找到!\n");
}
}


这是我在网上看到的一段代码,是有问题的,主要是拿来改错练习读改程序的能力,我已经改的差不多了,用的编译环境是VC++6.0,编译后只有一条错误提示了!
——————————————————————————————————
Compiling...
Text1.c
C:\Users\Administrator\Desktop\程序测试\Text1.c(59) : error C2059: syntax error : 'type'
执行 cl.exe 时出错.

Text1.obj - 1 error(s), 0 warning(s)
———————————————————————————————————
score[i][3]=float(0.1*score[i][0]+0.3*score[i][1]+0.6*score[i][2]);
问题应该是出在这个强制类型转换上!初学C语言,找了很久没有头绪,特来求教各位大牛!
...全文
1493 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
龙井茶 2011-05-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 kid_coder 的回复:]
C/C++ code

#include<stdio.h>
#include"string.h"

void calculdata();
void inputdata();
//void cacludata();
void statisdata();
void querydata();

char name[100][15];
float score[100][4];
i……
[/Quote]
你这是在干嘛,求真相!
龙井茶 2011-05-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zhao4zhong1 的回复:]
Type Cast Operator: (type)
cast-expression :

unary-expression
( type-name ) cast-expression

A type cast provides a method for explicit conversion of the type of an object in a specific situati……
[/Quote]
英语不好,读起来有点吃力,不过例子看懂了,只是奇怪type(<expression>) 这种格式不行么?
龙井茶 2011-05-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bdmh 的回复:]
float改为(float)
[/Quote]
嗯,谢谢!改了之后编译通过了,能帮开导下么,为什么原先的格式不能通过呢?不是说强制类型转换的格式是type(<expression>) 或 (type)<expression>么?
赵4老师 2011-05-12
  • 打赏
  • 举报
回复
Type Cast Operator: (type)
cast-expression :

unary-expression
( type-name ) cast-expression

A type cast provides a method for explicit conversion of the type of an object in a specific situation.

The compiler treats cast-expression as type type-name after a type cast has been made. Casts can be used to convert objects of any scalar type to or from any other scalar type. Explicit type casts are constrained by the same rules that determine the effects of implicit conversions. Additional restraints on casts may result from the actual sizes or representation of specific types.

Example

In the following example, the type cast operator converts the float value of 3.1 to an integer value of 3.

// Example of the type cast operator
float x = 3.1;
int i;
i = (int)x; // the value of i is now 3
KID_coder 2011-05-12
  • 打赏
  • 举报
回复

#include<stdio.h>
#include"string.h"

void calculdata();
void inputdata();
//void cacludata();
void statisdata();
void querydata();

char name[100][15];
float score[100][4];
int sum;
int main()
{



{
int choo;
printf("请输入实际学生人数:");
scanf("%d",&sum);
while(1)
{
printf("请选择下面的某一项任务:");
printf("\n(1)输入每个学生的姓名,平时成绩,期中成绩,期末成绩");
printf("\n(2)计算并输出每个学生的总评成绩。");
printf("\n(3)根据总评成绩对学生分类统计,输出各分数段的学生数。") ;
printf("\n(4)根据根据输入的学生姓名,查找并打印该学生的各项成绩") ;
printf("\n(5)结束程序运行");
printf("\n请输入你的选择(1或2或3或4或5):") ;
scanf("%d",&choo);
if(choo>=5)break;
switch(choo)
{
case 1 : inputdata(); break;
case 2 : calculdata(); break;//
case 3 : statisdata(); break;
case 4 : querydata(); break;
}
}
printf("\n程序运行结束,再见。");
}
return 0;
}
void inputdata()
{
int i;
for(i=0;i<sum;i++)
{
printf("\n请输学生的姓名:");
gets(name[i]);
printf("请输入学生平时,期中,期末成绩(例如98,95,96):");
scanf("%f,%f,%f",&score[i][0],&score[i][1],&score[i][2]) ;
}
}
void calculdata()
{
int i;
for(i=0;i<sum;i++)
{
score[i][3]=float(0.1*score[i][0]+0.3*score[i][1]+0.6*score[i][2]);
}
printf("\n每个学生总评成绩如下:\n");
for(i=0;i<sum;i++)
{
printf("%s:%f\n",name[i],score[i][3]);
}
}
void statisdata()
{
int i,d;
int grade[5]={0};
for(i=0;i<sum;i++)
{
d=(int)(score[i][3]/10);
switch(d)
{
case 10 : grade[4]=grade[4]+1; break;
case 9 : grade[3]=grade[3]+1; break;
case 8 : grade[2]=grade[2]+1; break;
case 7 : grade[1]=grade[1]+1; break;
case 6 : grade[0]=grade[0]+1; break;
default : grade[0]=grade[0]+1;
}
}
printf("\n总评成绩>90的人数是:%d",grade[4]);
printf("\n总评成绩>80且<90的人数是: %d",grade[3]);
printf("\n总评成绩>70且<80的人数是: %d",grade[2]);
printf("\n总评成绩>60且< 70的人数是:%d",grade[1]);
printf("\n总评成绩<60的人数是: %d",grade[0]);
printf("\n");
}
void querydata()
{
char lookname[20];
int i;
printf("\n请输入姓名:");
gets(lookname);
for(i=0;i<sum;i++)
{
if(strcmp(name[i],lookname)==0)
{
printf("平时成绩%f,期中成绩%f,期末成绩%f,总评成绩%f\n",score[i][0],score[i][1],score[i][2],score[i][3] );
}
if(i==sum) printf("没有找到!\n");
}
}



bdmh 2011-05-12
  • 打赏
  • 举报
回复
float改为(float)
proghua 2011-05-12
  • 打赏
  • 举报
回复
强制类型转换的语法
G_Spider 2011-05-12
  • 打赏
  • 举报
回复
score[i][3]=(float)(0.1*score[i][0]+0.3*score[i][1]+0.6*score[i][2]);

69,371

社区成员

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

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