| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 691 人关注过本帖
标题:为什么这个程序不能输入完全我想要输入的数据?
只看楼主 加入收藏
awei062
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2008-9-30
收藏
 问题点数:0 回复次数:3 
为什么这个程序不能输入完全我想要输入的数据?
求身高问题
#include "stdio.h"
void main()
{
 float faheight,moheight,myheight;
 char sex,sport,diet;
  printf("faheight and moheight:\n");
 scanf("% f,% f",&faheight,&moheight);
 printf("are you a boy(y/n):\n");
 scanf("%c",&sex);

 printf("do you like sports:(y/n)\n");
 scanf("% c",&sport);
 printf("do you have healthy diet(y/n):\n");
 scanf("% c",&diet);
 
 
 if(sex=='m'||sex=='M')
 myheight=(faheight+moheight)*0.54;
 else
 myheight=(faheight*0.923+moheight)/2;
 if(sport=='y'||sport=='Y')
 myheight=myheight*(1+0.02);
 if(diet=='y'||diet=='Y')
 myheight=myheight*(1+0.015);
 printf("myheight=%f\n",myheight);
 
}
搜索更多相关主题的帖子: 数据 输入 
2008-10-24 18:20
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
又是接收回车符的问题吧。

倚天照海花无数,流水高山心自知。
2008-10-24 18:22
gongqivictor
Rank: 1
来 自:武汉科大
等 级:新手上路
帖 子:32
专家分:0
注 册:2008-9-16
收藏
得分:0 
具体怎么改呢?
2008-10-24 19:22
awei062
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2008-9-30
收藏
得分:0 
终于发现错误了
原来是输入字符的scanf 函数中的空格位置不对。scanf("%  c")给为scanf("  %c")
正确程序为
#include "stdio.h"
void main()
{
 float faheight,moheight,myheight;
 char sex,sport,diet;
  printf("faheight:\n");
 scanf("%f",&faheight);
 printf("moheight:\n");
 scanf("%f",&moheight);
 printf("are you a boy(y/n):\n");
 scanf(" %c",&sex);

 printf("do you like sports:(y/n)\n");
 scanf(" %c",&sport);
 printf("do you have healthy diet(y/n):\n");
 scanf(" %c",&diet);
 
 
 if(sex=='Y'||sex=='y')
 myheight=(faheight+moheight)*0.54;
 else
 myheight=(faheight*0.923+moheight)/2.0;
 if(sport=='Y'||sport=='y')
 myheight=myheight*(1+0.02);
 if(diet=='Y'||diet=='y')
 myheight=myheight*(1+0.015);
 printf("myheight=%f\n",myheight);
 
}
2008-10-25 21:55
快速回复:为什么这个程序不能输入完全我想要输入的数据?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017132 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved