-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog6_mkh115_zmh11.cpp
626 lines (519 loc) · 20.5 KB
/
prog6_mkh115_zmh11.cpp
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
File: prog6_mkh115_zmh11.cpp
Author: Mavrick Henderson, Zachary Hickey
C.S.1428.002
Lab Section: L17, L17
Program: #6
Due Date: 12/02/19
This program prints to an output file a title and column headers for a
payroll report. It then reads an employees work record from an input file.
The number of overtime hours worked, gross pay, state tax, federal tax,
and net pay are calculated for each employee. The authors' personal
identification information followed by the company payroll report is
printed to the output file. Monetary values are displayed to two decimal
places.
An attempt to avoid repetitive code is made.
An appropriate message is displayed to the console screen if either the
input or the output file fails to open.
An appropriate message is written to the console screen informing the
user of the output file name to which the results have been written.
The client file (main) calls the following void functions to process
the data:
- printIdInfo prints the authors' personal information - name(s),
class/section number, lab section number(s), due date - on the first,
second, third and fourth lines. Two blank lines are left after the
due date. The lecture section number is displayed in a three-digit
field with leading zeros shown. The lab section number is displayed
in a two-digit field with leading zeros shown. The calling routine
determines the output stream to which the information is directed.
(Refer to sample output below.)
- printReportHeadings prints to the output file the title & column
headings for the payroll report. One blank line is left after the
centered report title. Column headers are displayed on two lines.
(Refer to sample output below.)
- dataIn reads the employee ID#, hours worked and pay rate from
an input file storing the values read into parallel arrays. The
employee ID# is stored in a 1D array of integers. The hours worked
and the pay rate are stored in the first and second columns of a
2D array of reals.
- overTime calculates the number of overtime hours worked by the
employee based on a 40 hour work week.
- grossPay calculates the employee's gross pay for the week. If the
employee worked 40 hours or less, gross pay is the hourly pay rate
multiplied by the number of hours worked; otherwise, the employee
worked more than 40 hours, and they are paid the regular hourly
rate for the first 40 hours plus time and a half for any hours
over 40.
- stateTax calculates state taxes owed by the employee, which is
calculated at a straight 6% of the employee's weekly gross pay.
(6% is a sample tax rate. The tax rate will vary with each state.)
- federalTax calculates federal taxes owed by the employee. If
weekly gross pay is $400.00 or less, the tax rate is 20%; otherwise,
the employee's weekly gross pay is more than $400.00, and the tax
rate is calculated at 20% for the first $400.00 and 31% for any
amount over $400.00.
(These rates will vary based on current federal tax regulations.)
- netPay calculates the employee's net pay for the week.
(gross pay minus calculated taxes both state & federal).
- printReportData prints to the output file the information for each
employee in tabular form. Monetary values are displayed to two
digits of precision. (Refer to sample output below.)
- writeFileLocation prints an appropriate message to the console
screen indicating to the user the name of the output file to which
the results have been written. The output file name is provided by
the calling routine.(Refer to sample output below.)
The following named constants are declared globally:
- the number of hours in a normal work week (40.00)
- the state tax rate (0.06)
- the federal tax rates (0.20; 0.31)
- the cut off at which federal taxes increase (400.00)
- parallel array dimensions
- names used to reference individual columns in the payroll array
Locally declared named constants include:
- a string to hold the authors' names
- a string to hold the authors' lab section number(s)
- a string to hold the project's due date
- an integer to hold the lecture section number
- an integer representing the maximum string length allowed for
input and output file names which are stored in character arrays
of that length
==========================================================================
Layout and content of the output are shown in the samples below.
Input (file - prog6_?inp.txt) // '?' represents three-digit lecture section #
one record per employee / each record containing three numbers
ID#(integer) hours worked (real) pay rate (real)
...
Constants: globally declared:
integer: ROWS
COLS
{2D array column indices)
HRS_WRKD = 0,
PAYRATE = 1,
OVRTIME = 2,
GROSS = 3,
ST_TAX = 4,
FED_TAX = 5,
NETPAY = 6;
double: CUT_OFF (hours in a work week)
STATE_TX_RATE
TAX_CUT_OFF (division @ which net pay is taxed higher)
LOW_TAX_RATE
HI_TAX_RATE
Constants: locally declared:
string: AUTHORS
LAB_SECTION
DUE_DATE
integer: LECTURE_SECTION
MAX_LENGTH_FN = ? // filename's maximum length
Output (console):
Sample Console Output
Authors' Names
C.S.1428.? // '?' represents three-digit lecture section number
Lab Section: L? // '?' represents two-digit lab section numbet
--/--/-- // dashes represent due date, month/day/year
<blank line>
<blank line>
Program results have been written to prog6_?out.txt.
Output (file: prog6_?out.txt): // '?' represents three-digit lecture section #
Sample File Output
Authors' Names
C.S.1428.? // '?' represents three-digit lecture section number
Lab Section: L? // '?' represents two-digit lab section numbet
--/--/-- // dashes represent due date, month/day/year
<blank line>
<blank line>
Monthly Payroll Report
<blank line>
ID# Hours Hourly Overtime Gross State Federal Net
Worked Rate Hours Pay Tax Tax Pay
1000 51.00 6.55 11.00 370.07 22.20 74.02 273.86
...
1002 26.00 15.00 0.00 390.00 23.40 78.00 288.60
...
=======================================================================
<Output will vary based on actual input values.>
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib> // 4 Code::Blocks
using namespace std;
const int ROWS = 10, // number of employees
COLS = 7;
// 2D array (payroll) column indices
const int HRS_WRKD = 0,
PAYRATE = 1,
OVRTIME = 2,
GROSS = 3,
ST_TAX = 4,
FED_TAX = 5,
NETPAY = 6;
const double CUT_OFF = 40.00, // work week
STATE_TX_RATE = 0.06,
TAX_CUT_OFF = 400.00, // earnings after which taxed at a higher rate
LOW_TAX_RATE = 0.20,
HI_TAX_RATE = 0.31;
void printIdInfo( ostream &, const string, const int, const string,
const string );
void printReportHeadings ( ostream & );
void dataIn ( ifstream &, int [], double [][COLS] );
void overTime ( double [][COLS] );
void grossPay ( double [][COLS] );
void stateTax ( double [][COLS] );
void federalTax ( double [][COLS] );
void netPay ( double [] [COLS] ) ;
void printReportData ( ostream &, const int[], const double[][COLS] );
void writeFileLocation( const char [] );
int main ( )
{
const string AUTHORS = "Mavrick Henderson, Zachary Hickey",
LAB_SECTION = "L17 & L17",
DUE_DATE = "12/02/19";
const int LECTURE_SECTION = 2,
MAX_LENGTH_FN = 20;
char input_filename[MAX_LENGTH_FN + 1] = "prog6_002inp.txt",
output_filename[MAX_LENGTH_FN + 1] = "prog6_002out.txt";
int employee[ROWS]; // employee ID#s
double payroll[ROWS][COLS]; // payroll data
ifstream fin;
fin.open( input_filename );
if ( !fin )
{
cout << endl << endl
<< "***Program Terminated.***" << endl << endl
<< "Input file failed to open." << endl;
system("PAUSE>NUL");
return 1;
}
ofstream fout;
fout.open ( output_filename );
if ( !fout )
{
cout << endl << endl
<< " ***Program Terminated.*** " << endl << endl
<< "Output file failed to open." << endl;
fin.close( );
system("PAUSE>NUL");
return 2;
}
printIdInfo( fout, AUTHORS, LECTURE_SECTION, LAB_SECTION, DUE_DATE );
printReportHeadings ( fout );
dataIn ( fin, employee, payroll );
overTime ( payroll );
grossPay ( payroll );
stateTax ( payroll );
federalTax ( payroll );
netPay ( payroll );
printReportData ( fout, employee, payroll );
printIdInfo( cout, AUTHORS, LECTURE_SECTION, LAB_SECTION, DUE_DATE );
writeFileLocation ( output_filename );
fin.close( );
fout.close( );
system("PAUSE>NUL");
return 0;
}
/*
Function: printIdInfo
Author(s): Mavrick Henderson, Zachary Hickey
C.S.1428.002
Lab Section: L17, L17
Program 6 Function printIdInfo
12/02/19
The void function, printIdInfo, writes the team members' identifying
information to the output file on four separate lines
- AUTHORS
- C.S.1428. Lecture section
- Lab Section: LAB_SECTION
- DUE_DATE
one blank line is left after DUE_DATE.
Sample Output:
Authors' Names
C.S.1428.? // '?' represents three-digit lecture section #
Lab Section: L? & L? // '?' represents two-digit lab section #s
--/--/-- // dashes represent due date, month/day/year
<blank line>
Recives: output file variable
AUTHORS (string constant)
LECTURE_SECTION (int constant)
LAB_SECTION (string constant)
DUE_DATE (string constant)
Constants: AUTHORS, LECTURE_SECTION, LAB_SECTION, DUE_DATE
Returns: nothing; prints user's team identifying info to a file
*/
void printIdInfo ( ostream & out, const string AUTHORS,
const int LECTURE_SECTION, const string LAB_SECTION,
const string DUE_DATE )
{
out << AUTHORS << endl
<< "C.S.1428." << setw(3) << setfill('0') << LECTURE_SECTION << endl
<< "Lab Section: " << LAB_SECTION << endl
<< DUE_DATE << endl << endl << endl;
}
/*
Function: printReportHeadings
Author(s): Mavrick Henderson, Zachary Hickey
C.S.1428.002
Lab Section: L17, L17
Program 6 Function printReportHeadings
12/02/19
This void function printReportHeadings print to the output file the title
& column headings for the payroll report. Leave one blank line after the
centered report title. Display column headers on two lines
Sample output:
Monthly Payroll Report
ID# Hours Hourly Overtime Gross State Federal Net
Worked Rate Hours Pay Tax Tax Pay
Recives: output file variable
Constants: none
Returns: nothing writes in output file
*/
void printReportHeadings ( ostream & out )
{
out << " Monthly Payroll Report" << endl << endl
<< " ID# Hours Hourly Overtime Gross State Federal"
<<" Net" << endl
<<" Worked Rate Hours Pay Tax Tax"
<<" Pay" << endl;
}
/*
Function: dataIn
Author(s): Mavrick Henderson, Zachary Hickey
C.S.1428.002
Lab Section: L17, L17
Program 6 Function dataIn
12/02/19
This read the employee ID#, hours worked and pay rate from an input file
(prog6_002inp.txt) storing the values read into parallel arrays. Store the
employee ID# in a 1D array of integers. The hours worked and the pay rate
should be stored in the first and second columns of a 2D array ofreals. A
Reccives: input file variable, 1D array of employees, 2D array of payroll
info
Constants: none
Returns: nothing reads information from file
*/
void dataIn ( ifstream & fin, int employee[], double payroll[][COLS])
{
for ( int row = 0; row < ROWS; row++ )
fin >> employee[row] >> payroll[row][HRS_WRKD] >> payroll[row][PAYRATE];
}
/*
Function: overTime
Author(s): Mavrick Henderson, Zachary Hickey
C.S.1428.002
Lab Section: L17, L17
Program 6 Function overTime
12/02/19
This void function calculates the number of overtime hours worked by the
employee based on a 40-hour work week and stores the number of hours of over
time in a 2D array (column OVERTIME)
recives: 2D array payroll
Constants: globally declared integers:
ROWS - parallel arrays row dimension
column designations in 2D array
HRS_WRKD
OVRTIME
globally declared reals:
CUT_OFF
returns: nothing
*/
void overTime ( double payroll[][COLS] )
{
for ( int row = 0; row < ROWS; row++ )
{
if ( payroll[row][HRS_WRKD] > CUT_OFF )
payroll[row][OVRTIME] = payroll[row][HRS_WRKD] - CUT_OFF;
else
payroll[row][OVRTIME] = 0;
}
}
/*
Function: grossPay
Author(s): Mavrick Henderson, Zachary Hickey
C.S.1428.002
Lab Section: L17, L17
Program 6 Function grossPay
12/02/19
This void function calculates the employee's gross pay for the week. If the
employee worked 40.0 hours or less, gross pay is the hourly pay rate
multiplied by the number of hours worked; otherwise, the employee worked
more than 40 hours, and they are paid the regular hourly rate for the first
40.0 hours plus time and a half for any hours over 40.
recives: 2D array payroll
Constants: globally declared integers:
ROWS - parallel arrays row dimension
column designations in 2D array
GROSS
globally declared reals:
CUT_OFF
globally declared integers:
HRS_WRKD
PAYRATE
OVRTIME
returns: nothing
*/
void grossPay ( double payroll[][COLS] )
{
for ( int row = 0; row < ROWS; row++ )
if ( payroll[row][OVRTIME] > 0)
{
payroll[row][GROSS] =
payroll[row][OVRTIME] * payroll[row][PAYRATE] * 1.5;
payroll[row][GROSS]+= CUT_OFF * payroll[row][PAYRATE];
}
else
payroll[row][GROSS] =
payroll[row][HRS_WRKD] * payroll[row][PAYRATE];
}
/*
Function: stateTax
Author(s): Mavrick Henderson, Zachary Hickey
C.S.1428.002
Lab Section: L17, L17
Program 6 Function stateTax
12/02/19
This fuction calculates state taxes owed by the employee, calculated at a
straight 6% of the employee's weekly gross pay
recives: 2D array payroll
Constants: globally declared integers:
ROWS - parallel arrays row dimension
column designations in 2D array
ST_TAX
globally declared reals:
STATE_TX_RATE
globally declared integers:
GROSS
returns: nothing
*/
void stateTax ( double payroll[][COLS] )
{
for ( int row = 0; row < ROWS; row++ )
payroll[row][ST_TAX] = payroll[row][GROSS] * STATE_TX_RATE;
}
/*
Function: federalTax
Author(s): Mavrick Henderson, Zachary Hickey
C.S.1428.002
Lab Section: L17, L17
Program 6 Function federalTax
12/02/19
The void function federalTax calculates federal taxes owed by the employee.
Note: The sample below assumes a tax cut-off of 400.00 and a tax rate
for 20% for low wage earners and a tax rate of 31% for high wage
earners. Actual values may differ.
If weekly gross pay is $400.00 or less,
tax rate is 20%.
otherwise // employee's weekly gross pay is > $400.00
tax rate is calculated at 20% for the first $400.00
and 31% for any amount over $400.00.
Receives: 2D array of reals (payroll information) / COLS
Constants: globally declared integers:
ROWS - parallel arrays row dimension
COLS - 2D array column dimension
column designations in 2D array
GROSS, FED_TAX
globally declared reals:
TAX_CUT_OFF - income level at which taxes are increased
LOW_TAX_RATE
HI_TAX_RATE
Returns: fills the federal tax column with calculated data
*/
void federalTax ( double payroll[][COLS] )
{
for ( int row = 0; row < ROWS; row++ )
if ( payroll[row][GROSS] <= TAX_CUT_OFF )
payroll[row][FED_TAX] = payroll[row][GROSS] * LOW_TAX_RATE;
else
{
payroll[row][FED_TAX] = TAX_CUT_OFF * LOW_TAX_RATE;
payroll[row][FED_TAX] +=
( payroll[row][GROSS] - TAX_CUT_OFF ) * HI_TAX_RATE;
}
}
/*
Function: netPay
Author(s): Mavrick Henderson, Zachary Hickey
C.S.1428.002
Lab Section: L17, L17
Program 6 Function netPay
12/02/19
This function calculates the employee's net pay for the week.
(gross pay minus calculated taxes both state & federal)
recives: 2D array payroll
Constants: globally declared integers:
ROWS - parallel arrays row dimension
column designations in 2D array
NETPAY
globally declared reals:
STATE_TX_RATE
globally declared integers:
GROSS
ST_TAX
FED_TAX
returns: nothing
*/
void netPay ( double payroll[][COLS] )
{
for ( int row = 0; row < ROWS; row++ )
payroll[row][NETPAY] =
payroll[row][GROSS] - payroll[row][ST_TAX] - payroll[row][FED_TAX];
}
/*
Function: printReportData
Author(s): Mavrick Henderson, Zachary Hickey
C.S.1428.002
Lab Section: L17, L17
Program 6 Function printReportData
12/02/19
The void function printReportData prints to an output file the payroll
information for each employee in tabular form under the appropriate column
headers. The employee records are single spaced. Monetary values are
displayed with two digits of precision.
Note: The report title and column headers are printed by a previously
called function.
Monthly Payroll Report
ID# Hours Hourly Overtime Gross State Federal Net
Worked Rate Hours Pay Tax Tax Pay
1000 51.00 6.55 11.00 370.07 22.20 74.02 273.86
1001 40.50 6.50 0.50 264.88 15.89 52.98 196.01
...
Receives: output file variable, protected 1D array of integer ID#s,
protected 2D array of reals (payroll information) / COLS;
(in this order)
Constants: globally declared integers:
ROWS - parallel arrays row dimension
COLS - 2D array column dimension
Returns: nothing; prints out report data under appropriate report headings
previously printed
*/
void printReportData ( ostream& out, const int employee[],
const double payroll[][COLS] )
{
for ( int row = 0; row < ROWS; row++ )
{
out << employee[row];
for ( int info = 0; info < COLS; info++ )
out << fixed << setprecision(2) << setfill(' ') << setw(10)
<< payroll[row][info];
out << endl;
}
}
/*
Function: writeFileLocation
Author(s): Mavrick Henderson, Zachary Hickey
C.S.1428.002
Lab Section: L17, L17
Program 6 Function writeFileLocation
12/02/19
This Function prints an appropriate message to the console screen
indicating to the user the name of the output file to which the results
have been written.
sample output:
Program has been written to prog6_002out.txt.
recives: 1D array output_file_name
Constants: none
returns: nothing
*/
void writeFileLocation( const char output_file_name[] )
{
cout <<"Program has been written to " << output_file_name << "." << endl;
}