I am not sure about how to get the text data put in to get the information output of this code
EMPLOYEE INFORMATION FOR TEXT DATA
EMPLOYEE ID-481756
NAME-BONITA KOWALSKI
MARITALSTATUS-SINGLE
HOURS WORKED-43
HOURLYRATE-12
EMPLOYEE ID-481755
NAME-WALT KOWALSKI
MARITALSTATUS-MARRIED
HOURS WORKED-12
HOURLYRATE-10
EMPLOYEE ID-481754
NAME-CHARLES XAVIER
MARITALSTATUS-SINGLE
HOURS WORKED-41
HOURLYRATE-15
EMPLOYEEID-481753
NAME-LOGAN WOLF
MARITALSTATUS-Married
HOURS WORKED-10
HOURLYRATE-9
EMPLOYEEID-481752
NAME-STORM RAIN
MARITAL STATUS-Single
HOURSWORKED-27
HOURLYRATE-10
HERE IS THE CODE I AM USING WHERE THE INFO NEEDS TO BE PUT IN
#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
class payroll{
ifstream fin;
char employeeid[12];
char employeename[20];
char maritalstatus;
int hoursworked, overtime;
double hourlyrate, overtimepay, regularpay, grosspay, taxrate, taxamount,netpay;
void calculategrosspay();
void calculatetax();
void calculatenetpay();
void printheadings();
void printdata();
public:payroll();
~payroll();
void printreport(); };
payroll::payroll(){
fin.open(“payroll,dat”); }//CONSTRUCTOR
payroll::~payroll(){
fin.close(); }//DESTRUCTOR
void payroll::calculategrosspay(){
if(hoursworked>40){
overtime=hoursworked=40;
regularpay=hoursworked*hourlyrate;
overtimepay=overtime*(hourlyrate*1.5);
grosspay=regularpay+overtimepay;}//F
else grosspay=hoursworked*hourlyrate; }//CALCULATEGROSSPAY
void payroll::calculatetax(){
if(grosspay>=500)taxrate=.30;
else if(grosspay>200)taxrate=.20;
else taxrate=.10;
if(maritalstatus=’S’)
taxrate=taxrate+.05;
taxamount=grosspay*taxrate; }//CALCULATETAX
void payroll::calculatenetpay(){
netpay=grosspay-taxamount; }//CALCULATENETPAY
void payroll::printheadings(){
cout<<setw(45)<<“-PAYROLL REPORT-“<<endl;
cout<<“__________________________________”<<endl;
cout<<“NAME EMPLOYEEID HOURSWORKED OVERTIME REGULARPAY OVERTIMEPAY GROSS TAX NETPAY”<<endl;
cout<<“__________________________________”<<endl;
}//PRINTHEADINGS
void payroll::printdata(){
cout<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint);
cout<<setw(6)<<employeename<<setw(12)<<employeeid<<setw(6)<<hoursworked<<setw(3)<<overtime<<setw(8)<<regularpay<<setw(8)<<overtimepay<<setw(8)<<grosspay<<setw(8)<<taxamount<<setw(8)<<netpay<<endl; }//PRINTDATA
void payroll::printreport(){
int i=0;
printheadings();
while(fin>>employeename>>employeeid>>maritalstatus>>hoursworked>>hourlyrate){
calculategrosspay();
calculatetax();
calculatenetpay();
printdata();
i++;}//WHILE
}//PRINTREPORT
int main(){
payroll employee;
employee.printreport() ;//MAIN
return 0; }
“payroll.dat” is what i am assuming would be what the employee data would be provided not sure how to go about this payroll.dat is what the example code for the assignment shows so i need to my own data text or what is being called the CONSTRUCTOR
Expert Answer
1. changed type of id and name to string from char array. Also file name payroll.dat had a comma instead of dot in the constructor.
2. in calculatetax, condition to check martialstatus had a single = sign, changed it to ==
3. overtime and overtimepay need to be reset to 0 for next record, otherwise it will take values previously computed. (in calculategrosspay() ). Also overtime is calculated as hoursworked – 40
4. The most important of all is that we need to extract data from the file based on the format. Since a hyphen i.e. – separates the actual data from its name, we need to tokenize and break the string and extract data. This is the change that is done in loop when each line is read.
Sample output is also attached. Please don’t forget to rate the answer if it helped. Thank you very much.
payroll.cpp
#include<fstream>
#include<iostream>
#include<iomanip>
#include <cstdlib>
using namespace std;
class payroll{
ifstream fin;
string employeeid;
string employeename;
char maritalstatus;
int hoursworked, overtime;
double hourlyrate, overtimepay, regularpay, grosspay, taxrate, taxamount,netpay;
void calculategrosspay();
void calculatetax();
void calculatenetpay();
void printheadings();
void printdata();
public:
payroll();
~payroll();
void printreport();
};
payroll::payroll(){
fin.open(“payroll.dat”);
}//CONSTRUCTOR
payroll::~payroll(){
fin.close();
}//DESTRUCTOR
void payroll::calculategrosspay(){
overtime = overtimepay =0;
if(hoursworked>40){
overtime=hoursworked-40;
hoursworked = hoursworked – overtime; //it will be 40
regularpay=hoursworked*hourlyrate;
overtimepay=overtime*(hourlyrate*1.5);
grosspay=regularpay+overtimepay;
}//F
else
grosspay=hoursworked*hourlyrate;
}//CALCULATEGROSSPAY
void payroll::calculatetax(){
if(grosspay>=500)
taxrate=.30;
else if(grosspay>200)
taxrate=.20;
else
taxrate=.10;
if(maritalstatus==’S’)
taxrate=taxrate+.05;
taxamount=grosspay*taxrate;
}//CALCULATETAX
void payroll::calculatenetpay(){
netpay=grosspay-taxamount;
}//CALCULATENETPAY
void payroll::printheadings(){
cout<<setw(45)<<“-PAYROLL REPORT-“<<endl;
cout<<“__________________________________”<<endl;
cout<<“tNAME tEMPLOYEEID HOURSWORKED OVERTIME REGULARPAY OVERTIMEPAY GROSS TAX NETPAY”<<endl;
cout<<“__________________________________”<<endl;
}//PRINTHEADINGS
void payroll::printdata(){
cout<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint);
cout<<setw(20)<<employeename
<<setw(12)<<employeeid
<<setw(6)<<hoursworked
<<setw(3)<<overtime
<<setw(8)<<regularpay
<<setw(8)<<overtimepay
<<setw(8)<<grosspay
<<setw(8)<<taxamount
<<setw(8)<<netpay<<endl;
}//PRINTDATA
void payroll::printreport(){
int i=0, idx;
printheadings();
char line[256];
string str;
while(fin.getline(line, 256)){
//extract employeeid
str = string(line);
idx = str.find(“-“);
employeeid = str.substr(idx+1);
//extract employeename
fin.getline(line, 256);
str = string(line);
idx = str.find(“-“);
employeename = str.substr(idx+1);
//extract maritalstatus
fin.getline(line, 256);
str = string(line);
idx = str.find(“-“);
maritalstatus = str[idx+1]; //extract just 1 character
//extract hoursworked
fin.getline(line, 256);
str = string(line);
idx = str.find(“-“);
hoursworked = atoi(str.substr(idx+1).c_str());
//extract hourlyrate
fin.getline(line, 256);
str = string(line);
idx = str.find(“-“);
hourlyrate = atof(str.substr(idx+1).c_str());
calculategrosspay();
calculatetax();
calculatenetpay();
printdata();
i++;
}//WHILE
}//PRINTREPORT
int main(){
payroll employee;
employee.printreport() ;//MAIN
return 0;
}
payroll.dat
EMPLOYEE ID-481756
NAME-BONITA KOWALSKI
MARITALSTATUS-SINGLE
HOURS WORKED-43
HOURLYRATE-12
EMPLOYEE ID-481755
NAME-WALT KOWALSKI
MARITALSTATUS-MARRIED
HOURS WORKED-12
HOURLYRATE-10
EMPLOYEE ID-481754
NAME-CHARLES XAVIER
MARITALSTATUS-SINGLE
HOURS WORKED-41
HOURLYRATE-15
EMPLOYEEID-481753
NAME-LOGAN WOLF
MARITALSTATUS-Married
HOURS WORKED-10
HOURLYRATE-9
EMPLOYEEID-481752
NAME-STORM RAIN
MARITAL STATUS-Single
HOURSWORKED-27
HOURLYRATE-10
output
-PAYROLL REPORT-
__________________________________
NAME EMPLOYEEID HOURSWORKED OVERTIME REGULARPAY OVERTIMEPAY GROSS TAX NETPAY
__________________________________
BONITA KOWALSKI 481756 40 3 480.00 54.00 534.00 186.90 347.10
WALT KOWALSKI 481755 12 0 480.00 0.00 120.00 12.00 108.00
CHARLES XAVIER 481754 40 1 600.00 22.50 622.50 217.88 404.62
LOGAN WOLF 481753 10 0 600.00 0.00 90.00 9.00 81.00
STORM RAIN 481752 27 0 600.00 0.00 270.00 67.50 202.50