使用scanf时跳过最后输入

时间:2019-02-05 02:34:04

标签: c

我的C程序存在一个问题,该问题根据年龄,性别,体重和活动水平来计算卡路里。它跳过了我的最后一个输入段(要求性别)。帮助任何人?

#include <stdio.h>

int
main (void)

{
    float weight, height, age,  activity, none, moderate, active, VeryActive, gender, boy, girl, 
          baseg, baseb, caloriesb1, caloriesb2, caloriesb3, caloriesb4, caloriesg1, caloriesg2, caloriesg3, caloriesg4;


    printf ("Enter your weight (kg): ");
    scanf ("%f", &weight);

    printf ("Enter your age (years): ");
    scanf ("%f", &age);

    printf ("enter your height (cm): ");
    scanf ("%f", &height);

    printf ("enter activity level (none/moderate/active/VeryActive): ");
    scanf ("%f", &activity);

    printf ("enter gender (boy/girl): ");
    scanf ("%f", &gender);


    if (gender == boy)
        {
        baseb = 10*weight + 6.25*height - 5*age + 5;
        caloriesb1 = 1.2*baseb;
        caloriesb2 = 1.375*baseb;
        caloriesb3 = 1.55*baseb;
        caloriesb4 = 1.725*baseb;

            if (activity == none)
                {
                printf ("daily calories is: %f, ", caloriesb1);
                } 

            else if (activity == moderate)
                {
                printf ("daily calories is: %f, ", caloriesb2);
                }

            else if (activity == active)
                {
                printf ("daily calories is: %f, ", caloriesb3);
                }

            else if (activity == VeryActive)
                {
                printf ("daily calories is: %f, ", caloriesb4);
                }

            else 
                {
                printf ("invalid activity level for boy \n");
                }
        }


    else if (gender == girl)
        {
        baseg = 10*weight + 6.25*height - 5*age - 161;
        caloriesg1 = 1.2*baseg;
        caloriesg2 = 1.375*baseg;
        caloriesg3 = 1.55*baseg;
        caloriesg4 = 1.725*baseg;

            if (activity == none)
                {
                printf ("daily calories is: %f, ", caloriesg1);
                } 

            else if (activity == moderate)
                {
                printf ("daily calories is: %f, ", caloriesg2);
                }

            else if (activity == active)
                {
                printf ("daily calories is: %f, ", caloriesg3);
                }

            else if (activity == VeryActive)
                {
                printf ("daily calories is: %f, ", caloriesg4);
                }

            else 
                {
                printf ("invalid activity level for gitl \n");
                }
        }


    else
        { 
        printf ("invalid gender statment \n");
        }



return (0);

}

1 个答案:

答案 0 :(得分:1)

当您将输入作为boygirl时,对于char,您需要使用float。将gender声明为char。清除有关数据类型和==使用的基础知识。

printf ("enter gender (boy/girl): ");    
scanf ("%c", &gender);