简体   繁体   中英

Skipping over last input when using scanf

there's an issue with my C program that calculates calories based on age gender weight height and activity level. its skipping over my last input segment (that asks for gender). Help anyone???

#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);

}

As you are taking the input as boy or girl you need to use char in case of float . Declare gender as char . Clear your basics about data types and use of == .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM