2016-10-02 36 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)