无法让程序在C++输出正确的结果

Can't get program to output the right result in C++

本文关键字:结果 输出 C++ 程序      更新时间:2023-10-16

我被要求制作这个程序来计算一个人的体重指数。由于我注意到METRIC未运行,US_IMPERIAL不完整。我无法算出错误,因为无论我输入什么数字,结果都是0。

#include <iostream>
using namespace std;
float METRIC (int b, int c)
{
float res;
res = b / c / c * 10000;
return res;
}

float US_IMPERIAL (float d, float e)
{
float resu;
resu = (d / e / e) * 703;
return resu;
}
int main () 
{
cout << "Hello. BMI CALCULATOR!" << endl <<
"Press 1 for METRIC or 2 for US imperial: ";
int a;
cin >> a;
if (a == 1) {
cout << "Enter your weight in KILOGRAMS (KG): ";
int b;
cin >> b;
cout << "Now enter you height in CENTIMETERS(CM): ";
int c;
cin >> c;
cout << "Your BMI is: " << METRIC (b, c);
} else if (a == 2) {
// not yet implemented
} else {
cout << "Error.";
}
return 0;
}

METRIC函数中,表达式b / c / c * 10000全整数表达式。如果你想要分数,那就不太好用了。

我建议您将参数bc转换为浮点变量。