Chris - 2 months ago 9
C Question

# BMI calculator lab can't find issue

For this lab I am not allowed to edit the main function, everything must be done in the function below main. I can't seem to find my problem here. I think it has something to do with the call to the calculateBMI function.

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

There are many simple mistakes in your code.

1. You should define your calculateBMI function before main or You should declare it before main.

2. while calling `calculateBMI` function pass the parameter for the function / read the values inside the `calculateBMI` function.

3. if you declare BMI as double then use `%lf` as format specifier in printf statement.
4. cant give condition for else statement, so make it `else if`
5. use bracket for equation `BMI = weightInPounds * 703 / heightInInches * heightInInches;`

6. you should pass the address of the variable for scanf statement (i.e &variable)

here is the modified code.

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

extra info. i think the in BMI formula you should give height in meters/ convert it into meters.

Source (Stackoverflow)