Fixing code

When running the code, the output displays the same numbers for the net pay, average and total. What am I doing wrong?

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


int main()
{
	int ID, emp_count = 0;
	double hourly_wage, hours_worked, overtime_pay, gross_salary, net_pay, avg_amount, payroll = 0; 
	char answer = 'n';

	do
	{
		cout << "Enter employee ID: " << endl;
		cin >> ID;

		cout << "Enter number of hours worked: " << endl;
		cin >> hours_worked;

		cout << "Enter hourly wage: " << endl;
		cin >> hourly_wage;

		if (hours_worked > 40)

		{
			overtime_pay = hours_worked * 1.5 * hourly_wage;
			gross_salary = overtime_pay - (hours_worked * hourly_wage);
			net_pay = gross_salary - (3.625 / gross_salary * 100);
		
		}

		else
		{
		gross_salary = hours_worked * hourly_wage;
		net_pay = gross_salary - (3.625 / gross_salary * 100);
			
		}
		avg_amount = payroll / emp_count;
		payroll += net_pay;
		emp_count++;
		

		cout << "Employee ID: " << ID << endl;
		cout << "Net pay: " << "$" << net_pay << endl;
		cout << "Average amount paid: " << "$" << avg_amount << endl;
		cout << "Total payroll: " << "$" << payroll << endl;


	 cout << "Do you want to enter another employee? (y/n)?" << endl;
		cin >> answer;


	} while (answer == 'y');

	

	return 0;
}

This is my output window:

Enter employee ID:
100
Enter number of hours worked:
45
Enter hourly wage:
15
Employee ID: 100
Net pay: $336.426
Average amount paid: $-1.#IND- I need help fixing this as well.
Total payroll: $336.426
Do you want to enter another employee? (y/n)?
y
Enter employee ID:
200
Enter number of hours worked:
30
Enter hourly wage:
13
Employee ID: 200
Net pay: $389.071
Average amount paid: $336.426
Total payroll: $725.496
Do you want to enter another employee? (y/n)?

Last edited on
I'm guessing the issue is this.

1
2
3
avg_amount = payroll / emp_count;
payroll += net_pay;
emp_count++;


You are trying to calculate the average amount before you calculate payroll and increment emp_count.


You should have
1
2
3
payroll += net_pay;
emp_count++;
avg_amount = payroll / emp_count;
Try printing the hours worked and hourly wage. I think you'll find that they aren't changing.

The problem with average amt paid being funny is because you're dividing by emp_count which is zero the first time through the loop. Increment emp_count before you divide.
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
#include <iostream>
#include <string>
using namespace std;


int main()
{
	int ID, emp_count = 0;
	double hourly_wage, hours_worked, overtime_pay, gross_salary, net_pay, avg_amount, payroll = 0; 
	char answer = 'n';

	do
	{
		cout << "Enter employee ID: " << endl;
		cin >> ID;

		cout << "Enter number of hours worked: " << endl;
		cin >> hours_worked;

		cout << "Enter hourly wage: " << endl;
		cin >> hourly_wage;

		if (hours_worked > 40)

		{
			overtime_pay = hours_worked * 1.5 * hourly_wage;
			gross_salary = overtime_pay - (hours_worked * hourly_wage);
			net_pay = gross_salary - (3.625 / gross_salary * 100);
		
		}

		else
		{
		gross_salary = hours_worked * hourly_wage;
		net_pay = gross_salary - (3.625 / gross_salary * 100);
			
		}
		payroll += net_pay; 
		emp_count++;
		avg_amount = payroll / emp_count;
		

		cout << "Employee ID: " << ID << endl;
		cout << "Net pay: " << "$" << net_pay << endl;
		cout << "Average amount paid: " << "$" << avg_amount << endl;
		cout << "Total payroll: " << "$" << payroll << endl;


	 cout << "Do you want to enter another employee? (y/n)?" << endl;
		cin >> answer;


	} while (answer == 'y');

	

	return 0;
}

OUTPUT WINDOW:

Enter employee ID:
1
Enter number of hours worked:
45
Enter hourly wage:
13
Employee ID: 1
Net pay: $291.261
Average amount paid: $291.261
Total payroll: $291.261
Do you want to enter another employee? (y/n)?
y
Enter employee ID:
23
Enter number of hours worked:
30
Enter hourly wage:
15
Employee ID: 23
Net pay: $449.194
Average amount paid: $370.228
Total payroll: $740.455
Do you want to enter another employee? (y/n)?

Why am I getting the same amounts for the first employee?


Last edited on
Why am I getting the same amounts for the first employee?

The first time through you compute the net pay. Payroll and emp_count are initially zero and you execute this code:
1
2
3
        payroll += net_pay;   // sets payroll to net_pay
        emp_count++;   // sets emp_count to 1
        avg_amount = payroll / emp_count; // sets avg_amount to payroll/1 

So how should I fix it? I'm completely stuck.
The point is that the first time through the loop you have only one employee. With just one employee, the net pay, payroll, and average pay amount will be the same. If you have one employee who makes $100 then your total payroll is $100, and your average salary is $100. So the numbers should be the same.

But let's look at the calculations. I think they may be wrong:
1
2
3
4
5
6
7
8
	if (hours_worked > 40) {
	    overtime_pay = hours_worked * 1.5 * hourly_wage;
	    gross_salary = overtime_pay - (hours_worked * hourly_wage);
	    net_pay = gross_salary - (3.625 / gross_salary * 100);
	} else {
	    gross_salary = hours_worked * hourly_wage;
	    net_pay = gross_salary - (3.625 / gross_salary * 100);
	}

Line 2: usually "overtime pay" is pay for work done in addition to the normal 40 hrs. It's calculated at 1.5 time the normal rate, but that multiplier only applies to the time over 40 hours. You're applying it to all the time worked.

Line 3: Gross salary is less than overtime pay? That doesn't seem right.

Line 4: What is this formula? Where does it come from?

Can you post the assignment? It's difficult to tell if your results are wrong without knowing what they are supposed to be.
Write a program to process weekly employee time cards for all employees of an organization. For each employee, program needs to collect three data items – an identification number, hourly wage rate and number of hours worked during a given week. Each employee is to be paid time and a half for all hours worked over 40. A tax amount of 3.625 percent of gross salary will be detected. The program output should show the employee’s number and net pay. Display the total payroll and the average amount paid at the end of the run.


The only thing wrong now are the formulas?
The only thing wrong now are the formulas?

And the fact that total payroll and average amount paid should be displayed only once at the end of the run.

As for the formulas, it says "Each employee is to be paid time and a half for all hours worked over 40." Also, I think the tax means that you should subtract 3.625% of the gross salary from the pay.

A tax amount of 3.625 percent of gross salary will be detected.

This is ambiguous and I urge you to get clarification from the professor. Is the tax paid by the employee or the employer? If it's paid by the employee then it should be subtracted from the employees net pay. If it's paid by the employer then it should be added to the total payroll cost. Also I think the "average amount paid" is the average gross salary since that is the amount paid by the employer. The average net salary is the average amount received. You might seek clarification of this too.
Topic archived. No new replies allowed.