简体   繁体   English

C ++程序不读取公式

[英]C++ Program not reading formulas

My program I have been working on is supposed to output the following: * The number of gallons of paint required * The hours of labor required * The cost of the paint * The labor charges * The total cost of the paint job However, it displays 0 in every field.. What have I done wrong now? 我一直在从事的程序应该输出以下内容:*所需的油漆加仑数*所需的工时*油漆的成本*人工费用*油漆工作的总成本每个字段都为0。我现在做错了什么? Your help would be greatly appreciated. 您的帮助将不胜感激。

Here is my code: 这是我的代码:

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

Here is a screenshot of the console: 这是控制台的屏幕截图: 在此处输入图片说明

You're multiplying by zero in some of the calculations. 在某些计算中,您要乘以零。 For example, in the following line of code: 例如,在下面的代码行中:

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

You put 1/115 in parenthesis, which evaluates to zero because of integer division. 您将1/115放在括号中,由于整数除法,该括号的值为零。 To achieve the desired effect, try: 为了达到预期的效果,请尝试:

numBucket = calc / 115.0f;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM