gpt4 book ai didi

c - BMI 计算器实验室找不到问题

转载 作者:行者123 更新时间:2023-11-30 20:22:20 32 4
gpt4 key购买 nike

对于这个实验,我不允许编辑 main 函数,所有事情都必须在 main 下面的函数中完成。我在这里似乎找不到我的问题。我认为这与调用calculateBMI函数有关。

 #include <stdio.h>
FILE *fp;

//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;

BMI = weightInPounds * 703 / heightInInches * heightInInches;

//If BMi is less then 18.5 print this.
if (BMI < 18.5) {
printf("Your BMI is %d, you are underweight.", BMI);
fprintf(fp, "Your BMI is %d, 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 %d, you are Normal.", BMI);
fprintf(fp, "Your BMI is %d, 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 %d, you are Overweight.", BMI);
fprintf(fp, "Your BMI is %d, you are Overweight.", BMI);
}
//if BMI is greater then 30 print this.
else (BMI > 30) {
printf("Your BMI is %d, you are Obese.", BMI);
fprintf(fp, "Your BMI is %d, you are Obese.", BMI);
}

//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("What is your height in inches?");
scanf("%d\n", heightInInches);
fscanf(fp, "%d\n", heightInInches);

getchar(0);
return (0);
}

最佳答案

在 else if 语句中您使用了 & 运算符,但在本例中您需要使用 && 运算符。 & 运算符是按位运算符。例如,如果您有两个 4 位变量 1001 和 1010 。您使用 & 运算符,结果将是 1000 。在这种情况下,您必须使用 && 运算符它应该看起来像这样:

    else if (BMI > 18.5 && BMI < 25) 

关于c - BMI 计算器实验室找不到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39820616/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com