0

私が取り組んでいる私のプログラムは、次のように出力することになっています: * 必要な塗料のガロン数 * 必要な労働時間 * 塗料の費用 * 人件費 * 塗装作業の総費用 ただし、それは表示されますすべてのフィールドで 0.. 私は今何を間違えましたか? どうぞよろしくお願いいたします。

これが私のコードです:

//Headers
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <iomanip>

using namespace std;

void PaintJobEstimator(double gallonprice, double calc)
{
    float numBucket=0;
    float hours=0;
    float bucketCost=0;
    float laborCharges=0;
    float totalCost=0;
    //calculates number of buckets of paint (gallons) needed
    numBucket=numBucket+calc*(1/115);
    //calculates paint cost
    bucketCost=bucketCost+gallonprice*numBucket;
    //calculates labor hour
    hours=hours+calc*(8/115);
    //calculates labor charges
    laborCharges=hours*18;
    //calculates total cost
    totalCost=totalCost+bucketCost+laborCharges;
    //Console output
    cout << "The number of Gallons of paint required:\t" << setprecision(2) << numBucket << endl;
    cout << "The hours of labor required:\t" << setprecision(2) << hours << " hrs" << endl;
    cout << "The labor charges:\t$" << setprecision(2) << laborCharges << endl;
    cout << "The cost of the paint:\t$" << setprecision(2) << bucketCost << endl;
    cout << "The total cost of the paint job:\t$" << setprecision(2) << totalCost << endl;
}

void main ()
{
    int rooms;
    double calc=0;
    double wallspace;
    double gallonprice;
    cout << "=========================================================\n";
    cout << "___________________Paint Job Estimator___________________\n";
    cout << "_________________________________________________________\n";
    cout << endl;
    cout << "Enter the number of rooms: ";
    cin >> rooms;
    while (rooms<1) //validates rooms
    {
        cout << "Invalid entry, enter one or more rooms:\t";
        cin >> rooms;
    }
    for (int roomNum=1;
        roomNum<=rooms;
        roomNum++)
    {
        cout << "Enter the wall space in square meters for room " << roomNum << ":\t" << endl;
        cin >> wallspace;
        while (wallspace < 0.01)//validates wallspace
        {
            cout << "Invalid entry, please re-enter the wall area for room " << roomNum << ":\t";
            cin >> wallspace;
        }
        calc=calc+wallspace;
    }//end loop
    cout << "\nEnter price of the paint per gallon: ";
    cin >> gallonprice;
    if (gallonprice <10) //validates price per gallon
    {
        cout << "Invalid entry, Reenter price at a $10.00 minimum: ";
        cin >> gallonprice;
    }
    PaintJobEstimator(gallonprice,wallspace);
    system ("pause");
}

コンソールのスクリーンショットは次のとおりです。ここに画像の説明を入力

4

1 に答える 1

7

一部の計算でゼロを掛けています。たとえば、次のコード行では:

numBucket=numBucket+calc*(1/115);

1/115 を括弧に入れると、整数除算のためにゼロに評価されます。目的の効果を得るには、次のことを試してください。

numBucket = calc / 115.0f;
于 2012-11-14T22:51:30.923 に答える