Having trouble finding the correct average

Write your question here.
I am trying to write a program that uses a running sum inside of a loop to find and display the average pay for a number of employees to be processed.

I entered 4 to be processed

For each employee the program should ask for the number of hours worked and the hourly wage. The pay should be calculated on the basis of the first 40 hours at the hourly wage and the additional hours at "time and a half".

The average should be displayed wit a dollar sign and two decimal places.

The problem I am having is that the average is not calculating correctly

here are the hours and wages that i used.
30 hrs at 7.50
52 hrs at 8.25
60 hrs at 7.90
40 hrs at 9.50

the average that the programs computes is $473.38
the average i come up with when I calculate it myself is $409.12

it is probably something simple but for the life of me I cant see it

any help would be appreciated

Thanks

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 <iomanip>

using namespace std;

int main()

{
	double total = 0.0;
	double wage;
	double overtimeWage;
	double overtimeHours;
	double totalWage;
	double average;
	int hours;
	int numemploy;
	int minNum = 1;

	cout << "Please enter the number of employees " << endl;
	cout << "Number of employees must be between 1 and 10." << endl;
	cin >> numemploy;

	while (numemploy < 1 || numemploy > 10)
	{
		cout << "Invalid entry:  The Maximum number of employees is 10 and the Minimum is 1" << endl;
		cout << "Enter the number of employees" << endl;
		cin >> numemploy;
	}

	cout << fixed << showpoint << setprecision(2);

	for (minNum; minNum <= numemploy; minNum++)
	{
		
		cout << "How many hours were worked?" << endl;
		cin >> hours;
		cout << "What is the employees wage per hour?" << endl;
		cin >> wage;

		if (hours > 40)
		{
			overtimeHours = hours - 40;
			overtimeWage = (wage / 2) + wage;
			totalWage = (wage * hours) + (overtimeWage * overtimeHours);
		}
		else 
		{
			totalWage = hours * wage;
		}
		total += totalWage;
	}

	average = total / numemploy;
	
	cout << "The average pay is $ " << average << endl;

	return 0;
}
Last edited on
Your code is right but your logic is wrong in calculating the overtime hours.

Line 45 should be
 
totalWage = (wage * 40) + (overtimeWage * overtimeHours);


Because if you put hours you're paying them their normal wage for ALL their hours even their overtime hours. However, they are only paid the normal wage for the first 40 hours. You can also simply change the overtime wage calculation to, but this is just my preference:
 
overtimeWage = wage *1.5
Last edited on
I cant believe I overlooked that.... Works perfect now.

Thank You
Topic archived. No new replies allowed.