新手提问,请各路在侠相助!

wjlforever 2005-01-11 06:34:44
我抄了一例,如下:
main()
{struct student
{int num;
char name[20];
float score;
};
struct student stu[4];
struct student *p;
int i,temp = 0;
float max;
for(i=0;i<4;i++)
{ printf("the %d time",i);
scanf("%d %s%f",&stu[i].num,stu[i].name,&stu[i].score);}
for(max=stu[0].score,i=1;i<4;i++)
if(stu[i].score > max)
{max = stu[i].score;temp=i;}
p=stu+temp;
printf("\nThe maxinum score:\n");
printf("NO.:%d\nname:%s\nscore:%4.1f\n",p->num,p->name,(*p).score);
}

但是为什么不能输入?
我是这样输入的
101(空格)Li(空格)90(回车)   然后出错跳出!
为什么不能向书上那样
运行情况如下:
101 li 90(是个箭头,应该是回车吧)
102 Tan 98(回车)
103 Wang 67(回车)
104 Fun 89(回车)
The maxinum score;
No.:102
name:Tan
score:98.0
...全文
161 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjlforever 2005-01-20
  • 打赏
  • 举报
回复
自己顶!
wjlforever 2005-01-19
  • 打赏
  • 举报
回复
可是我为什么真的不行!
pcboyxhy 2005-01-17
  • 打赏
  • 举报
回复
在GCC编译后运行,
结果完全没有问题
程序没有错误
wjlforever 2005-01-17
  • 打赏
  • 举报
回复
自己顶!
wjlforever 2005-01-16
  • 打赏
  • 举报
回复
to qingyuan12(zealot_tang):
我知道,我也是这样输入的:
101 li90
但是就是不行。
郁闷!!!!!!!!!!
king1619 2005-01-16
  • 打赏
  • 举报
回复
我也遇到过这种情况!!
望大虾赐教!!
wjlforever 2005-01-15
  • 打赏
  • 举报
回复
应该是那个float形的不支行,我试过去掉float ,就可以!
难到是系统不支持,我用的是turbo c 2.0!
是字符串的问题吗!??
qingyuan18 2005-01-15
  • 打赏
  • 举报
回复
scanf()语句是格式化输入,你是scanf("%d %s%f",&stu[i].num,stu[i].name,&stu[i].score);
那就必须这样输入:
103 wqn455.0
你是scanf("%d,%s,%f",&stu[i].num,stu[i].name,&stu[i].score);当然输入时也必须加‘,’了。

建议楼主使用sscanf(),可以有效的区分不同的类型
wjlforever 2005-01-15
  • 打赏
  • 举报
回复
各位大侠:
请问你们真的运行了吗!?
我是这样输入的
101(空格)li90(回车) 或 101(空格)li(空格)90(回车)
然后跳出,按alt+f5,就出现上面的错误
scanf : floatinng point formats not linked
abnormal program termination;
但是我改成scanf("%d,%s,%f",&stu[i].num,stu[i].name,&stu[i].score)
然后输入
101,li,90
102,tan,89
103,wqn,78
104,fff,455
就不会出错了!
szywelcome 2005-01-15
  • 打赏
  • 举报
回复
没有错啊,
# include "stdio.h"
main()
{struct student
{int num;
char name[20];
float score;
};
struct student stu[4];
struct student *p;
int i,temp = 0;
float max;
for(i=0;i<4;i++)
{ //printf("%d\n",i);
scanf("%d %s%f",&stu[i].num,stu[i].name,&stu[i].score);}
for(max=stu[0].score,i=1;i<4;i++)
if(stu[i].score > max)
{max = stu[i].score;temp=i;}
p=stu+temp;
printf("\nThe maxinum score:\n");
printf("NO.:%d\nname:%s\nscore:%4.1f\n",p->num,p->name,(*p).score);
}

rorl 2005-01-14
  • 打赏
  • 举报
回复
vc下测试
正常,可以输入4次,并得出正确结果
prgmBaggio 2005-01-14
  • 打赏
  • 举报
回复
用scanf时最好不要接收字符串或者字符,会出现意想不到的结果,最好用gets()然后再做相应的类型转换
wen1818 2005-01-14
  • 打赏
  • 举报
回复
帮忙顶
wjlforever 2005-01-11
  • 打赏
  • 举报
回复
自己顶!
wjlforever 2005-01-11
  • 打赏
  • 举报
回复
我在输入101(空格)Li(空格)90(回车)后就
报这样的错误:
scanf : floatinng point formats not linked
abnormal program termination;
这是为什么呢?????
wjlforever 2005-01-11
  • 打赏
  • 举报
回复
但是,我在输入
101 li 90(回车)
102 Tan 98(回车)
103 Wang 67(回车)
104 Fun 89(回车)
的时候,
输入第一行就跳出!

那连续输入字符串时,怎样写scanf(),在输入的时候怎么断开,就是怎么才能让系统认出是不同的字符串。
CMyMfc 2005-01-11
  • 打赏
  • 举报
回复
没有错误

69,371

社区成员

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

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