function not outputting as expected when utilizing array

Good Afternoon Everyone,

I am having issues getting a function to output as expected for a payroll program that I am working on. I added an array so that I may differentiate between employees (employee 1, 2, 3, etc.). This is for a payroll program which the user is prompted to enter hours for each employee (7 total). These hours are then separately calculated to multiply the hours by pay rate to display the gross pay for each employee. While the array values display 1-7 as intended, it performs 1-7 for every gross pay for each employee (as seen below)...

*******************************************************************************
Monthly Payroll
*******************************************************************************
Enter the number of hours worked for employee1: 1
Enter the number of hours worked for employee2: 2
Enter the number of hours worked for employee3: 3
Enter the number of hours worked for employee4: 4
Enter the number of hours worked for employee5: 5
Enter the number of hours worked for employee6: 6
Enter the number of hours worked for employee7: 7

*******************************************************************************
Employee Gross Pay Totals
*******************************************************************************
Employee 1's gross pay for this period: $7.75
Employee 2's gross pay for this period: $7.75
Employee 3's gross pay for this period: $7.75
Employee 4's gross pay for this period: $7.75
Employee 5's gross pay for this period: $7.75
Employee 6's gross pay for this period: $7.75
Employee 7's gross pay for this period: $7.75
Employee 1's gross pay for this period: $15.5
Employee 2's gross pay for this period: $15.5
Employee 3's gross pay for this period: $15.5
Employee 4's gross pay for this period: $15.5
Employee 5's gross pay for this period: $15.5
Employee 6's gross pay for this period: $15.5
Employee 7's gross pay for this period: $15.5
Employee 1's gross pay for this period: $23.25
Employee 2's gross pay for this period: $23.25
Employee 3's gross pay for this period: $23.25
Employee 4's gross pay for this period: $23.25
Employee 5's gross pay for this period: $23.25
Employee 6's gross pay for this period: $23.25
Employee 7's gross pay for this period: $23.25
Employee 1's gross pay for this period: $31
Employee 2's gross pay for this period: $31
Employee 3's gross pay for this period: $31
Employee 4's gross pay for this period: $31
Employee 5's gross pay for this period: $31
Employee 6's gross pay for this period: $31
Employee 7's gross pay for this period: $31
Employee 1's gross pay for this period: $38.75
Employee 2's gross pay for this period: $38.75
Employee 3's gross pay for this period: $38.75
Employee 4's gross pay for this period: $38.75
Employee 5's gross pay for this period: $38.75
Employee 6's gross pay for this period: $38.75
Employee 7's gross pay for this period: $38.75
Employee 1's gross pay for this period: $46.5
Employee 2's gross pay for this period: $46.5
Employee 3's gross pay for this period: $46.5
Employee 4's gross pay for this period: $46.5
Employee 5's gross pay for this period: $46.5
Employee 6's gross pay for this period: $46.5
Employee 7's gross pay for this period: $46.5
Employee 1's gross pay for this period: $54.25
Employee 2's gross pay for this period: $54.25
Employee 3's gross pay for this period: $54.25
Employee 4's gross pay for this period: $54.25
Employee 5's gross pay for this period: $54.25
Employee 6's gross pay for this period: $54.25
Employee 7's gross pay for this period: $54.25
Press any key to continue . . .

I've been looking at this for hours now and can't make heads or tails as to what is causing this. I'm pretty new to c++, so it's likely something simple that I'm missing or overlooking.

Here is my code so far:


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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  **Payroll.h**

    #include <iostream>
    
    using namespace std;
    
    class Payroll
    {
    public:
    	Payroll();
    	void setHourlyPayRate(double), setHoursWorked(double), setTotalWeeklyPay(double);
    	double getHourlyPayRate();
    	double getHoursWorked();
    	double getTotalWeeklyPay();
    private:
    	double hourlyPayRate, hoursWorked, totalWeeklyPay;
    };
    
    Payroll::Payroll()
    {
    	hourlyPayRate = 0.0;
    	hoursWorked = 0.0;
    	totalWeeklyPay = 0.0;
    }
    
    void Payroll::setHourlyPayRate(double hpr)
    {
    	hourlyPayRate = hpr;
    }
    double Payroll::getHourlyPayRate()
    {
    	return hourlyPayRate;
    }
    void Payroll::setHoursWorked(double hw)
    {
    	hoursWorked = hw;
    }
    double Payroll::getHoursWorked()
    {
    	return hoursWorked;
    }
    void Payroll::setTotalWeeklyPay(double twp)
    {
    	totalWeeklyPay = twp;
    }
    double Payroll::getTotalWeeklyPay()
    {
    	return totalWeeklyPay;
    }

**Payroll.cpp**

    #include <iostream>
    #include <iomanip>
    #include <string>
    #include "Payroll.h"
    
    using namespace std;
    
    void displaypayroll(Payroll * const);
    
    int main()
    {
    	const int employee = 7; /*array of employees with maximum of 7*/
    	int employeeNumber[employee] = { 1,2,3,4,5,6,7 };
    	Payroll payCycle[employee];
    
    	/*Beginning of program. Outputs the following to the user, prompting them to enter the number of hours worked for each of the 7 employees*/
    	cout << "*******************************************************************************" << endl
    		<< setw(48) << "Monthly Payroll" << endl
    		<< "*******************************************************************************" << endl;
    	for (int index = 0; index < employee; ++index)
    	{
    		double hpr, hw, twp; /*initializes hourly rate, hours worked, and twp mutators, accepting decimal values*/
    		hpr = 7.75;
    		char decision = 'Y';
    
    		cout << "Enter the number of hours worked for employee" << (index + 1) << ": ";
    		cin >> hw;
    
    		while (hw >= 61) /**/
    		{
    			cout << "ERROR: Hours must be between 0 and 60" << endl
    				<< "Are the hours you entered correct (Y or N)?  ";
    			cin >> decision;
    
    			if (decision == 'Y' || decision == 'y')
    			{
    				cout << endl << "Hours above 60 are considered overtime. Please contact management." << endl << endl;
    				exit(0);
    			}
    			else if (decision == 'N' || decision == 'n')
    			{
    				cout << "Please re-enter the total number of hours worked:  ";
    				cin >> hw;
    			}
    		}
    		twp = hpr * hw; /*calculates employee's gross pay for the week by multiplying hourly pay rate by hours worked*/
    
    		payCycle[index].setHourlyPayRate(hpr);
    		payCycle[index].setHoursWorked(hw);
    		payCycle[index].setTotalWeeklyPay(twp);
    	}
    	/*Outputs employee gross pay totals to user, utilizing the twp calculations above*/
    	cout << endl
    		<< "*******************************************************************************" << endl
    		<< setw(52) << "Employee Gross Pay Totals" << endl
    		<< "*******************************************************************************" << endl;
    	for (int index = 0; index < employee; ++index) /*displays gross pay for each of the 7 employees*/
    	{
    		displaypayroll(&payCycle[index]);
    	}
    
    	system("PAUSE");
    }
    void displaypayroll(Payroll * const e)
    {
    	const int employee = 7;
    	int employeeNumber[employee] = { 1,2,3,4,5,6,7 };
    	Payroll payCycle[employee];
    	for (int index = 0; index < employee; ++index)
    	cout << "Employee " << (index + 1) << "'s gross pay for this period: $" << e->getTotalWeeklyPay() << endl;
    }
1
2
3
4
5
6
7
8
    void displaypayroll(Payroll * const e)
    {
    	const int employee = 7;
    	int employeeNumber[employee] = { 1,2,3,4,5,6,7 };
    	Payroll payCycle[employee];
    	for (int index = 0; index < employee; ++index)
    	cout << "Employee " << (index + 1) << "'s gross pay for this period: $" << e->getTotalWeeklyPay() << endl;
    }


why does this function have the first 4 lines? And if you don't need them, why do you have the function instead of:
1
2
3
4
5
6
7
8
9
10
11
12
    	/*Outputs employee gross pay totals to user, utilizing the twp calculations above*/
    	cout << endl
    		<< "*******************************************************************************" << endl
    		<< setw(52) << "Employee Gross Pay Totals" << endl
    		<< "*******************************************************************************" << endl;
    	for (int index = 0; index < employee; ++index) /*displays gross pay for each of the 7 employees*/
    	{
    		cout << "Employee " << (index+1) << "'s gross pay for this period: $" << payCycle[index].getTotalWeeklyPay() << endl;
    	}
    
    	system("PAUSE");
    }


You have the repetition problem because you loop from 0 to 6 in your display function. I expect if you remove that loop your problem will be removed as well. But you are doing more things in this function that seems useless to me, like creating local arrays that you don't use.
Last edited on
Topic archived. No new replies allowed.