Write a C++ program that will read information from an input file named employee
ID: 3624892 • Letter: W
Question
Write a C++ program that will read information from an input file named employee_info.dat and that will create an output file named lnam_pay.out, where lnam corresponds to the first four letters of your last name. The output file should contain payroll information for various employees in a neat, formatted form.
Physical name of input file: employee_info.dat
The program should read from each line of the input file an employee identification number (an integer), the hours worked, and the number of dependents. Each employee is paid $10.50 per hour for regular hours and $15.25 per hour for overtime hours. From the worker’s gross pay, 8% is withheld for social security tax, 15% is withheld for federal income tax, and 7% is withheld for state income tax. If the worker has four or more dependents, then an additional $35 is withheld to cover the extra cost of medical insurance.
Your program should write to the output file the employee identification number, the gross pay, and the net pay. This process should continue until an identification number of zero is entered.
Design your output file to have a heading line and to be formatted similar to below:
Employee Deps Hours Gross Pay Net Pay
======== ==== ===== ========= =======
12345 2 30 315.00 220.50
11335 1 46 511.50 358.05
44444 5 40 420.00 259.00
etc.
Program Requirements:
1) Comments to include your name, class, assignment number, point value, etc. Also, use comments to explain the action of your code.
2) Name your output file lnam_pay.out, where lnam corresponds to the first four letters of your last name.
3) Use the setw and setprecision manipulators as well as the iosflags to format the output.
4) Use a sentinel-controlled while loop to control the program’s execution.
Explanation / Answer
//header section
#include < iostream >
#include < fstream >
#include < string >
#include < iomanip >
using namespace std;
//employee structure
struct Employee
{
int idNum;
int wHours;
int nDependents;
double grosspay;
double netpay;
};
//main function
int main()
{
//constant variables for tax rates
const double sstax=0.08;
const double ftax=0.15;
const double stax=0.07;
char *lastname;
lastname=new char[4];
char filename[13];
//promt user to enter filename for filename
cout<<"Enter your lastname: ";
cin>>lastname;
strcpy(filename,lastname);
strcat(filename,"_pay.txt");
//input file
fstream infile("empdata.txt",ios::in);
//output file
fstream outfile(filename,ios::out);
struct Employee emp;
//writing data to file
outfile<<"Employe Deps Hours Gross Pay Net Pay"<<endl;
outfile<<"----------------------------------------"<<endl;
//reading each record from file until end of file
while(!infile.eof())
{
infile>>emp.idNum;
infile>>emp.nDependents;
infile>>emp.wHours;
if(emp.wHours<=40)//verifying for extra hours i assume here 40 hours are normal hours
{
emp.grosspay=emp.wHours*10.5;
}
else
{
emp.grosspay=(emp.wHours-40)*15.25+(40*10.5);
}
double tempTax=emp.grosspay*sstax+emp.grosspay*ftax+
emp.grosspay*stax;
emp.netpay=emp.grosspay-tempTax;
//verifying for number of dependants
if(emp.nDependents>=4)
emp.netpay=emp.netpay-35;
//placing formatted out put to file
outfile<<setw(6)<<emp.idNum<<" "<<setw(3)
<<emp.nDependents<<" "<<setw(3)<<emp.wHour
<<" "<<emp.grosspay<<" "<<emp.netpay<<endl;
}
outfile<<"etc..";
system("pause");
}//end of main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.