#c #function
#c #функция
Вопрос:
Для этой лаборатории мне не разрешено редактировать функцию 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 amp; 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 amp; 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);
}
Комментарии:
1. Не можете найти свою «проблему» и «проблему»? Компилятор выдает так много предупреждений и ошибок, что вы должны устранять их одно за другим, как сообщает компилятор. Пожалуйста, включите все предупреждения компилятора.
2. Извините, я спешил, и я новичок на этом форуме.
Ответ №1:
В инструкции else if вы использовали оператор amp; , но в этом случае вам нужно использовать оператор amp;amp; . Оператор amp; является побитовым оператором. Например, если у вас есть две 4-разрядные переменные 1001 и 1010. При использовании оператора amp; результат будет равен 1000. В этом случае вы должны использовать amp;amp; operator Это должно выглядеть так:
else if (BMI > 18.5 amp;amp; BMI < 25)
Ответ №2:
В вашем коде много простых ошибок.
-
Вы должны определить свою функцию calculateBMI перед main или объявить ее перед main.
-
при вызове
calculateBMI
функции передайте параметр для функции / прочитайте значения внутриcalculateBMI
функции. - если вы объявляете BMI как double, используйте
%lf
в качестве спецификатора формата в инструкции printf. - не могу указать условие для оператора else, поэтому сделайте это
else if
-
используйте скобку для уравнения
BMI = weightInPounds * 703 / heightInInches * heightInInches;
-
вы должны передать адрес переменной для оператора scanf (т.е. amp;variable)
вот измененный код.
#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", amp;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", amp;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 amp; 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 amp; 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 вы должны указать рост в метрах / преобразовать его в метры.