2012-11-14 37 views
0

我的節目,我一直在努力應該輸出如下: *爲必填油漆加侖數 *爲必填勞動時間 *油漆 *勞動力成本收費 *油漆作業的總成本 但是,它在每個領域顯示0 ..我現在做錯了什麼? 您的幫助將不勝感激。C++程序不讀公式

這裏是我的代碼:

//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"); 
} 

這裏是控制檯的截圖:enter image description here

+0

'paint.cc:34:12:錯誤:「::主」必須返回「int'' –

回答

7

你零中的一些計算倍增。例如,在以下代碼行中:

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

您將1/115放在括號中,由於整數除法計算爲零。爲了達到預期的效果,請嘗試:

numBucket = calc/115.0f; 
+0

太感謝您了! –