晚上好。我刚开始在大学里学习C,我们的第一个任务就是制作一个计算用户bmi的程序。对于我的生活,我无法弄清楚为什么我没有得到我想要的结果。无论身高和高度重量我提交给程序...我继续得到像4258544这样可笑的高bmi然后它告诉我我体重不足。我以为我可以用另一双眼睛。 请帮忙。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void h_conversion (double *h) {
*h = *h/12;
}
int bmi_category (double h, double w, double b) {
b = (w * 4.88)/(h * h);
if (b < 20)
return 1;
else if (b >= 20 && b < 25)
return 2;
else if (b >= 25 && b < 30)
return 3;
else if (b >= 30 && b < 40)
return 4;
else
return 5;
}
int main (void) {
double height, weight, bmi;
double *h, *w, *b;
h = &height;
w = &weight;
b = &bmi;
printf ("Please enter height (inches): \n");
scanf (" %lf", &height);
while ( h <= 0 ) {
printf ("Weee, this is fun! Try again.\n");
scanf (" %.2lf", &height);
}
if ( *h < 42 || *h > 84 )
printf ("Your entry falls outside the norm for an adult.\n");
h_conversion( h );
printf ("Please enter weight (lbs): \n");
scanf (" %.2lf", &weight);
while ( w <= 0 ) {
printf ("This is why we can't have nice things. Try again.\n");
scanf (" %.2lf", &weight);
}
switch ( bmi_category(*h, *w, *b)) {
case 1:
printf ("BMI: %d. Underweight.", (int)&bmi);
break;
case 2:
printf ("BMI: %d. Normal weight.", (int)&bmi);
break;
case 3:
printf ("BMI: %d. Slightly overweight.", (int)&bmi);
break;
case 4:
printf ("BMI: %d. Overweight.", (int)&bmi);
break;
case 5:
printf ("BMI: %d. Extremely overweight.", (int)&bmi);
break;
default:
printf ("error condition\n");
}
system ("pause");
return 1;
}
答案 0 :(得分:2)
在您的打印中,您正在使用
(int)&bmi
// this request the address of bmi and casts that value to an integer (addresses are always integers)
何时使用
(int)bmi
// this casts the value of the variable bmi to an integer which is what you want.
您总是得到的奇怪价值是因为您获取地址而不是变量值。
还有为什么所有指针变量?你只需要一个。 bmi函数不要求你使用指针,那么为什么要使用它们而不是它的非指针对应物?
当主要执行完毕且没有任何问题时,主要返回0。由于某种原因,您返回1表示该程序没有正确完成执行。
答案 1 :(得分:0)
在您对bmi_category
功能的调用中,参数传递不正确。您在预期指针时传递了值。因此,计算出的bmi
的值是bmi_category
函数的本地值,不会更新给调用者。
打印bmi
的值时,您打印的是地址&bmi
,而不是值。
这是固定代码,稍加清理:
#include <stdio.h>
#include <stdlib.h>
void h_conversion(double *h) {
*h = *h / 12;
}
int bmi_category(const double *h, const double *w, double *b) {
*b = (*w * 4.88) / (*h * *h);
if (*b < 20)
return 1;
else if (*b >= 20 && *b < 25)
return 2;
else if (*b >= 25 && *b < 30)
return 3;
else if (*b >= 30 && *b < 40)
return 4;
else
return 5;
}
int main(void) {
double height, weight, bmi;
printf("Please enter height (inches): \n");
scanf(" %lf", &height);
while (height <= 0) {
printf("Weee, this is fun! Try again.\n");
scanf(" %lf", &height);
}
if (height < 42 || height > 84)
printf("Your entry falls outside the norm for an adult.\n");
h_conversion(&height);
printf("Please enter weight (lbs): \n");
scanf(" %lf", &weight);
while (weight <= 0) {
printf("This is why we can't have nice things. Try again.\n");
scanf(" %lf", &weight);
}
switch (bmi_category(&height, &weight, &bmi)) {
case 1:
printf("BMI: %d. Underweight.", (int)bmi);
break;
case 2:
printf("BMI: %d. Normal weight.", (int)bmi);
break;
case 3:
printf("BMI: %d. Slightly overweight.", (int)bmi);
break;
case 4:
printf("BMI: %d. Overweight.", (int)bmi);
break;
case 5:
printf("BMI: %d. Extremely overweight.", (int)bmi);
break;
default:
printf("error condition\n");
}
system("pause");
return 1;
}