论坛首页· 友情链接申请·申请版主· 广告投放· 道具中心· 设为首页· 收藏本站
发新话题
打印

C语言scanf 有错问题!

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");
scanf ("%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'" );
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) ;
  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;
}
这个是改过的...
好像对的

TOP

把getch()改成getchar();
char sex,sports,diet;
float hf,hm,hc;

[ 本帖最后由 困C之斗 于 2008-9-21 14:35 编辑 ]

TOP

楼主

我用的是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'



我不知道怎么上传图片呵呵...要不然更明白了

TOP

刷一下缓冲区就行了
fflush(stdin);
复制内容到剪贴板
代码:
#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;
}

TOP

我发了个帖子,lz看看
http://www.stubc.com/thread-2515-1-1.html

TOP

用 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;
}
这个改后正确的...

TOP

因为WIN-TC不支持新标准C99

TOP

楼主
谢谢了!
原来是缓存问题 啊!

TOP

发新话题