help with a gross pay question

Im really confused with how to do the gross pay with the void function and how to calculate more then one time for more employees and calculate the total gross pay from all of the employees. And im not sure what a value-returning function is and i have to have that
Here is the problem
"you will be using a void function to determine an employee’s gross pay. Therefore you can use your solution to
Lab 9 - Intermediate as a starting point.
The payroll manager at Gerston Blankets wants a program that calculates and displays the gross pay for
each of the company’s employees. It also should calculate and display the total gross pay. The payroll
manager will enter the number of hours the employee worked and his or her pay rate. Employees
working more than 40 hours should receive time and one-half for the hours over 40. Use a void function
to determine an employee’s gross pay. Use a value-returning func-tion to accumulate the total gross
pay. The program should display the total gross pay only after the payroll manager has finished entering
the data for all the employees. Use a sentinel value to end the program."

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
56
57
58
59
60
61
  #include <iostream>  
#include <iomanip>
#include <cmath>  

using namespace std;

double getGrossPay(double hrsWrk, double payRate);

int main()
{
    // declare variables  
    double hrsWrk        = 0.0;
    double payRate       = 0.0;
    double totalGrossPay = 0.0;

    char   sentinel      = ' ';

    // prime the loop  
    cout << "Do you have hours to enter? Y/N  (N to stop): ";
    cin >> sentinel;

    while (toupper(sentinel) == 'Y')
    {
        cout << "Enter hours worked:\n";
        cin  >> hrsWrk;
        cout << "Enter pay rate:\n";
        cin  >> payRate;

        // call function for individual gross pay
        double grossPay = getGrossPay(hrsWrk, payRate);
        totalGrossPay += grossPay;
        

        // Display information  
        cout << fixed << setprecision(2);
        cout << "grossPay:$" << grossPay << endl;
        cout << "Do you have any more hours to enter? (Y/N):" << endl;
        cin  >> sentinel;
    }


    cout << "totalGrossPay:$" << totalGrossPay << endl;
}

double getGrossPay(double hrsWrk, double payRate)
{
    // calculates and returns the gross Pay 

    double grossPay = 0.0;
    
    if (hrsWrk <= 40)
    {
        grossPay = hrsWrk * payRate;
    }
    else
    {
        grossPay = (hrsWrk - 40) * (payRate * 1.5) + (40 * payRate);
    }
    
    return grossPay;
}
Last edited on
Hello BtheMan,

Your program works just fine, but not to the requirements of what is needed. The requirements are also a little vague.

The "getGrossPay" function needs to be a void function with "grossPay" pass as a third parameter and by reference,so that you can use that variable for the second function.

Maybe my mind went in the wrong direction here, but this is what I a thinking of:
1
2
3
4
double GetTotalGrossPy(double grossPay)
{
    return grossPay;
}


Then back in main you can add it to "totalGrossPay" as you did on line 31. Just need the second function call to do this. I did line 31 like this:totalGrossPay += GetTotalGrossPay(grossPay);. Since the function returns a value you can add that to "totalGrossPay".

Hope that helps,

Andy


Hello BTheMan,
An easy way is to do what "Handy Andy" said about references.

Use a void function that takes "amountOfHoursWorked", "payRate" and a vector (take the vector by reference). Use a "push_back" in to the vector because you are not sure of the number of workers the manager is going to input.

Once the manager is done filling in the gross pay of each employee, use a totalGrossPay() function that takes only the vector (it's not important that you take it by reference) and iterates through each element and adds them to a "grossPay" variable which it finally returns. Enough talk!! Let's code...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Function for individual employees
void employeeGross(double hrsWrk, double payRate, vector<double> &list){
    if (hrsWrk <= 40)
    { list.push_back(hrsWrk * payRate); }
    else
    { list.push_back((hrsWrk - 40) * (payRate * 1.5) + (40 * payRate)); }
}

// Function for totalGrossPay
double totalGrossPay(vector<double> sample){
	double gross = 0.0;

	for(int i = 0; i < sample.size(); ++i){
		gross += sample[i];
	}

	return gross;
}


Don't put the totalGrossPay function inside the loop (duh!!). Finally, do the displaying.
I'm sure that solves your problem. Have a nice day!
Last edited on
Topic archived. No new replies allowed.