需要帮助在C中编写BMI计算器

时间:2013-10-21 21:59:24

标签: c

我正在尝试编写一个程序,该程序将接受用户输入的重量和高度,然后返回BMI值并告诉用户他们是否低于/超过或正常体重。代码编译时没有错误,但无论我输入的重量和高度是多少,结果总是“你的BMI为0,你的体重状态超重”。我的代码有问题或我的数学不正确吗?

#include <stdio.h>

int main()
{
    double wt_lb, ht_in, bmi, ht_ft;

    printf("Please enter your weight in whole pounds: ");
    scanf("%lf", &wt_lb);
    printf("Please enter your height in whole inches: ");
    scanf("%lf", &ht_in);

    ht_ft = ht_in/12;
    bmi = (703*wt_lb)/(ht_ft*ht_ft);

    if (bmi < 18.5) {
            printf("You have a BMI of %.lf, and your weight status is underweight\n" &bmi);
    } else if (bmi >= 18.5 && bmi < 25) {
        printf("You have a BMI of %.lf, and your weight status is normal\n", &bmi);
    } else {
        printf("You have a BMI of %.lf, and your weight status is overweight\n", &bmi);
    }
}

2 个答案:

答案 0 :(得分:2)

从您&的所有参数中删除printf

 printf("You have a BMI of %f, and your weight status is underweight\n" &bmi);  
                                                                        ^
                                                                        |
                                                                  Remove this &  

应该是

  printf("You have a BMI of %f, and your weight status is underweight\n", bmi);  

也绝不使用%lfdouble的{​​{1}}说明符(在printf中必须使用),而是使用scanf

答案 1 :(得分:1)

在printf语句中不要使用&amp; bmi,请使用简单的bmi。 它应该工作