Overtime Pay

Hello all, I am working on an assignment for school and am a bit stuck. We have to edit a basic pay calculator to include Overtime. And we have to use functions. Specifically:
void getEmpData(string &name, float &hours, float &rate)
float calEmpPay (float hours, float rate)
void dispEmpPay(name, pay)

He did not say what the overtime rate is, nor if we need to get that as input from the user. So I added it to my getEmpData function. I am not sure what to put in the parentheses in my function main,and don't know how to calculate the overtime. I'm also not sure about my void functions correctness.

I am pretty confused on functions still, and would appreciate any tips or direction. Thank you in advance!

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

void getEmpData (string& name, float& hours, float& rate, float& overtimeRate);
float calEmpPay (float hours, float rate, float overtimeRate);
void dispEmplPay (string name, float pay);

int main()
{
	getEmpData ();

	calEmpPay ();

	dispEmplPay ();

system("pause");

return 0;
}


void getEmpData (string& name, float& hours, float& rate, float& overtimeRate)
{
	cout << "What is your name?";
	cin >> name;
	cout << "How many hours did you work? ";
	cin >> hours;
	cout << "How much do you get paid per hour? ";
	cin >> rate;
	cout << "How much do you get paid per hour overtime?";
	cin >> overtimeRate;
	cout << endl;
}

float calEmpPay (float hours, float rate, float overtimeRate)
{
	float pay

		if (hours <= 40)
			pay = hours * rate
		else 
			pay = 
}


void dispEmplPay (name, pay)
{
	cout << name << ", You have earned $" << pay << endl;
}
In your getEmpData function you are accepting 4 arguments by reference (they are using & so they all keep their value when it exits the function). If you need any more explanation on that I would look up "passing by value" vs "passing by reference". Those values need to actually be declared somewhere in your program so you need to pass those in from main. A quick example would be something like this.
1
2
3
4
5
6
7
8
9
int main()
{
	string name = "";
	float hours = 0;
	float rate = 0;
	float overtimeRate = 1.25;   // Should probably be entered by user later on 

	getEmpData(name, hours, rate, overtimeRate);  // Pass them all to this function 
}


It is always good to initialize your variables to some default value just in case.

As for your calEmpPay function it will take in 3 arguments (so it will be fairly similar to the getEmpData call I showed above) and it has a return value of float. So at some point you will need to add "return pay" at the end of that function and do
float Pay = calEmpPay(/* Values */) in your main function to access that value.

Will let you try and figure out the rest but that should give you a good start.
Thank you! Very helpful. I am still trying to figure out how to calculate the pay, am I on the right track? If so, how do I reconcile the formal paremeter to my actual parameters now that I have added the regularpay and overtime pay?
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
int main()
{
	string name = "";
	float hours = 0;
	float rate = 0;
	float overtimeRate = 0;
	float pay = calEmpPay (hours, rate, overtimeRate);

	getEmpData (name, hours, rate, overtimeRate);
	
	calEmpPay (hours, rate, overtimeRate);

	dispEmplPay (name, pay);

system("pause");

return 0;
}


void getEmpData (string& name, float& hours, float& rate, float& overtimeRate)
{
	cout << "What is your name?";
	cin >> name;
	cout << "How many hours did you work? ";
	cin >> hours;
	cout << "How much do you get paid per hour? ";
	cin >> rate;
	cout << "How much do you get paid per hour overtime?";
	cin >> overtimeRate;
	cout << endl;
}

float calEmpPay (float hours, float rate, float overtimeRate)
{
	float pay = calEmpPay(hours, rate, overtimeRate);
	float regularPay;
	float overtimePay;

		for (hours <= 40)
			regularPay = (hours * rate);
		for (hours > 40)
			hours = 40;
			regularPay = (hours * rate);
			overtimePay = (hours - 40) * overtimeRate;
		
		pay = regularPay + overtimePay;
}


void dispEmplPay (name, pay)
{
	cout << name << ", You have earned $" << pay << endl;
}
Last edited on
float pay = calEmpPay (hours, rate, overtimeRate);
When you assign a variable equal to a function it calls that function at the same time (so you don't need to repeat it again later). However it should be placed after the getEmpData function of course to get all of the data.

1
2
3
 for (hours <= 40)
regularPay = (hours * rate);
for (hours > 40)

You were fine with if/else statements. For loops are for repeating a certain piece of code over and over, not what you want to do here. Also you are setting hours equal to 40 in your second case so "hours - 40" is always going to give you 0 which is a bad idea.

You still aren't returning anything from your calEmpPay function. I would read most of http://www.cplusplus.com/doc/tutorial/functions/ very carefully as it will explain it in more detail than I can here. Especially the examples with the "return" keyword used.

There are other minor things you are missing that I am sure you can see if you look through carefully (such as your dispEmplPay function arguments have no types in front of them).
Thank you for the link, unfortunately I am a procrastinator and this assignment is due at midnight. But I think I almost have it. The only build errors I see are "error: no operator "<<" matches these operands. One is the first cin >> in the getEmpData funtion, and the other is the last cout << in the dispEmpPay function.

As for calculating overtime, I went back to if else. But I do need to differentiate the overtime rate from the regular rate being applied to the first 40 hours as opposed to the overtime hours. Is there a different way I should be doing it?

I really appreciate your help on this!

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
int main()
{
	string name = "";
	float hours = 0;
	float rate = 0;
	float overtimeRate = 0;
	float pay = 0;

	getEmpData (name, hours, rate, overtimeRate);
	
	calEmpPay (hours, rate, overtimeRate);

	dispEmplPay (name, pay);

system("pause");

return 0;
}


void getEmpData (string& name, float& hours, float& rate, float& overtimeRate)
{
	cout << "What is your name?";
	cin >>[/b] name;
	cout << "How many hours did you work? ";
	cin >> hours;
	cout << "How much do you get paid per hour? ";
	cin >> rate;
	cout << "How much do you get paid per hour overtime?";
	cin >> overtimeRate;
	cout << endl;
}

float calEmpPay (float hours, float rate, float overtimeRate)
{
	float hours;
	float rate;
	float overtimeRate;
	float pay;

		if (hours <= 40)
			pay = (hours * rate);
		else 
			pay = (40 * rate) + (hours - 40)*overtimeRate;
		return pay;
}


void dispEmplPay (string& name, float& pay)
{
	string name;
	float pay;

	cout << name << ", You have earned $" << pay << endl;
}
Take this as a lesson to try and get work done earlier (especially programming which always ends up taking longer than it should ;) )

On line 11 change it to
pay =calEmpPay (hours, rate, overtimeRate);
or else the pay variable in main will always be equal to 0. (You change a pay variable in the calEmpPay function but that is not the same variable as seen in main. I may not be explaining that in the best way but I would definitely read that link when you have the chance.

cin >>[/b] name;
typo?

You don't need to redefine hours/rate/overtimeRate in your calEmpPay function as you are passing them from main (you do need pay to stay in that function though as you are going to be returning that). Same with dispEmplPay you already declared name/pay in your main function so you don't need to redeclare them in the function.

Your overtime pay calculations look good to me. Try entering in some values and see if they give logical results.
Last edited on
Agreed. This took WAY longer than I thought it would! lol

Okay so I made the changes you suggested, but it still won't build. I am getting that squiggly red line under two of my double carrots '<<'; the first (cin >>) in the getEmpData funtion, and the other is the last (cout <<) in the dispEmpPay function. The message it gives is "error: no operator "<<" matches these operands." Do you know how I might fix that? I haven't the slightest idea what is wrong with it. :(

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

void getEmpData (string& name, float& hours, float& rate, float& overtimeRate);
float calEmpPay (float hours, float rate, float overtimeRate);
void dispEmplPay (string& name, float& pay);

int main()
{
	string name = "";
	float hours = 0;
	float rate = 0;
	float overtimeRate = 0;
	float pay = 0;

	getEmpData (name, hours, rate, overtimeRate);
	
	pay = calEmpPay (hours, rate, overtimeRate);

	dispEmplPay (name, pay);

system("pause");

return 0;
}


void getEmpData (string& name, float& hours, float& rate, float& overtimeRate)
{
	cout << "What is your name?";
	cin >> name;
	cout << "How many hours did you work? ";
	cin >> hours;
	cout << "How much do you get paid per hour? ";
	cin >> rate;
	cout << "How much do you get paid per hour overtime?";
	cin >> overtimeRate;
	cout << endl;
}

float calEmpPay (float hours, float rate, float overtimeRate)
{
	float pay;

		if (hours <= 40)
			pay = (hours * rate);
		else 
			pay = (40 * rate) + (hours - 40)*overtimeRate;
		return pay;
}


void dispEmplPay (string& name, float& pay)
{
	cout << name << ", You have earned $" << pay << endl;
}
Add #include <string> to the top of your file as it has no idea what it is right now.
Thank you very much for your help! My program builds perfectly now :)
Topic archived. No new replies allowed.