Calculating Employees Paycheck

Apr 21, 2016 at 6:02am
I have been trying to figure out how to do the problem below:

Write a program that will record information about employees and compute their paychecks. Each employee must be represented by a struct containing the last name of the employee, the hours worked each day of the week, the hourly rate, and the pay for that week. The "hours worked each day of the week" must be an array storing five values for the hours from Monday to Friday. (The company is not open on Saturday or Sunday.) Therefore, this array will be inside the struct. There will also be an array of all the employees (an array of structs). There must be at least four employees.
The array of structs must be declared in the main function of the program. Then the entire array must be passed to a function called "initialize", which will ask the user to enter values for every part of every struct, except for the pay for that week (which will be computed in a different function).
A loop for processing the array of employees must be set up in the main function. Inside the loop, a single employee will be passed into a function called "compute", which will calculate the paycheck for that employee. If the employee worked longer than 40 hours, overtime pay of 1.5 times the hourly rate is to be used for each hour worked over the 40 hours. Do not pass the entire array of employees into the "compute" function until all of their paychecks have been calculated, at which time the loop will terminate. The employee will need to be passed by reference.
A single employee must be passed into the "result" function, which will output the last name of the employee and the amount of the paycheck for that employee. Do not pass the entire array of employees into the "result" function. Each employee must be passed through call by value. one at a time, until all the paycheck amounts have been calculated, at which time the loop (in the main function) will terminate. Then the program will end.

Here is what I have so far. I am having trouble with the whole array of structs and how I would deal with all the calculations.

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
#include <iostream>
#include <string>

using namespace std;

int initiallize();
int compute();
int restult;


struct Employee
{
	string lastName;
	double hours[5];
	double rate, pay;
};


int main()
{
	Employee * pointer = new Employee[6];

	for( int j =1, j < 7, j = j +1)
	{
		
	}
}

int initiallize
{
	string day;

	cout << "Enter the last name of the Employee: " << endl;
	cin >> lastName;

	for(int i =1, i < 6, i = i +1)
	{
		if(i = 1)
			day = 'Monday';
		else if(i = 2)
			day = 'Tuesday';
		else if(i = 3)
			day = 'Wednesday';
		else if(i = 4)
			day = 'Thursday';
		else if(i = 5)
			day = 'Friday';

		cout << "Enter the hours worked for " << day << ": " << endl;
		cin >> hours[i];
	}

	cout << "Enter the rate of pay: " << endl;
	cin >> rate;
}

int compute
{

}

int result
{

}

Last edited on Apr 21, 2016 at 4:01pm
Apr 21, 2016 at 8:35am
I would do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void calculate(Employee& emp)
{
   // your code here
}

void result (Employee emp)
{
   // your code here
}
int main()
{
   Employee Employees[6];

   initialize(Employees);
   for (int i = 0; i < 6; i++)
  {
     calculate(Employees[i]);
     result(Employees[i]);
  }
}
Apr 21, 2016 at 7:57pm
So I tried to follow what you said but I get the following errors when I compile:

paycheck2.cpp:18:9: error: use of undeclared identifier 'lastName'
cin >> lastName;
^
paycheck2.cpp:21:9: error: use of undeclared identifier 'rate'
cin >> rate;
^
paycheck2.cpp:27:4: error: use of undeclared identifier 'day'
day = "Monday";
^
paycheck2.cpp:31:4: error: use of undeclared identifier 'day'
day = "Tuesday";
^
paycheck2.cpp:35:4: error: use of undeclared identifier 'day'
day = "Wednesday";
^
paycheck2.cpp:39:4: error: use of undeclared identifier 'day'
day = "Thursday";
^
paycheck2.cpp:43:4: error: use of undeclared identifier 'day'
day = "Friday";
^
paycheck2.cpp:46:44: error: use of undeclared identifier 'day'
cout << "Enter the hours worked for " << day << ": " << endl;
^
paycheck2.cpp:47:10: error: use of undeclared identifier 'hours'
cin >> hours[i];
^
paycheck2.cpp:49:29: error: use of undeclared identifier 'hours'
sumofhours = sumofhours + hours[i];
^
paycheck2.cpp:58:5: error: use of undeclared identifier 'sumofhours'
if(sumofhours > 40)
^
paycheck2.cpp:60:14: error: use of undeclared identifier 'sumofhours'
overtime = sumofhours - 40;
^
paycheck2.cpp:61:16: error: use of undeclared identifier 'sumofhours'
regularpay = sumofhours - overtime;
^
paycheck2.cpp:63:3: error: use of undeclared identifier 'pay'
pay = (overtime * (1.5 * rate)) + (regularpay * rate);
^
paycheck2.cpp:63:28: error: use of undeclared identifier 'rate'
pay = (overtime * (1.5 * rate)) + (regularpay * rate);
^
paycheck2.cpp:63:51: error: use of undeclared identifier 'rate'
pay = (overtime * (1.5 * rate)) + (regularpay * rate);
^
paycheck2.cpp:65:10: error: use of undeclared identifier 'sumofhours'
else if(sumofhours !> 40)
^
paycheck2.cpp:67:3: error: use of undeclared identifier 'pay'
pay = sumofhours * rate;
^
paycheck2.cpp:67:9: error: use of undeclared identifier 'sumofhours'
pay = sumofhours * rate;


Here is my 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
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
#include <iostream>
#include <string>

using namespace std;

struct Employee
{
	string lastName;
	double hourse[5];
	double rate, pay;
};

void initialize(Employee& emp)
{
	double sumofhours;

	cout << "Enter the last name of the employee: " << endl;
	cin >> lastName;

	cout << "Enter the rate of pay for this employee: " << endl;
	cin >> rate;

	for(int i = 1; i < 6; i++)
	{
		if(i == 1)
		{
			day = "Monday";
		}	
		else if(i == 2)
		{
			day = "Tuesday";
		}	
		else if(i == 3)
		{
			day = "Wednesday";
		}
		else if(i == 4)
		{
			day = "Thursday";
		}
		else if(i == 5)
		{
			day = "Friday";
		}

		cout << "Enter the hours worked for " << day << ": " << endl;
		cin >> hours[i];

		sumofhours = sumofhours + hours[i];
	}
}

void compute(Employee& emp)
{
	double overtime;
	double regularpay;

	if(sumofhours > 40)
	{
		overtime = sumofhours - 40;
		regularpay = sumofhours - overtime;

		pay = (overtime * (1.5 * rate)) + (regularpay * rate);
	}
	else if(sumofhours !> 40)
	{
		pay = sumofhours * rate;
	}

	return pay;
}

void result(Employee emp)
{
	cout << "Employee " << lastName << "'s " << "pay is " << pay;
}

int main()
{
	Employee * pointer = new Employees[4];

	initialize(Employees);

	for(int i = 0; i < 5; i++)
	{
		compute(Employees[i]);
		result(Employees[i]);
	}

	return 0;
}


I think my problem is with the parameters of the functions. I really don't understand what to put in there.
Apr 21, 2016 at 8:27pm
You need to use the right variables. for example
paycheck2.cpp:18:9: error: use of undeclared identifier 'lastName'
cin >> lastName;
must be cin >> emp.lastName. That's the reason why you pass the Employee variable as reference.
Same with
paycheck2.cpp:21:9: error: use of undeclared identifier 'rate'
cin >> rate;
paycheck2.cpp:27:4: error: use of undeclared identifier 'day'
day = "Monday";

For that you need a string variable inside the initialize function
Apr 23, 2016 at 6:26am
Okay so I spent hours trying to figure this out and I finally got it to compile without any errors. However, there is still something wrong and I can't seem to figure out what it is. I think I have spent to much time on it already that I am missing something obvious that I just can't catch. Your help would be much appreciated.

Here is my 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
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
#include <iostream>
#include <string>

using namespace std;

struct Employee
{
	string lastName;
	double hours[5];
	double rate, pay;
};

void initialize(Employee& emp)
{
	string day;

	cout << "Enter the last name of the employee: " << endl;
	cin >> emp.lastName;

	cout << "Enter the rate of pay for this employee: " << endl;
	cin >> emp.rate;

	for(int i = 1; i < 6; i++)
	{
		if(i == 1)
		{
			day = "Monday";
		}	
		else if(i == 2)
		{
			day = "Tuesday";
		}	
		else if(i == 3)
		{
			day = "Wednesday";
		}
		else if(i == 4)
		{
			day = "Thursday";
		}
		else if(i == 5)
		{
			day = "Friday";
		}

		cout << "Enter the hours worked for " << day << ": " << endl;
		cin >> emp.hours[i];
	}
}

void compute(Employee& emp)
{
	double overtime, regularpay, sumofhours;

	for(int j = 0; j < 6; j++)
	{
		sumofhours = sumofhours + emp.hours[j];
	}

	if(sumofhours > 40)
	{
		overtime = sumofhours - 40;
		regularpay = sumofhours - overtime;

		emp.pay = (overtime * (1.5 * emp.rate)) + (regularpay * emp.rate);
	}
	else if(sumofhours <= 40)
	{
		emp.pay = sumofhours * emp.rate;
	}

	cout << emp.pay << endl;
}

void result(Employee& emp)
{
	cout << "Employee " << emp.lastName << "'s " << "pay is " << emp.pay;
}

void initialize(Employee& emp);
void compute(Employee& emp);
void result(Employee& emp);

int main()
{
	Employee Employees[5];

	for(int i = 0; i < 5; i++)
	{
		initialize(Employees[4]);
		compute(Employees[i]);
		result(Employees[i]);
	}



	return 0;
}


Here is the output:


Enter the last name of the employee: 
Smith
Enter the rate of pay for this employee: 
10
Enter the hours worked for Monday: 
5
Enter the hours worked for Tuesday: 
5
Enter the hours worked for Wednesday: 
5
Enter the hours worked for Thursday: 
5
Enter the hours worked for Friday: 
5
0
Employee 's pay is 0
Enter the last name of the employee: 
Jones
Enter the rate of pay for this employee: 
10
Enter the hours worked for Monday: 
5
Enter the hours worked for Tuesday: 
5
Enter the hours worked for Wednesday: 
5
Enter the hours worked for Thursday: 
5
Enter the hours worked for Friday: 
5
0
Employee 's pay is 0
Enter the last name of the employee: 
Grant
Enter the rate of pay for this employee: 
10
Enter the hours worked for Monday: 
5
Enter the hours worked for Tuesday: 
5
Enter the hours worked for Wednesday: 
5
Enter the hours worked for Thursday: 
5
Enter the hours worked for Friday: 
5
0
Employee 's pay is 0
Enter the last name of the employee: 
Gomez
Enter the rate of pay for this employee: 
10
Enter the hours worked for Monday: 
5
Enter the hours worked for Tuesday: 
5
Enter the hours worked for Wednesday: 
5
Enter the hours worked for Thursday: 
5
Enter the hours worked for Friday: 
5
0
Employee 's pay is 0
Enter the last name of the employee: 
Brown
Enter the rate of pay for this employee: 
10
Enter the hours worked for Monday: 
5
Enter the hours worked for Tuesday: 
5
Enter the hours worked for Wednesday: 
5
Enter the hours worked for Thursday: 
5
Enter the hours worked for Friday: 
5
125
Employee Brown's pay is 125

Apr 23, 2016 at 8:22am
In void initialize(Employee& emp) your array index is wrong.
1
2
for(int i = 1; i < 6; i++)
cin >> emp.hours[i];

Same in
1
2
3
4
5
void compute(Employee& emp)
for(int j = 0; j < 6; j++)
{
   sumofhours = sumofhours + emp.hours[j];
}

Array index starts with zero and and end with size - 1

If you create a pointer with new you should delete it at the end of main.
However your assignment doesn't say anything about using pointers.

Apr 24, 2016 at 6:03pm
Thank you very much! I learned a lot from this assignment and with your help I was able to identify the problems with my original code. Here is how the code looks now:

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
#include <iostream>
#include <string>

using namespace std;

struct Employee
{
	string lastName;
	double hours[5];
	double rate, pay;
	double totalhours; /* I added the total hours worked to the struct so that I could pass 
	 					  it to the compute function using the the dot operator. I still am a little fuzzy on function parameters so
	 					  I decide to do it this way instead. */
};

void initialize(Employee& emp)
{
	string day;

	cout << "Enter the last name of the employee: ";
	cin >> emp.lastName;

	cout << "Enter the rate of pay for this employee: ";
	cin >> emp.rate;

	for (int i = 0; i < 5; i++)
	{
		if (i == 0)
		{
			day = "Monday";
		}	
		else if (i == 1)
		{
			day = "Tuesday";
		}	
		else if (i == 2)
		{
			day = "Wednesday";
		}
		else if (i == 3)
		{
			day = "Thursday";
		}
		else if (i == 4)
		{
			day = "Friday";
		}

		cout << "Enter the hours worked for " << day << ": ";
		cin >> emp.hours[i];

		emp.totalhours = emp.totalhours + emp.hours[i]; // total hours are now calcuated here
	}
}

void compute(Employee& emp) // I took the for loop out of this function as I now calculated total hours in the function initialize
{
	double regularhours, overtime;

	if (emp.totalhours > 40) // total hours are now passed with the dot operator emp.
	{
		overtime = emp.totalhours - 40;
		regularhours = emp.totalhours - overtime;
		emp.pay = (overtime * (1.5 * emp.rate)) + (regularhours * emp.rate);
	}
	else if (emp.totalhours <= 40)
	{
		emp.pay = emp.totalhours * emp.rate;
	}
}

void result(Employee& emp)
{
	cout << "Employee " << emp.lastName << "'s " << "pay is: " << emp.pay << endl;
	cout << endl;
}

void initialize(Employee& emp);
void compute(Employee& emp);
void result(Employee& emp);

int main()
{
	Employee Employees[4]; // I removed the pointer here

	for(int k = 0; k < 4; k++) // I fixed the array index here and in all other for loops
	{
		initialize(Employees[k]);
		compute(Employees[k]);
		result(Employees[k]);
	}

	return 0;
}


And here is the output:


Enter the last name of the employee: Velazquez
Enter the rate of pay for this employee: 10
Enter the hours worked for Monday: 10
Enter the hours worked for Tuesday: 10
Enter the hours worked for Wednesday: 10
Enter the hours worked for Thursday: 10
Enter the hours worked for Friday: 10
Employee Velazquez's pay is: 550

Enter the last name of the employee: Jones
Enter the rate of pay for this employee: 10
Enter the hours worked for Monday: 5
Enter the hours worked for Tuesday: 5
Enter the hours worked for Wednesday: 5
Enter the hours worked for Thursday: 5
Enter the hours worked for Friday: 5
Employee Jones's pay is: 250

Enter the last name of the employee: Brown
Enter the rate of pay for this employee: 10
Enter the hours worked for Monday: 6
Enter the hours worked for Tuesday: 6
Enter the hours worked for Wednesday: 6
Enter the hours worked for Thursday: 6
Enter the hours worked for Friday: 6
Employee Brown's pay is: 300

Enter the last name of the employee: Smith
Enter the rate of pay for this employee: 20
Enter the hours worked for Monday: 7
Enter the hours worked for Tuesday: 7
Enter the hours worked for Wednesday: 7
Enter the hours worked for Thursday: 7
Enter the hours worked for Friday: 7
Employee Smith's pay is: 700
Topic archived. No new replies allowed.