Write a complete C++ program, including comments, to do the following: The progr
ID: 3580960 • Letter: W
Question
Write a complete C++ program, including comments, to do the following: The program will compute a weekly payroll. It will read in information for each of the employees of a company. For each employee, it will compute a series of calculations and then print the results. After processing the last employee, the program will print the total number of employees processed. The output will be sent to a file (see Output format for details).
1. The program will do the following for each employee: First, it will read in the data values for this employee. Each employee will have a 3-digit identification number (the ID will be an integer), hours worked, rate of pay, and union status (1 if the employee is in the union, 0 if not). For example, here are data values for two typical workers: 123 47 12.00 1 (ID # 123 is in the union) 456 32.5 3.50 0 (ID # 456 is not in the union) Let your prompt guide the user to enter a 3-digit ID number. Your program should NOT test whether the ID number is in a valid range.
2. The program will compute the weekly pay, which includes an overtime bonus; an employee who works more than 40 hours per week is paid time and a half for each hour over 40. Your computation should use the following formula: if hours are 40 or less, weekly pay is the number of hours times the rate of pay but if hours are more than 40, weekly pay is 40 times the rate of pay plus the number of overtime hours times 1.5 times the rate of pay For example, id # 123, who worked 47 hours, would earn 40 * 12.00 + 7 * 1.5 * 12.00 id # 456, who worked 32.5 hours, would earn 32.5 * 3.50 (Another way of computing overtime is to figure that an employee who works 47 hours at 12.00 per hour earns 12.00 for each of 47 hours, and another half of 12.00 [or 6.00] for 7 hours.)
3. The program will compute the union dues, which are 10% of the weekly pay for anyone who is in the union, but $15 (note, this is dollars, not percent) for anyone who is not in the union.
4. The program will compute net pay, which is weekly pay minus the union dues.
5. The program will print all of the data values read in and the words "union" or "non-union", plus each item that was computed (weekly pay, dues, and net pay). Make sure that all monetary amounts have exactly 2 decimal places and a dollar sign.
6. Then the program will skip two lines and go to the next employee.
After the last employee has been read, the program should print the total number of employees that have been processed. (This count should not include a phony employee used to end the set of data.)
Your program must use the phony value method to determine that the last employee has been read. Explain this in a comment. If you submit the program to CodeLab, CodeLab expects your phony value to be -1.
Your program should use an if-else statement instead of two if statements wherever possible.
OUTPUT FORMAT: Here is a sample set of output for two employees (note that your messages must be the same as these if you are going to test your answers in CodeLab; note that hours also has 2 decimal places). Don=t forget to set the number of decimal places in the output file!
employee 123 worked 47.00 hours at $12.00 per hour union weekly pay $606.00 dues $60.60 net pay $545.40 employee 456 worked 32.50 hours at $3.50 per hour non-union weekly pay $113.75 dues $15.00 net pay $98.75 We processed 2 employees In the final version, this output will go a file; the prompts and entered data values will go to the screen. You should copy the screen output to a separate file and hand in both. Make sure that the screen output has a name (like screen2.txt or output2.txt) that identifies it.
DATA: Have at least 10 employees.
Make sure that at least 4 are in the union and at least 4 are not. Make at least 4 worked overtime and 4 did not (and cover all possible combinations of union status and overtime). Make sure that at least one of your set of overtime values consists of hours = 41 and rate = 10) so that I can check the calculations by hand, but also make sure that a few have decimal places (e.g., 6.50 per hour or 30.5 hours). Use different values for hours and rate for each employee.
optional:
1. Determine the total weekly payroll (total pay BEFORE subtracting the dues) for all employees added together). Print this at the end.
2. If a person's union status is not one of the allowed values (1 or 0), print an error message to the file (make the message say what is wrong; don=t just print "error"). In this case, set the dues to 0). Also send the error message to the file with the rest of the output.
Explanation / Answer
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
int main()
{
int ident_code, unnion, ;
double hours_worked, ratepay, dues, net_pay, pay, overtime, overtimepay;
ifstream infile;
ofstream outfile;
infile.open("c:\input.txt");
cout << " ******************************************************** " << endl;
if (!infile)
{
cout << " Cannot open the input file. " << endl;
cout << endl;
return 1;
}
outfile.open("c:\output.txt");
outfile << fixed << showpoint;
outfile << setprecision(2);
infile >> ident_code >> hours_worked >> ratepay >> unnion;
cout << " Processing Data.... " << endl;
outfile <<" Weekly Payroll " << endl;
outfile << endl;
outfile << endl;
outfile << "ID No. Hours Rate Pay Member Dues NetPay " << endl;
outfile << endl;
while (infile)
{
if (hours_worked <= 40)
{
pay = hours_worked * ratepay;
}
else if (hours_worked > 40)
{
overtime = hours_worked - 40;
overtimepay = overtime * ratepay * 1.5;
pay = hours_worked * ratepay + overtimepay;
}
if (unnion == 1)
{
dues = .10 * pay;
net_pay = pay - dues;
}
if (unnion == 0)
{
dues = pay - 5.00;
net_pay = pay - dues;
}
outfile << ident_code << setw(16) << hours_worked << setw(13) << ratepay
<< setw(13)<< pay << setw(8)<< unnion << setw(16)<< dues<< setw(13) << net_pay ;
break;
infile >> ident_code >> hours_worked >> ratepay >> unnion;
}
cout << " Please Check the Output File! " << endl;
cout << endl;
cout<< "Created by : Stanley Patela " << endl;
system("PAUSE");
return 0;
}
Sample Output:
WEEKLY PAYROLL
Union
ID No. Hours Rate Pay Member Dues Net
110 40.00 10.50 420.00 Yes 42.00 378.00
120 36.00 13.00 468.00 Yes 46.80 421.20
135 46.00 11.25 551.25 No 5.00 546.25
140 42.50 12.50 546.88 No 5.00 541.88
142 45.20 13.00 621.40 Yes 62.14 559.26
145 37.00 11.25 416.25 No 5.00 411.25
15 9.00 9.36 84.24 Unknown 0.00 84.24
Totals 3108.02 165.94 22942.08
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.