2016-10-02 93 views
-2

对于本实验我不允许编辑主函数,所有操作都必须在main函数下完成。我似乎无法在这里找到我的问题。我认为这与调用calculateBMI函数有关。BMI计算器实验室找不到问题

#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); 
    } 
+1

无法找到你的“问题”和编译器有太多的警告和错误,您应该按照编译器的通知逐一解决它们。请启用所有编译器警告。 –

+0

对不起,我很匆忙,我是这个论坛的新手。 – Chris

回答

0

你的代码有很多简单的错误。

  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("%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公式中,您应该以米为单位给出高度/将其转换为米。

+0

非常感谢你的帮助! – Chris

1

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

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