我在制作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;
}
答案 0 :(得分:4)
scanf需要指向格式字符串后面的参数的指针。 使用&amp;运营商因此:
printf("\aEnter your height: ");
scanf(" %f", &height);
printf("\a\nEnter your weight: ");
scanf(" %f", &weight);
答案 1 :(得分:4)
您需要将变量的地址传递给scanf,以便它可以修改该地址的值:
scanf(" %f", &height);
^
\ Address-of operator (Returns the memory address of the float)
和
scanf(" %f", &weight);