Trouble with void functions, program not compiling correctly.

Mar 29, 2016 at 3:46pm
This is my first time creating a program using void functions. The program allows a user to enter an employees hours and pay rate to calculate gross pay. In the end, the program should display the total gross pay for all of the employees. However, my program displays the total gross pay for all employees as 0. Please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <iomanip>
using namespace std;

//function prototype
void calcGrossPay(int hours, double rate, double &pay);
double getTotalPay(double total, double pay);

//main
int main()
{
    
    char yesOrNo = ' ';
    int hoursWorked = 0;
    double payRate = 0.0;
    double grossPay = 0.0;
    double all = 0.0;
    double gPay = 0.0;
    
    cout << fixed << setprecision(2);
    
    do {
        //inputs
        cout << "Enter hours for employee: ";
        cin >> hoursWorked;
        cout << "Enter pay rate: ";
        cin >> payRate;
        
        calcGrossPay(hoursWorked, payRate, gPay);
        cout << "Gross pay for employee: " << gPay << endl;
        all = getTotalPay(all, grossPay);
        
        cout << "Calculate another employees gross pay? (Y or N): ";
        cin >> yesOrNo;
        toupper(yesOrNo);
        cout << endl;
    } while (toupper(yesOrNo) == 'Y');
    
    cout << "Total gross pay for all employees is: " << all << endl;
}

void calcGrossPay(int hours, double rate, double &pay)
{
    //calculate gross pay
    if (hours <= 40)
        pay = hours * rate;
    else
        pay = (rate * 40) + ((hours - 40) * 1.5 * rate);
}


double getTotalPay(double total, double pay)
{
    return total + pay;
}
Mar 29, 2016 at 4:14pm
There is a logical error.
But first, i would like some clarification. Here, what is the use of All and the difference between gPay and grosspPay?
Mar 29, 2016 at 4:15pm
Look at this snippet:
all = getTotalPay(all, grossPay);
Where do you ever assign a value, other than zero, to grossPay?
Mar 29, 2016 at 4:24pm
Roson, all is supposed to be the total gross pay for all of the employees. gPay is supposed to be a parameter for pay in the calcGrossPay function, and grossPay is supposed to be a parameter for pay in the getTotalPay function. Sorry if it's confusing, I'm having trouble understanding void functions AND value returning functions.

jlb, so should I change the getTotalPay function? I'm not exactly sure what to do.

EDIT: I fixed it. Thank you!
Last edited on Mar 29, 2016 at 4:28pm
Mar 29, 2016 at 4:31pm
so should I change the getTotalPay function?

No there is nothing wrong with the function, it is the values you're passing into the function.

I would recommend that you get rid of the gpay variable and just use the more descriptive grossPay variable instead.

1
2
3
        calcGrossPay(hoursWorked, payRate, grossPay);
        cout << "Gross pay for employee: " << gossPay << endl;
        all = getTotalPay(all, grossPay);

Topic archived. No new replies allowed.