0

課題を手伝ってもらえますか? テスト ケースを取り込んで、小数点以下 1 桁の BMI を生成する BMI Calculator を作成する必要がありました。私のコードは、フィート = 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 に答える