My code needs to include average netpay and I belive that is all that it is miss
ID: 3846074 • Letter: M
Question
My code needs to include average netpay and I belive that is all that it is missing. so the output needs to display the average netpay and that seems to be the only missing component, using DEVC++ compiler.
MY code
#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<<" 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(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;
}
and my payroll.dat is
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
Explanation / Answer
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
class employee{
ifstream fin;
public: char employeename[20];
int hoursworked,overtime;
double hourlyrate, overtimepay, regularpay, grosspay, taxrate, taxamount, netpay;
void calculategrosspay();
void calculatetax();
void calculatenetpay();
void printheadings();
void printdata();
void sortbypointers(); // ******** this line was missing
public: employee();
~employee();
void printreport();
}; // CLASS EMPLOYEE
employee::employee(){
fin.open("employee.txt");
} //constructor
employee::~employee(){
fin.close();
} //destructor
class hourly: public employee{
public: double calculategrosspay(){
if(hoursworked > 40){
int overtimehours=hoursworked-40;
regularpay=hoursworked*hourlyrate;
overtimepay=overtimehours*(hourlyrate*1.5);
grosspay=regularpay+overtimepay;}
else{
grosspay=hoursworked*hourlyrate;
regularpay=grosspay;
return grosspay;
}//END IF
}//calculate grosspay for hourly employee
};//Hourly employee Object
class salaried:public employee{
public:
void calculategrosspay(){
if(hoursworked>0){
overtimepay=hoursworked*(regularpay/52/40);
regularpay=hourlyrate/52;
grosspay=regularpay+overtimepay;
return ;
}//If
}//calculate grosspay for salaried employee
};//Salaried employee Object
void employee::calculatetax() {
taxrate = .30;
taxamount=grosspay*taxrate;
return ;
} //CALCULATE TAX
void employee::calculatenetpay() {
netpay = grosspay -taxamount;
return ;
} //CALCULATE NETPAY
void employee:: sortbypointers(){
cout << "Before sorting by pointer:" << endl;
double p[100];
int i,j, n;
double temp;
int sortedflag=0;
for(i=0;i<n;i++) p[i]=netpay+i; //INITIALIZING POINTER ARRAY
for(i=0;i<n;i++)cout<< "$" << p[i]<<" ";
while (!sortedflag){
sortedflag=1;
for(j=0;j<n-1;j++ ){
if (p[j]>p[j+1]){
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
sortedflag=0;
}//SWAP
}//J
}//I
cout<<endl<<"SORTED ARRAY:";
for(i=0;i<n;i++)cout<<p[i]<<" ";
}//sort function
void employee::printheadings(){
cout<<setw(45)<<"-PAYROLL REPORT-"<<endl;
cout<<setw(9)<<"EMPLOYEE NAME"<<setw(5)<<"HW"<<setw(6)<<"HR"<<setw(6)<<"OTP"<<setw(9)<<"GROSS"<<setw(6)<<"TAX"<<setw(11)<<"NET PAY"<<endl<<endl;
} //PRINT HEADINGS
void employee::printdata(){
cout<<setw(6)<<employeename<<setw(12)<<hoursworked<<setw(6)<<hourlyrate<<setw(8)<<overtimepay<<setw(8)<<grosspay<<setw(8)<<taxamount<<setw(8)<<netpay<<endl;
} //PRINT DATA
void employee::printreport(){
int i = 0;
double sum;
printheadings();
while(fin>>employeename>>hoursworked>>hourlyrate){
void calculategrosspay();
calculatetax();
calculatenetpay();
printdata();
sum = sum + netpay; //calculate netpay sum
i++;
} //END WHILE
cout<<"Average Net Pay = "<<sum/i<<endl; //calculate and print the average netpay
return;
} //PRINT REPORT
int main(){
employee payroll;
payroll.printreport();
} //MAIN
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.