绝神005 2008-9-21 12:48
C语言scanf 有错问题!
各位大哥小弟刚学...问题有点低级不要见笑啊。我用的是WIN-TC
int main(void)
{
char sex,sports,diet ;
int a,b;
float hf,hm,hc ;
printf("please input hf and hm\n");
[color=#f00000]scanf[/color] ("%f%f",&hf,&hm);
printf("you are man or women?\n man input'M'women input'F'\n") ;
scanf("%c",&sex);
if (sex=='M') hc=(hf+hm)*0.54 ;
else hc=(hf*0.923+hm)/2;
printf ("Do you like physicd execise it?input 'y'or'n'" );
[color=#f00000]scanf[/color] ("%c",&sports);
if (sports=='y')hc*=1.02;
printf ("Do you have good eating habits?input 'y'or'n'" ) ;
[color=#f00000]scanf[/color]("%c",&diet);
if (diet=='y')hc*=1.015;
printf("hc=%f",hc) ;
getch();
return 0;
}
这3个scanf 为什么有2个是没用的
程序运行时
想问下哪里错了?
#include <Stdio.h>
#include "Conio.h"
int main(void)
{
char sex,sports,diet ;
float hf,hm,hc ;
printf("please input hf and hm\n");
scanf ("%f%f",&hf,&hm);
printf("you are man or women?\n man input'M'womeninput'F'\n") ;
sex=getch();
if (sex=='M') hc=(hf+hm)*0.54 ;
else hc=(hf*0.923+hm)/2;
printf ("Do you like physicd execise it?input 'y'or'n'\n" );
sports=getch();
if (sports=='y')hc*=1.02;
printf ("Do you have good eating habits?input 'y'or'n'\" ) ;
diet=getch() ;
if (diet=='y')hc*=1.015;
printf("hc=%f",hc) ;
getch();
return 0;
}
这个是改过的...
好像对的
困C之斗 2008-9-21 14:33
把getch()改成getchar();
char sex,sports,diet;
float hf,hm,hc;
[[i] 本帖最后由 困C之斗 于 2008-9-21 14:35 编辑 [/i]]
绝神005 2008-9-21 14:48
楼主
我用的是win-tc
只是 第一种方法scanf 没用。第二种是对的.
输出结果是:蓝的为键盘输入
please input hf and hm
170 /*这2个170是输入部分*/
170
you are man or women?
man input'M'women input'F' /* 就是这个问题!本来这里会要求用户输入一个字符的,可是直接跳过了*/
Do you like physicd execise it?input 'y'or'n'
我不知道怎么上传图片呵呵...要不然更明白了
jason 2008-9-21 15:49
刷一下缓冲区就行了
fflush(stdin);[code]#include <iostream>
using namespace std;
int main(void)
{
char sex,sports,diet ;
int a,b;
float hf,hm,hc ;
printf("please input hf and hm\n");
scanf ("%f%f",&hf,&hm);
fflush(stdin);
printf("you are man or women?\n man input'M'women input'F'\n") ;
scanf("%c",&sex);
if (sex=='M') hc=(hf+hm)*0.54 ;
else hc=(hf*0.923+hm)/2;
printf ("Do you like physicd execise it?input 'y'or'n'" );
scanf ("%c",&sports);
if (sports=='y')hc*=1.02;
printf ("Do you have good eating habits?input 'y'or'n'" ) ;
scanf("%c",&diet);
if (diet=='y')hc*=1.015;
printf("hc=%f",hc) ;
getchar();
return 0;
}[/code]
jason 2008-9-21 16:03
我发了个帖子,lz看看
[url]http://www.stubc.com/thread-2515-1-1.html[/url]
绝神005 2008-9-21 16:14
用 fflush(stdin);
果然解决的了。谢谢
不过我就不明白为什么只有scanf 会没用!
哪位高手说下啊...
环境:Win-TC
#include<stdio.h>
int main(void)
{
char sex,sports,diet ;
int a,b;
float hf,hm,hc ;
printf("please input hf and hm\n");
scanf ("%f%f",&hf,&hm);
fflush(stdin);
printf("you are man or women?\n man input'M'women input'F'\n") ;
scanf("%c",&sex);
if (sex=='M') hc=(hf+hm)*0.54 ;
else hc=(hf*0.923+hm)/2;
printf ("Do you like physicd execise it?input 'y'or'n'" );
fflush(stdin);
scanf ("%c",&sports);
if (sports=='y')hc*=1.02;
printf ("Do you have good eating habits?input 'y'or'n'" ) ;
fflush(stdin);
scanf("%c",&diet);
if (diet=='y')hc*=1.015;
printf("hc=%f",hc) ;
getch();
return 0;
}
这个改后正确的...