Sort Payroll Structures

Hello,
I am trying to solve this project I was given for school. I am unsure how these structures and arrays work which is why although I added alot more code than given, I sent it into my teacher and he was puzzled with my project. So I brought it back to a payroll calculator unsorted. Any help is much appreciated. Thank you

Sort the payroll data, and output the data to the file in order by gross pay (lowest to highest)
You must use a structure in your solution.


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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;

// Functions

void CalculateOvertimePay();
double ComputeWithholding(int Dependents, double Gross);
double ComputeIRADeduction();
double MedicalDeduction(double NetPay);
void OutputPayroll();

//Variables

int RecordNumber, ValidRecord, HoursWorked, OverTimeHours, DoubleTimeHours, Dependents, FullTime;
double HourlyRate, BasePay, OverTimePay, DoubleTimePay, GrossPay, ModGrossPay, Withholding,
DepositAmount, NetPay, MedicalDeductions, NewNetPay;

ifstream in_stream;
ofstream out_stream;

int main()

{
 OverTimePay = 0;
 OverTimeHours = 0;
 DoubleTimePay = 0;
 DoubleTimeHours = 0;
 MedicalDeductions = 0;

 in_stream.open("employeeData.dat");

 if (in_stream.fail( ))
 {
 cout << "Input file opening failed.\n";
 exit(1);
 }

 out_stream.open("payrollData.dat");

 if (in_stream.fail( ))
 {
 cout << "Input file opening failed.\n";
 exit(1);
 }

 RecordNumber = 0;
 in_stream >> ValidRecord; // Read Valid

 while (ValidRecord > 0) // Zero=No data
 {
 RecordNumber++; // Count
 in_stream >> HourlyRate >> HoursWorked >> Dependents >> FullTime; // Read
 CalculateOvertimePay(); // Call Functions
 DepositAmount = ComputeIRADeduction();
 ModGrossPay = GrossPay - DepositAmount;
 Withholding = ComputeWithholding(Dependents, ModGrossPay);
 NetPay = ModGrossPay - Withholding;

 if (FullTime == 1)
 MedicalDeductions = MedicalDeduction(NetPay);
 NewNetPay = NetPay - MedicalDeductions;
 OutputPayroll();

 in_stream >> ValidRecord; // Read valid again
 } // End of While Loop

 in_stream.close( ); // Close Files Outta Loop

 out_stream.close( );
 cout << "The program has completed successfully.\n";
 return 0;
} // END OF MAIN

//Functions

 // OT Function
void CalculateOvertimePay()

{
 if (HoursWorked > 40) // Test OT
 {
 BasePay = HourlyRate * 40;
 OverTimeHours = HoursWorked - 40;

 if (OverTimeHours > 10) // If Double Time
 {
 DoubleTimeHours = OverTimeHours - 10;
 OverTimeHours = 10;
 DoubleTimePay = HourlyRate * 2 * DoubleTimeHours;
 OverTimePay = HourlyRate * 1.5 * OverTimeHours;
 }

 else // No DT
 OverTimePay = HourlyRate * 1.5 * OverTimeHours;
 GrossPay = BasePay + OverTimePay + DoubleTimePay;
 }

 else
 { // No OT
 GrossPay = HoursWorked * HourlyRate;
 BasePay = GrossPay;
 }

 }

// Taxes Withheld Function
double ComputeWithholding(int Dependents, double Gross)
 {
 double WithheldAmount;

 if(Dependents > 2)
 WithheldAmount = 0.15;

 else if (Dependents == 2)
 WithheldAmount = 0.18;

 else if (Dependents == 1)
 WithheldAmount = 0.2;

 else // no dependents
 WithheldAmount = 0.28;

 WithheldAmount = Gross * WithheldAmount;
 return (WithheldAmount);
 }

//IRA Function
double ComputeIRADeduction()
 {
 double DeductionAmount;

 if(GrossPay >= 500)
 DeductionAmount = GrossPay * 0.10;

 else if (GrossPay > 400)
 DeductionAmount = GrossPay * 0.05;

 else
 DeductionAmount = 0;

 return(DeductionAmount);
 }

//NetPay Function
double MedicalDeduction(double NetPay)

{
 double BenefitsDeduction;
 BenefitsDeduction = (NetPay * 0.07);
 return(BenefitsDeduction);
}

//Output

void OutputPayroll()
{
 out_stream.setf(ios::fixed);
 out_stream.setf(ios::showpoint);
 out_stream.precision(2);


 out_stream << "EMPLOYEE RECORD NUMBER " << RecordNumber << endl;
 out_stream << "Total Hours Worked = " << HoursWorked << endl;
 out_stream << "Hourly pay rate = $" << HourlyRate << endl;
 out_stream << "Base pay = $" << BasePay << endl << endl;
 out_stream << "Overtime hours at time and a half = " << OverTimeHours << endl;
 out_stream << "Overtime pay at time and a half = $" << OverTimePay << endl << endl;
 out_stream << "Overtime hours at double time = " << DoubleTimeHours << endl;
 out_stream << "Overtime pay at double time = $" << DoubleTimePay << endl << endl;
 out_stream << "Gross pay = $" << GrossPay << endl << endl;
 out_stream << "IRA Deduction = $" << DepositAmount << endl << endl;
 out_stream << "Modified Gross Pay = $" << ModGrossPay << endl;
 out_stream << "Taxes withheld = $" << Withholding << endl << endl;
 out_stream << "Medical Benefits deduction = $" << MedicalDeductions << endl << endl;
 out_stream << "Net pay = $" << NewNetPay << endl << endl << endl << endl;
}
You must use a structure in your solution.

I don't see a struct in your code.

You want to declare a struct that contains all payroll data for an employee.
Then you want to declare an array of structs into which you read the data from the file.
Once you've loaded the array of payroll_data structs, then you want to sort the array of structs, ordering on gross pay.
I'm not really sure how these struct things work, but I've tried something.

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;

// Functions
struct PayrollStruct
{
int RecordNumber, HoursWorked, OverTimeHours, DoubleTimeHours;
double HourlyRate, BasePay, OverTimePay, DoubleTimePay, GrossPay, ModGrossPay, Withholding,
DepositAmount, MedicalDeductions, NewNetPay;
};
void CalculateOvertimePay();
double ComputeWithholding(int Dependents, double Gross);
double ComputeIRADeduction();
double MedicalDeduction(double NetPay);
void Sort(int numbers[], double NewNetPay);
void swap_values(double numbers[index], double numbers[index_2]);


{
void OutputPayroll();

//Variables

int RecordNumber, ValidRecord, HoursWorked, OverTimeHours, DoubleTimeHours, Dependents, FullTime;
double HourlyRate, BasePay, OverTimePay, DoubleTimePay, GrossPay, ModGrossPay, Withholding,
DepositAmount, NetPay, MedicalDeductions, NewNetPay;

ifstream in_stream;
ofstream out_stream;

int main()

struct PayrollStruct array_of_structs[];
Sort(numbers[], NewNetPay);
swap_values(numbers[index], numbers[index_2]);

{
 OverTimePay = 0;
 OverTimeHours = 0;
 DoubleTimePay = 0;
 DoubleTimeHours = 0;
 MedicalDeductions = 0;

 in_stream.open("employeeData.txt");

 if (in_stream.fail( ))
 {
 cout << "Input file opening failed.\n";
 exit(1);
 }

 out_stream.open("payrollData.txt");

 if (in_stream.fail( ))
 {
 cout << "Input file opening failed.\n";
 exit(1);
 }

 RecordNumber = 0;
 in_stream >> ValidRecord; // Read Valid

 while (ValidRecord > 0) // Zero=No data
 {
 RecordNumber++; // Count
 in_stream >> HourlyRate >> HoursWorked >> Dependents >> FullTime; // Read
 CalculateOvertimePay(); // Call Functions
 DepositAmount = ComputeIRADeduction();
 ModGrossPay = GrossPay - DepositAmount;
 Withholding = ComputeWithholding(Dependents, ModGrossPay);
 NetPay = ModGrossPay - Withholding;

 if (FullTime == 1)
 MedicalDeductions = MedicalDeduction(NetPay);
 NewNetPay = NetPay - MedicalDeductions;
 OutputPayroll();

 in_stream >> ValidRecord; // Read valid again
 } // End of While Loop

 in_stream.close( ); // Close Files Outta Loop

 out_stream.close( );
 cout << "The program has completed successfully.\n";
 return 0;
} // END OF MAIN

//Functions

 }
 // OT Function
void CalculateOvertimePay()

{
 if (HoursWorked > 40) // Test OT
 {
 BasePay = HourlyRate * 40;
 OverTimeHours = HoursWorked - 40;

 if (OverTimeHours > 10) // If Double Time
 {
 DoubleTimeHours = OverTimeHours - 10;
 OverTimeHours = 10;
 DoubleTimePay = HourlyRate * 2 * DoubleTimeHours;
 OverTimePay = HourlyRate * 1.5 * OverTimeHours;
 }

 else // No DT
 OverTimePay = HourlyRate * 1.5 * OverTimeHours;
 GrossPay = BasePay + OverTimePay + DoubleTimePay;
 }

 else
 { // No OT
 GrossPay = HoursWorked * HourlyRate;
 BasePay = GrossPay;
 }

 }

// Taxes Withheld Function
double ComputeWithholding(int Dependents, double Gross)
 {
 double WithheldAmount;

 if(Dependents > 2)
 WithheldAmount = 0.15;

 else if (Dependents == 2)
 WithheldAmount = 0.18;

 else if (Dependents == 1)
 WithheldAmount = 0.2;

 else // no dependents
 WithheldAmount = 0.28;

 WithheldAmount = Gross * WithheldAmount;
 return (WithheldAmount);
 }

//IRA Function
double ComputeIRADeduction()
 {
 double DeductionAmount;

 if(GrossPay >= 500)
 DeductionAmount = GrossPay * 0.10;

 else if (GrossPay > 400)
 DeductionAmount = GrossPay * 0.05;

 else
 DeductionAmount = 0;

 return(DeductionAmount);
 }

//NetPay Function
double MedicalDeduction(double NetPay)

{
 double BenefitsDeduction;
 BenefitsDeduction = (NetPay * 0.07);
 return(BenefitsDeduction);
}
void Sort(int numbers[], double NewNetPay)
{
for(int index = 0; index < NewNetPay - 1; index++)
{
for(int index_2 = index + 1; index_2 < NewNetPay; index_2++)
{
if(numbers[index_2] < numbers[index])
(numbers[index], numbers[index_2]);
    }
    
void swap_values(double numbers[index], double numbers[index_2])
{
    double temp;
    int index
    temp = v1;
    v1 = v2;
    v2 = temp;

//Output

void OutputPayroll()
{
 out_stream.setf(ios::fixed);
 out_stream.setf(ios::showpoint);
 out_stream.precision(2);


 out_stream << "EMPLOYEE RECORD NUMBER " << RecordNumber << endl;
 out_stream << "Total Hours Worked = " << HoursWorked << endl;
 out_stream << "Hourly pay rate = $" << HourlyRate << endl;
 out_stream << "Base pay = $" << BasePay << endl << endl;
 out_stream << "Overtime hours at time and a half = " << OverTimeHours << endl;
 out_stream << "Overtime pay at time and a half = $" << OverTimePay << endl << endl;
 out_stream << "Overtime hours at double time = " << DoubleTimeHours << endl;
 out_stream << "Overtime pay at double time = $" << DoubleTimePay << endl << endl;
 out_stream << "Gross pay = $" << GrossPay << endl << endl;
 out_stream << "IRA Deduction = $" << DepositAmount << endl << endl;
 out_stream << "Modified Gross Pay = $" << ModGrossPay << endl;
 out_stream << "Taxes withheld = $" << Withholding << endl << endl;
 out_stream << "Medical Benefits deduction = $" << MedicalDeductions << endl << endl;
 out_stream << "Net pay = $" << NewNetPay << endl << endl << endl << endl;
}
Last edited on
Structs :)
http://www.cplusplus.com/doc/tutorial/structures/

If your curious of what they are.
As far as I can see, you're never storing data into the struct.

Your sort routine is going to have to sort the array_of_structs.

Line 27-29: You should be avoiding the use of globals.

Line 36: You're not allocating any storage for array_of_structs.

Line 66: You're not testing if a record was actually read. If no record was read, ValidRecord is going to be unmodified. You should be testing the in_stream.
 
  while (in_stream >> ValidRecord) 


Line 77: What's the value of MedicalDeductions if FullTime is not 1?

Line 190: OutputPayroll should be printing one element of array_of_structs.


Agree with AbstractionAnon, you have to store data in the struct to get the results you want ie. gross pay form lowest to highest.
HINT : do that after the all the gross pay has calculated
Last edited on
Topic archived. No new replies allowed.