I am just starting to program and have an assignment where we need to use classes for an employee payroll program. Everything is working fine until I try to add a function that calculates the sum of netpays. I think that I need to enter a loop in the findsumnetpay funtion but have not had any luck. Please help to guide me in the right direction.
Here is the code:
Code:
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//file input output stream
//function prototypes
class payroll{
public: ifstream fin;
char employeeid[6];
char employeename[12];
int hoursworked, overtime;
double hourlyrate,overtimepay,regularpay,grosspay,taxrate,taxamount,netpay,sumnetpay;
void calculategrosspay();
void calculatetax();
void calculatenetpay();
double findsumnetpay();
void printheadings();
void printdata();
public:payroll();
~payroll();
void printreport(); };
payroll::payroll(){
fin.open("employee8.in");}
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; }
else grosspay = hoursworked * hourlyrate; }//CALCULATGROSSPAY
void payroll::calculatetax(){
taxamount = grosspay * .30; }//CALCULATETAX
void payroll::calculatenetpay(){
netpay = grosspay - taxamount;
}//CALCULATENETPAY
double payroll::findsumnetpay(){
sumnetpay = sumnetpay+netpay;
return sumnetpay;}//FINDSUMNETPAY
void payroll::printheadings(){
cout<<setw(45)<<"-PAYROLL REPORT-"<<endl;
cout<<"-------------------------------------------------------------------------"<<endl;
cout<<"NAME ID HW RT-PAY OT-PAY GROSS"
" TAX NETPAY"<<endl;
cout<<"-------------------------------------------------------------------------"<<endl;
}//PRINTHEADINGS
void payroll::printdata(){
cout<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint);
cout<<setw(8)<<employeename<<setw(6)<<employeeid<<setw(8)<<hoursworked<<
setw(11)<<regularpay<<setw(11)<<overtimepay
<<setw(10)<<grosspay<<setw(10)<<taxamount<<setw(10)
<<netpay<<endl;}
void payroll::printreport(){
int i;
printheadings();
while(fin>>employeename>>employeeid>>hoursworked>>hourlyrate){
calculategrosspay();
calculatetax();
calculatenetpay();
findsumnetpay();
printdata();
i++; }//WHILE
//FINDSUMNETPAY
}//PRINTREPORT
int main(){
payroll employee;
employee.printreport();
cout<<employee.sumnetpay<<endl;
system("Pause");
}//MAIN