0

私は C++ の使用方法を学んでおり、私が抱えている問題を解決するために助けていただければ幸いです。これは私が書いた最初のプログラムで、消費カロリー数とカロリーを消費するのに必要な距離を計算します。私の唯一の問題は、出力「total_calories」に小数点以下の桁数が表示されないことです。1775 の代わりに 1775.00 を表示したいです。入力値は、burgers_consumed = 3、fries_consumed = 1、drinks_consumed = 2 です。

私が得た出力は次のとおりです。1775 カロリーを摂取しました。そのエネルギーを消費するには、4.73 マイル走る必要があります。

コードは次のとおりです。

#include <iostream>
using namespace std;

int main()
{
    const int BURGER_CALORIES = 400;
    const int FRIES_CALORIES = 275;
    const int SOFTDRINK_CALORIES = 150;
    double burgers_consumed;
    double fries_consumed;
    double drinks_consumed;
    double total_calories;
    double total_distance;


    //Get the number of hamburgers consumed.
    cout << " How many hamburgers were consumed? ";
    cin >>  burgers_consumed;

    //Get the number of fries consumed.
    cout << " How many french fries were consumed? ";
    cin >> fries_consumed;

    //Get the number of drinks consumed.
    cout << " How many soft drinks were consumed? ";
    cin >> drinks_consumed;

    //Calculate the total calories consumed.
    total_calories = (BURGER_CALORIES * burgers_consumed) + (FRIES_CALORIES * fries_consumed) + (SOFTDRINK_CALORIES * drinks_consumed);

    //Calculate total distance needed to burn of calories consumed.
    total_distance = total_calories/375;

    //Display number of calories ingested.
   cout.precision(6);
    cout << " You ingested " << total_calories << " calories. " << endl;

    //Display distance needed to burn off calories.
   cout.precision(3);
    cout << " You will have to run " << total_distance << " miles to expend that much energy. " << endl;
    return 0;
}   
4

1 に答える 1