1

我在制作 BMI 计算器程序时遇到了麻烦,我想知道我做错了什么。我是初学者,所以请放轻松,谢谢!

#include <stdio.h>

main()
{
    // Variables for height, weight, and bmi
    float height;
    float weight;
    float bmi;

    printf("\aEnter your height: ");
    scanf(" %f", height);

    printf("\a\nEnter your weight: ");
    scanf(" %f", weight);

    bmi = (height * 4.88) / (weight * weight);

    printf("\a\nYour BMI is: %f", bmi);
    getchar();

    return 0;
}
4

2 回答 2

4

scanf 需要一个指向格式字符串后面的参数的指针。因此使用 & 运算符:

printf("\aEnter your height: ");
scanf(" %f", &height);

printf("\a\nEnter your weight: ");
scanf(" %f", &weight);
于 2013-08-29T20:50:29.170 回答
4

您需要将变量的地址传递给scanf,以便它可以修改该地址处的值:

scanf(" %f", &height);
             ^
             \ Address-of operator (Returns the memory address of the float)

和:

scanf(" %f", &weight);
于 2013-08-29T20:50:42.867 回答