Paint Job Estimator C ++

时间:2017-10-19 02:45:34

标签: c++ function

嗨,我正在为一所学校的项目工作,并且不能为我的生活找出如何使totalJobCost功能起作用。其他函数没有问题,但我不认为他们正在将var传递回main,以便totalJobCost抓取,因为totalJobCost输出0.这是我正在使用的代码:

#include "stdafx.h"
#include <iostream>

using namespace std;

void space(double paintarea, double paintcost, double paintneeded, double totalpaint);
void cost(double hrs, double hrcost, double spacetopaint);
void totalJobCost(double allTheirPaintCost, double allTheirWages, double theirTotalJobCost);


const double AREA_FORMULA = 220.00;
const double AREAFORMULA_PAINT = 1.00;
const double AREAFORMULA_HOURS = 8.00;
const double AREAFORMULAHOURS_WAGES = 35.00;

int main()
{
    double areaTP;
    double paintCST = 0;
    double paintNeeded = 0;
    double allPaintCost = 0;
    double hoursNeeded = 0;
    double hoursWages = 0;
    double allWages = 0;
    double allJobCost = 0;

    cout << "Enter the square footage you need to paint, then press enter" << endl;
    cin >> areaTP;

    cout << "Enter the price by gallons of paint you will use, then press enter" << endl;


    cin >> paintCST;
    while (paintCST < 10)
    {
        cout << "Enter the price by gallons of paint you will use, then press enter. cannot be less than 10 :";
        cin >> paintCST;
    }

    space(areaTP, paintCST, paintNeeded, allPaintCost);

    cost(hoursNeeded, hoursWages, areaTP);


    totalJobCost(allPaintCost, hoursWages, allJobCost);


    system("Pause");

    return 0;
}

void space(double paintarea, double paintcost, double paintneeded, double totalpaint)
{

    paintneeded = paintarea / AREA_FORMULA * AREAFORMULA_PAINT;
    totalpaint = paintneeded * paintcost;

    cout << "How many gallons of paint you will need: " << paintneeded << endl;
    cout << "Your total paint cost will be: " << totalpaint << endl;
}

void cost(double hrs, double hrcost, double spacetopaint)
{

    hrs = (spacetopaint / AREA_FORMULA) * AREAFORMULA_HOURS;
    hrcost = hrs * AREAFORMULAHOURS_WAGES;

    cout << "The number of hours for the job will be: " << hrs << endl;
    cout << "The total amount of wages will be: " << hrcost << endl;

}

void totalJobCost(double totalpaint, double hrcost, double theirTotalJobCost)
{                        
    theirTotalJobCost =  totalpaint + hrcost;

    cout << "The total price of your paint job will be: " << theirTotalJobCost << endl;
}

2 个答案:

答案 0 :(得分:1)

您需要声明您的参数(totalpainthrcost)作为参考。

目前,函数space()cost()只需在调用时复制totalpainthrcost,更新它们,然后打印它们。但是当函数返回时,totalpainthrcost中存储的值将丢失。

要解决此问题,您应该按如下方式声明这些功能:

void space(double paintarea, double paintcost, double paintneeded, double& totalpaint)

void cost(double hrs, double& hrcost, double spacetopaint)

现在,当totalpainthrcost对其进行操作时,您在space()cost()传递的任何变量都会更新。

答案 1 :(得分:1)

这是一个值传递值与传递引用问题。

  

在C ++中,布尔值,字符,整数,浮点数,   数组,类 - 包括字符串,列表,字典,集合,堆栈,   队列和枚举是值类型,而引用和指针   是参考类型。

CPP reference

您使用的变量是双精度(双精度浮点数),因此它们是值类型。将值类型变量作为参数传递给函数时,变量的当前值将复制到所调用函数的调用堆栈中。进入函数后,参数名称只是用于访问复制值的名称。无论您对这些复制值执行什么操作都不会影响传递给函数的原始变量的值。阅读函数范围和C / C ++的调用堆栈架构以了解更多信息。

要在函数调用之间更改变量的值,您需要将引用传递给它在内存中的位置。如果在函数的前几行中声明一个变量,它在内存中的位置将是该函数调用堆栈的一部分,并且您可以在原始函数中调用的任何函数调用中安全地访问该内存。所以你可以这样做:

int main() {
    double variable = 0;
    function(&variable);
    cout << variable;
}

void function(double* variable_address) {
    *variable_address = 1.5;
}

这涉及解除引用运算符。很抱歉,如果这是太多的信息,但如果你知道C / C ++的底层函数调用和内存架构中发生了什么,那么通过引用传递和按值传递会更容易理解。