Calculating overtime pay using a void function

I was given a program that used double functions to calculate overtime pay. Hours 1 to 37 gets paid their standard rate. Hours 38 to 50 gets paid time and a half. Hours over 50 get paid double their rate. The only thing I have to do is do all of the calculations I have already done, but use a void function instead of a double function. The problem is that every time I try to, I get an error that says you can't use a double in a void function. Here is the code:
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
#include <iostream>
#include <iomanip>
using namespace std;

//function prototypes
double getOvertime(double hours, double rate);

int main()
{
	double hoursWkd = 0.0;
	double payRate = 0.0;
	double overtime = 0.0;
	double grossPay = 0.0;

	cout << fixed << setprecision(2);

	cout << "Hours worked (negative number to end): ";
	cin >> hoursWkd;
	while (hoursWkd >= 0)
	{
		cout << "Pay rate: ";
		cin >> payRate;
		grossPay = hoursWkd * payRate;

		if (hoursWkd > 37)
		{
			overtime = getOvertime(hoursWkd, payRate);
			grossPay += overtime;
		}  //end if

		cout << "Gross pay: $" << grossPay << endl << endl;

		cout << "Hours worked (negative number to end): ";
		cin >> hoursWkd;
	}	//end while
	return 0;
}	//end of main function

//*****function definitions*****
double getOvertime(double hours, double rate)
{
	double extraPay = 0.0;

	extraPay = (hours - 37) * rate / 2;
	if (hours > 50)
		 extraPay += (hours - 50) * rate / 2;
	//end if
	return extraPay;

}	
//end of function definitions 
Last edited on
It's not clear from the code posted what changes were made to make the function void. If extraPay is no longer returned from the function at line 48, did you add it to the function parameters?
So, I actually looked at some code I wrote earlier regarding the temperature, and I re-formatted the code to do this. It actually works. It's kind of embarrassing that I have asked for help twice now and figured it out immediately after I posted. I apologize.

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
#include <iostream>
#include <iomanip>
using namespace std;

//function prototypes
void getOvertime(double hours, double rate, double &extraPay);

int main()
{
	double hoursWkd = 0.0;
	double payRate = 0.0;
	double overtime = 0.0;
	double grossPay = 0.0;

	cout << fixed << setprecision(2);

	cout << "Hours worked (negative number to end): ";
	cin >> hoursWkd;
	while (hoursWkd >= 0)
	{
		cout << "Pay rate: ";
		cin >> payRate;
		grossPay = hoursWkd * payRate;

		if (hoursWkd > 37)
		{
			getOvertime(hoursWkd, payRate, overtime);
			grossPay += overtime;
		}  //end if

		cout << "Gross pay: $" << grossPay << endl << endl;

		cout << "Hours worked (negative number to end): ";
		cin >> hoursWkd;
	}	//end while
	return 0;
}	//end of main function

	//*****function definitions*****
void getOvertime(double hours, double rate, double &extraPay)
{

	extraPay = (hours - 37) * rate / 2;
	if (hours > 50)
		extraPay += (hours - 50) * rate / 2;
	//end if
}
//end of function definitions 
EDITED POST: @jj245037 figured it out, already. :-)
Since you must use the void type of function, you need to pass by reference.
http://www.cplusplus.com/doc/tutorial/functions/

To give you some ideas,
Line 6: double getOvertime(double hours, double rate); , could be:
void getOvertime(double hours, double rate, double extraPay);

Line 27: overtime = getOvertime(hoursWkd, payRate);, could be:
getOvertime(hoursWkd, payRate, overtime);

Lines 40 - 50 : From:
1
2
3
4
5
6
7
8
9
10
11
double getOvertime(double hours, double rate)
{
	double extraPay = 0.0;

	extraPay = (hours - 37) * rate / 2;
	if (hours > 50)
		 extraPay += (hours - 50) * rate / 2;
	//end if
	return extraPay;

}	


To:
1
2
3
4
5
6
7
8
void getOvertime(double hours, double rate, double &extraPay)
{

	extraPay = (hours - 37) * rate / 2;
	if (hours > 50)
		extraPay += (hours - 50) * rate / 2;
	//end if
}


You should add an additional parameter on the function getOvertime to pass by reference the extraPay value to the calling function in main.
Last edited on
Topic archived. No new replies allowed.