Trouble calculating overtime hours worked, getting error

I'm creating a payroll program and one of the things I'm trying to do is determine whether or not the employee in question has worked any overtime hours, and if so, calculate how many they worked. My function on line 56 attempts to do this. The function calcOvrTmeHrs takes a Payroll object as a parameter, and starts by trying to determine if the employee has worked over 40 hours. Thats where I'm getting the error that says: Error": expected a'('
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 #include <string>
using namespace std;

class Payroll
{
public:

	static int count; //To keep track of number of employees calculated for

	Payroll(){} //Default constructor

	explicit Payroll(string& n, double& hw, double& wh) : //Constructor
		name(n), hrlyWge(hw), wrkdHrs(wh)
	{
		count++; //gets incremented for every constuctor call
	}
	

	~Payroll(){} //Destructor, if needed

	void setName(const string& name)
	{
		this->name = name;
	}

	string getName()
	{
		return name;
	}

	void setWage(const double& wage)
	{
		this->hrlyWge = wage;
	}

	double getWage()
	{
		return hrlyWge;
	}

	void setHours(const double& hours)
	{
		this->wrkdHrs = hours;
	}

	double getHours()
	{
		return wrkdHrs;
	}

	double calcBasePay()
	{
		return hrlyWge * wrkdHrs;
	}

	double calcOvrTmeHrs(Payroll& empObj) //Determines if overtime hours were worked, 
	{                                     //and if so, calculates how many were worked
		int ovrHrsWrkd; 

		if empObj.wrkdHrs > 40
		{
			for (int ohw = 40; ohw = wrkdHrs; ohw++)
			{
				ovrHrsWrkd++;
			}

			return ovrHrsWrkd; //Returns number of overtime hours worked if applicable
		}
	}

	double calcOvrTmePay(Payroll& empObj) //Calculates ovetime pay if any, for employee
	{
		return calcOvrTmeHrs(empObj) * ((empObj.hrlyWge / 2) + empObj.hrlyWge);
	}

	double calcEmpTotlPay(Payroll& empObj) //Adds up employees total pay earned
	{
		return empObj.calcBasePay() + calcOvrTmePay(empObj);
	}

	double calcTotlPayroll(Payroll& emp1, Payroll& emp2, Payroll& emp3) //Adds up amount paid to all employees combined
	{
		return (emp1.calcBasePay() + calcOvrTmePay(emp1)) + (emp2.calcBasePay() + calcOvrTmePay(emp2)) + (emp3.calcBasePay() + calcOvrTmePay(emp3));
	}

	double calcTotlHrsWrkd(Payroll& emp1, Payroll& emp2, Payroll& emp3) //Gets total number of hours worked for all employess combined
	{
		return (emp1.wrkdHrs + calcOvrTmeHrs(emp1)) + (emp2.wrkdHrs + calcOvrTmeHrs(emp2)) + (emp3.wrkdHrs + calcOvrTmeHrs(emp3));
	}

	double calcTotlOvrTmeHrs(Payroll& emp1, Payroll& emp2, Payroll& emp3) //Gets total number of overtime hours worked for all employees
	{
		calcOvrTmeHrs(emp1) + calcOvrTmeHrs(emp2) + calcOvrTmeHrs(emp3);
	}

private:
	string name;
	double hrlyWge;
	double wrkdHrs;


}; //End Payroll class  
Copy and paste the exact error message you are getting and please indicate which line it is referring to.

Also, you seem to have some misconceptions about classes - have you heard of the this pointer? You seem to have designed the functions to take their own instance as parameters. Also, the ones that deal with multiple instances should not exist (or should not be inside the class at all).
Thats one of the things that was confusing me, I can build it fine without any errors, but on line 60, there is a red line under empObj that reads Error: expected a '('

I'm not entirely sure what you mean by
You seem to have designed the functions to take their own instance as parameters.


Also, this is only my header file, the cpp file I haven't created yet where I would actually go and create the instances that are going to be used in the functions.
The reason you can build it fine without any errors is because the header is not being included by any source files yet so it is not even being compiled at all. Your red squiggly line is right - the conditions for if statements have to have round parens () around them. This isn't python ;)

As for my confusing statement, would you please explain to me why on lines 56, 71, and 76 you have that parameter?

Any why are the functions on lines 81-94 even in the class at all? Why do they even exist if they only complicate a simple task?
I can't believe I would forget something so simple like parentheses around conditions in an if statement. Thanks for helping me resolve that.

For lines 56, 71, 76, I was first going to create some Payroll objects in my cpp file and give them each some values, then I would pass the object to the desired function that I wanted to use. That's the gist of what I was attempting to do with those lines you mentioned.

I've been teaching myself C++ for about a month and a half now and I haven't really covered classes/objects extensively yet, so I must not be understanding something that I thought I did, regarding classes and passing objects as parameters.

As far as lines 81-94, now that I think about it, I probably could accomplish those same tasks by using my functions I already have. I probably don't even need those functions on lines 81-94. I'm not sure if that's what you were getting at when you mentioned those lines in your post, but that's just what came to mind when looking at the program again.
I think you should read about the this pointer:
http://www.cplusplus.com/doc/tutorial/templates/#this
So I made several corrections and finally got my program to work the way it should and give me the correct output. One question I had was, instead of having all the input and output done in main, shouldn't I be able to have functions that do that? Where I would simply have to call the getinput function from main and it would gather user input and store it in the array of objects. Then do the same thing for output. I tried having a member function in my Payroll class that gets user input, but I couldn't figure out how to call it properly, as well as get the input stored in the array.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <string>
using namespace std;

class Payroll
{
public:

	static int count; //To keep track of number of employees calculated for

	Payroll(){} //Default constructor

	explicit Payroll(string& n, double& hw, double& wh) : //Constructor
		name(n), hrlyWge(hw), wrkdHrs(wh)
	{
		count++; //gets incremented for every constuctor call
	}
	

	~Payroll(){} //Destructor, if needed

	

	void setName(const string& name)
	{
		this->name = name;
	}

	string getName()
	{
		return name;
	}

	void setWage(const double& wage)
	{
		this->hrlyWge = wage;
	}

	double getWage()
	{
		return hrlyWge;
	}

	void setHours(const double& hours)
	{
		this->wrkdHrs = hours;
	}

	double getHours()
	{
		return wrkdHrs;
	}

	double calcBasePay()
	{
		if (wrkdHrs <= 40.0)
		{
			return wrkdHrs * hrlyWge;
		}

		else
		{
			return hrlyWge * 40;
		}
		
		return hrlyWge * wrkdHrs;
	}

	double calcOvrTmeHrs() //Determines if overtime hours were worked, 
	{                                     //and if so, calculates how many were worked
		double ovrHrsWrkd(0); 

		if (wrkdHrs > 40.0)
		{
			for (double ohw = 41.0; ohw <= wrkdHrs; ohw++)
			{
				ovrHrsWrkd++;
				
			}//End for

			return ovrHrsWrkd; //Returns number of overtime hours worked if applicable

		} //End if

		else
		{
			return ovrHrsWrkd;
		}
	}

	double calcOvrTmePay() //Calculates ovetime pay if any, for employee
	{
		return calcOvrTmeHrs() * ((hrlyWge / 2) + hrlyWge);
	}

	double calcEmpTotlPay() //Adds up employees total pay earned
	{
		return calcBasePay() + calcOvrTmePay();
	}

private:
	string name;
	double hrlyWge;
	double wrkdHrs;

}; //End Payroll class 


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
#include <iostream>
#include <string>
#include <iomanip>
#include "Payroll.h"
using namespace std;

int main()
{
	Payroll Employees[3];
	
	string info;
	double num;

	for (int i = 0; i < 3; i++)
	{
		cout << "Enter the employee's name: ";
		getline(cin, info);
		Employees[i].setName(info);
		cout << endl;

		cout << "Enter the employee's hourly wage: ";
		cin >> num;
		Employees[i].setWage(num);
		cout << endl;

		cout << "Enter the empoloyee's worked hours: ";
		cin >> num;
		Employees[i].setHours(num);
		cout << endl;
		cin.ignore();

	} //End for

	for (int i = 0; i < 3; i++)
	{
		cout << "Name" << setw(20) << "= " << Employees[i].getName() << endl;
		cout << "Base Pay" << setw(16) << "= " << Employees[i].calcBasePay() << endl;
		cout << "Hours in Overtime" << setw(8) << "= " << Employees[i].calcOvrTmeHrs() << endl;
		cout << "Overtime Pay Amount" << setw(7) << "= " << Employees[i].calcOvrTmePay() << endl;
		cout << "Total Pay" << setw(15) << "= " << Employees[i].calcEmpTotlPay() << endl;
		cout << endl;
	}

	cout << setfill('%') << setw(50) << "%" << endl;
	cout << setfill(' ') << setw(15) << " " << "EMPLOYEE SUMMARY" << endl;
	cout << endl;
	cout << "Total Employee Salaries" << setw(6) << "= " << Employees[0].calcEmpTotlPay() + Employees[1].calcEmpTotlPay() 
		+ Employees[2].calcEmpTotlPay() << endl;
	cout << "Total Employee Hours" << setw(9) << "= " << Employees[0].getHours() + Employees[1].getHours() 
		+ Employees[2].getHours() << endl;
	cout << "Total Overtime Hours" << setw(9) << "= " << Employees[0].calcOvrTmeHrs() + Employees[1].calcOvrTmeHrs() 
		+ Employees[2].calcOvrTmeHrs() << endl;
	cout << endl;
	
	return 0;

} //End main 
You can have function for input and output if you want, but it is bad practice to do that. Instead, it is more proper to overload the >> and << operators instead.
Ok, I wasn't sure. Thanks LB for your help with this.
Topic archived. No new replies allowed.