我需要对输出进行舍入并在C ++中显示xxx.0以进行赋值

时间:2012-08-24 20:13:33

标签: c++ cmath ceil

我可以帮助完成作业吗?我必须创建一个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;
}

1 个答案:

答案 0 :(得分:0)

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

实施例

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

输出:

3.1416