0

我可以在作业上获得一些帮助吗?我必须创建一个 BMI 计算器来接收测试用例并生成一个小数点后的 bmi。我的代码通过了大多数情况,但有一种情况,其中英尺 = 4,英寸 = 10,磅 = 1000。预期结果是 209.0,我只能得到 209 输出,我只需要做一个 if 语句并添加“.0”还是有办法对其进行编码?

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    double feet, inches, pounds, bmi;

    cout << "== BMI Calculator ==" << endl;

    cout << "Step 1: Enter height" << endl;
    cout << "Feet:" << endl;
    cin >> feet;
    cout << "Inches:" << endl;
    cin >> inches;
    cout << "Step 2: Enter weight" << endl;
    cout << "Pounds:" << endl;
    cin >> pounds;

    inches = (feet * 12) + inches;
    bmi = ((pounds * 703) / (inches * inches));
    bmi = ceil(bmi * 10) / 10;

    cout << "BMI: " << bmi << endl;

    if (bmi < 18.5)
        cout << "you are underweight." << endl;
    else if (bmi >= 18.5 && bmi < 25)
        cout << "you are normal." << endl;
    else if (bmi >= 25 && bmi <= 29.9)
        cout << "you are overweight." << endl;
    else
        cout << "you are obese." << endl;
}
4

1 回答 1

0

这只是一个格式问题。看这里:

例子:

  double f = 3.14159;
  cout.unsetf(ios::floatfield);            // floatfield not set
  cout.precision(5);

输出:

3.1416
于 2012-08-24T20:19:04.190 回答