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



对于这个实验,我不允许编辑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("%dn", weightInPounds);
        fscanf(fp, "%dn", weightInPounds);
        // Asks user for input height in inches.
        printf("What is your height in inches?");
        fprintf("What is your height in inches?");
        scanf("%dn", heightInInches);
        fscanf(fp, "%dn", heightInInches);
        getchar(0);
        return (0);
    }

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

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

你的代码中有许多简单的错误。

  1. 你应该在main之前定义你的calculateBMI函数或者你应该在main之前声明它

  2. 调用calculateBMI函数时传递函数的参数/读取calculateBMI函数内部的值

  3. 如果您声明BMI为double,则在printf语句中使用%lf作为格式说明符。
  4. 不能为else语句提供条件,所以用else if
  5. 用括号表示方程BMI = weightInPounds * 703 / heightInInches * heightInInches;

  6. 你应该为scanf语句传递变量的地址(即&变量)

下面是修改后的代码。

 #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("%dn", &weightInPounds);
        fscanf(fp, "%dn", weightInPounds);
        // Asks user for input height in inches.
        printf("What is your height in inches?");
        fprintf(fp,"What is your height in inches?");
        scanf("%dn", &heightInInches);
        fscanf(fp, "%dn", 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公式中,你应该以米为单位给出身高/将其转换成米

最新更新