主题:请教关于scanf函数的问题(不知道程序为什么出错)
[code=c]
请填写代码
[/code]
# include <stdio.h>
void main(){
double hf,hm,h1,h2;
char sex;
//printf("if you are a man,input 'm',else input 'f'\n ");
//scanf("%c",&sex);
printf("input your father's height:hf = ");
scanf("%lf",&hf);
printf("input your mather's height:hm = ");
scanf("%lf",&hm);
printf("if you are a man,input 'm',else input 'f'");
scanf("%c",&sex);
if(sex == 'f')
{
h1=(hf*0.923+hm)*0.5;
h2= (hf*0.923+hm)*0.5*(1+0.015)*(1+0.02);
printf("the worst is %g\n",h1);
printf("the best is %g\n",h2);
}
else if(sex == 'm')
{
h1=(hf+hm)*0.52;
h2 = (hf+hm)*0.52*(1+0.015)*(1+0.02);
printf("the worst is %g\n",h1);
printf("the best is %g\n",h2);
}
else
printf("wrong!");
}
[code=c]
请填写代码
[/code]
上面有两个注释掉的地方,这是完全相同的语句,只不过是放的地方不同,如果把下面的语句注释掉,执行上面的,得出的结果是正确的,但如果把上面的注释掉,执行下面的语句,编译,连接都正确,但结果不正确,都不用你输入是男是女,就得出结果,不知道是什么原因,希望大家给解答下,哈哈,不盛感激1
请填写代码
[/code]
# include <stdio.h>
void main(){
double hf,hm,h1,h2;
char sex;
//printf("if you are a man,input 'm',else input 'f'\n ");
//scanf("%c",&sex);
printf("input your father's height:hf = ");
scanf("%lf",&hf);
printf("input your mather's height:hm = ");
scanf("%lf",&hm);
printf("if you are a man,input 'm',else input 'f'");
scanf("%c",&sex);
if(sex == 'f')
{
h1=(hf*0.923+hm)*0.5;
h2= (hf*0.923+hm)*0.5*(1+0.015)*(1+0.02);
printf("the worst is %g\n",h1);
printf("the best is %g\n",h2);
}
else if(sex == 'm')
{
h1=(hf+hm)*0.52;
h2 = (hf+hm)*0.52*(1+0.015)*(1+0.02);
printf("the worst is %g\n",h1);
printf("the best is %g\n",h2);
}
else
printf("wrong!");
}
[code=c]
请填写代码
[/code]
上面有两个注释掉的地方,这是完全相同的语句,只不过是放的地方不同,如果把下面的语句注释掉,执行上面的,得出的结果是正确的,但如果把上面的注释掉,执行下面的语句,编译,连接都正确,但结果不正确,都不用你输入是男是女,就得出结果,不知道是什么原因,希望大家给解答下,哈哈,不盛感激1