Stuck on Code for Program
LostStudent (27)Jan 6, 2009 at 4:06am UTC
I cleaned up my code so it is easier to read. I am having difficulties with the allocation of employee[6] in my code. When I run the program, I can get the headers to printout, but, that's it. Can anyone kindly help me? I have been working on this now for over 2 weeks and would love to figure it out!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
#include<fstream>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class payroll{
ifstream fin;
int n;
public : string employeename;
char paystat;
float taxrate;
double hoursworked, overtimehours, regularhours;
double hourlyrate,regularpay,totalnetpay,minnp,maxnp;
double avgnetpay,taxamount,netpay,grosspay,overtimepay;
virtual double findgrosspay();
void setvariables(char [],char ,double ,double );
virtual double findtaxamount();
virtual double findnetpay();
virtual double findavgnetpay();
void printheaders();
void printdata();
double minnet(double , int );
double maxnet(double , int );
void printminmax(double , double );
void printreport();
void sortbypointers(void );
payroll();
~payroll();
};
class hourly: public payroll
{
public :
double findgrosspay()
{
if (hoursworked > 40)
{
overtimehours=hoursworked-40;
regularpay=hoursworked*hourlyrate;
overtimepay=overtimehours*(hourlyrate*1.5);
grosspay=regularpay+overtimepay;
}
else
{
grosspay=hoursworked*hourlyrate;
regularpay=grosspay;
return grosspay;
}//IF
};//findgrosspay
};
class salaried:public payroll
{
public : double findgrosspay()
{
if (hoursworked>0)
{
overtimepay=hoursworked*(regularpay/52/40);
regularpay=hourlyrate/52;
grosspay=regularpay+overtimepay;
return grosspay;
}//If
};//findgrosspay
};
payroll::payroll(){
fin.open("salariedemployees.in" ); }
payroll::~payroll() {
fin.close();}
double payroll::findtaxamount()
{
taxrate=.30;
taxamount=grosspay*taxrate;
return taxamount;
}//findtaxamount
double payroll::findgrosspay()
{
if (hoursworked>0)
{
overtimepay=hoursworked*(regularpay/52/40);
regularpay=hourlyrate/52;
grosspay=regularpay+overtimepay;
return grosspay;
}//If
}//findgrosspay
double payroll::findnetpay()
{
netpay=grosspay-taxamount;
totalnetpay=totalnetpay+netpay;
return netpay;
}//findnetpay
double payroll::findavgnetpay()
{
avgnetpay=totalnetpay/n;
cout<<endl<<"The average net pay for " <<n<<" employees is "
<<setw(8)<<setprecision(2)<<fixed<<left<<showpoint<<avgnetpay<<endl;
return avgnetpay;
}//findavgnetpay
double payroll::minnet(double minnp, int n)
{
if (n==0)
{
minnp=1000000;
}
if (netpay<minnp) {minnp=netpay;}
cout<<endl<<"The minimum net pay for " <<n<<" employees is"
<<setw(8)<<setprecision(2)<<fixed<<left<<showpoint<<minnp<<endl;
return minnp;
}//minnet
double payroll::maxnet(double maxnp, int n)
{
if (n==0) {maxnp=3000000;}
if (netpay>maxnp) {maxnp=netpay;}
cout<<endl<<"The maximum net pay for " <<n<<"employees is"
<<setw(8)<<setprecision(2)<<fixed<<left<<showpoint<<maxnp<<endl;
return maxnp;
}//maxnp
void payroll:: sortbypointers()
{
cout << "Before sorting by pointer:" << endl;
double p[100];
int i,j;
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]<<" " ;
}//sortfunction
void payroll::setvariables(char aemployeename[],
char apaystat, double ahoursworked, double ahourlyrate)
{
employeename = aemployeename;
paystat = apaystat;
hoursworked = ahoursworked;
hourlyrate = ahourlyrate;
}// setvariables
void payroll::printheaders()
{
cout<<setw(40)<<"-PAYROLL REPORT-" <<endl;
cout<<"------------------------------------------------------------------" <<endl;
cout<<" NAME STAT HW HR REGP OT-PAY GROSS TAX NETPAY" <<endl;
cout<<"------------------------------------------------------------------" <<endl;
}//PRINTHEADINGS
void payroll::printdata()
{
cout<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint);
cout<<setw(6)<<employeename<<setw(6)<<paystat<<
setw(2)<<hoursworked<<setw(8)<<
hourlyrate<<setw(8)<<regularpay<<setw(8)<<overtimepay<<setw(8)<<grosspay<<
setw(8)<<taxamount<<setw(8)<<netpay<<endl;
}//PRINTDATA
void payroll::printreport()
{
n=0,totalnetpay=0;
printheaders();
while (fin>>employeename>>paystat>>hoursworked>>hourlyrate)
{
findgrosspay();
findtaxamount();
findnetpay();
printdata();
sortbypointers();
n++;
}//WHILE
avgnetpay=findavgnetpay();
cout<<"The average net pay for " <<n<<" employees is $" <<avgnetpay<<endl;
}//PRINTREPORT
int main()
{
payroll *employee[6], *report;
report=new payroll();
employee[6]->printreport();
int i=0;
char aemployeename[14],apaystat;
double ahoursworked, ahourlyrate, minnp,maxnp, netpays[6];
void sortnetpays(double netpays[], int i);
report->printheaders();
ifstream fin;
fin.open("salariedemployees.in" );
while (fin>>aemployeename>>apaystat>>ahoursworked>>ahourlyrate)
{
if (apaystat=='s' )
{
employee[i]=new salaried();
employee[i]->setvariables(aemployeename,apaystat,ahoursworked,ahourlyrate);
employee[i]->findgrosspay();
}//if s
if (apaystat=='h' )
{
employee[i]=new hourly();
employee[i]->setvariables(aemployeename,apaystat,ahoursworked,ahourlyrate);
employee[i]->findgrosspay();
}//if h
employee[i]->findtaxamount();
netpays[i]=employee[i]->findnetpay();
minnp = employee[i]->minnet(minnp, i);
maxnp = employee[i]->maxnet(maxnp, i);
employee[i]->printdata();
i++;
}//WHILE
fin.close();
system ("pause" );
}//MAIN
guestgulkan (481)Jan 6, 2009 at 4:06am UTC
Line 199:
employee[6]->printreport(); Software crash - the Employee array of pointers is uninitialised. Line 203:
void sortnetpays(double netpays[], int i); what is this all about??
Last edited on Jan 6, 2009 at 4:06am UTC
guestgulkan (481)Jan 6, 2009 at 4:06am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
int main()
{
payroll *employee[6], *report;
report=new payroll();
//report->printreport();/* nonsense statement as this will just print rubbish */
//employee[6]->printreport(); //error - array unitialised - software crash
int i=0;
char aemployeename[14],apaystat;
double ahoursworked, ahourlyrate, minnp,maxnp, netpays[6];
//sortnetpays( netpays[6], i); //what is this??
report->printheaders();
ifstream fin;
fin.open("salariedemployees.in" );
while (fin>>aemployeename>>apaystat>>ahoursworked>>ahourlyrate)
{
if (apaystat=='s' )
{
employee[i]=new salaried();
employee[i]->setvariables(aemployeename,apaystat,ahoursworked,ahourlyrate);
employee[i]->findgrosspay();
}//if s
if (apaystat=='h' )
{
employee[i]=new hourly();
employee[i]->setvariables(aemployeename,apaystat,ahoursworked,ahourlyrate);
employee[i]->findgrosspay();
}//if h
employee[i]->findtaxamount();
netpays[i]=employee[i]->findnetpay();
/* problem with the next two statments - you are calling the minnet and maxnet functions with the
uninitialised variable minnp and maxnp*/
minnp = employee[i]->minnet(minnp, i);
maxnp = employee[i]->maxnet(maxnp, i);
/*Printdata function has a problem - because it tries to print out overtime
pay which hasn't been calculated - so it just print rubbish for that value*/
employee[i]->printdata();
i++;
}//WHILE
fin.close();
system ("pause" );
}//MAIN
Last edited on Jan 6, 2009 at 4:06am UTC
guestgulkan (481)Jan 6, 2009 at 4:06am UTC
salaried::findgrosspay and
hourly::findgrosspay and
payroll::findgrosspayfunctions are not returing their values properly. all paths through those functions should return a value. I aslo believe your method of calculating grosspay is way off the mark (by the way in the file is the hours worked the number of hours for the year??)
Last edited on Jan 6, 2009 at 4:06am UTC
LostStudent (27)Jan 6, 2009 at 4:06am UTC
The file is employee name, pay status, hours worked per week and hourly wage.
This topic is archived - New replies not allowed.