2016-10-02 9 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

をあなたの「問題」と見つけることができません「問題を?」コンパイラからは非常に多くの警告とエラーがありますので、コンパイラから通知されたとおり、それらを1つずつ記述する必要があります。すべてのコンパイラの警告を有効にしてください。 –

+0

申し訳ありませんが、急いでおり、このフォーラムは初めてです。 – Chris

答えて

0

コードには多くの簡単な間違いがあります。

  1. mainの前にcalculateBMI関数を定義するか、mainの前に宣言する必要があります。

  2. calculateBMI関数は、関数のパラメータを渡します。calculateBMI関数内の値を読み取ります。

  3. BMIをdoubleと宣言した場合は、printfステートメントの書式指定子として%lfを使用します。
  4. else文の条件を与えるので、それは式 BMI = weightInPounds * 703/heightInInches * heightInInches;

  5. ためelse if

  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ステートメントでは&演算子を使用しましたが、この場合は& &演算子を使用する必要があります。 &演算子はビット単位の演算子です。 たとえば、2つの4ビット変数1001と1010があるとします。 &演算子を使用すると、結果は1000になります。 あなたが& &オペレータ を使用する必要があります。この場合、それは次のようになります。

else if (BMI > 18.5 && BMI < 25) 
関連する問題