对于本实验,我不允许编辑main函数,所有内容都必须在main下面的函数中完成。我似乎无法在这里找到我的问题。我认为它与对calculateBMI函数的调用有关。
SELECT SYSDATETIME();
答案 0 :(得分:1)
在else if语句中,您使用了&运营商,但在这种情况下,您需要使用&&运营商。 & operator是一个按位运算符。 例如,如果您有两个4位变量1001和1010。你使用&运算符结果将为1000。 在这种情况下,你必须使用&&操作者 它应该是这样的:
else if (BMI > 18.5 && BMI < 25)
答案 1 :(得分:0)
您的代码中有许多简单的错误。
您应该在main之前定义您的calculateBMI函数,或者您应该在main之前声明它。
在调用calculateBMI
函数时传递函数的参数/读取calculateBMI
函数内的值。
%lf
作为格式说明符。else if
使用方括号括起来
BMI = weightInPounds * 703 / heightInInches * heightInInches;
你应该传递scanf语句变量的地址(即&amp;变量)
这是修改后的代码。
#include <stdio.h>
FILE *fp;
double calculateBMI();
//For loop, which allows up to 4 entries.
int main(void) {
int i;
fp = fopen("csis.txt", "w");
for (i = 1; i <= 4; ++i) {
calculateBMI();
}
fclose(fp);
return 0;
}
//Function that calculates the BMI of the Input.
double calculateBMI(int weightInPounds, int heightInInches) {
double BMI=0;
//Asks user for input weight in pounds.
printf("What is your weight in pounds?");
fprintf(fp, "What is your weight in pounds?");
scanf("%d\n", &weightInPounds);
fscanf(fp, "%d\n", weightInPounds);
// Asks user for input height in inches.
printf("What is your height in inches?");
fprintf(fp,"What is your height in inches?");
scanf("%d\n", &heightInInches);
fscanf(fp, "%d\n", heightInInches);
BMI = (weightInPounds * 703) / (heightInInches * heightInInches);
//If BMi is less then 18.5 print this.
if (BMI < 18.5) {
printf("Your BMI is %f, you are underweight.", BMI);
fprintf(fp, "Your BMI is %f, you are underweight.", BMI);
}
//if BMI is between 18.5 and less then 25 print this.
else if (BMI > 18.5 & BMI < 25) {
printf("Your BMI is %f, you are Normal.", BMI);
fprintf(fp, "Your BMI is %f, you are Normal.", BMI);
}
//if BMI is greater then 25 and less then 30 print this.
else if (BMI > 25 & BMI < 30) {
printf("Your BMI is %f, you are Overweight.", BMI);
fprintf(fp, "Your BMI is %f, you are Overweight.", BMI);
}
//if BMI is greater then 30 print this.
else if(BMI > 30) {
printf("Your BMI is %f, you are Obese.", BMI);
fprintf(fp, "Your BMI is %f, you are Obese.", BMI);
}
getchar();
return (0);
}
额外信息。我认为在BMI公式中你应该给出以米为单位的高度/将其转换为米。