Problem with function
archer86 (15)Jan 6, 2009 at 11:10pm UTC
I cannot get the function highlownetpay to work. Tried many variations with same results. Output produces initialized values 1000000 and 0 for minnp and maxnp variables. I don't believe netpay is being passed to the function from the main. Any insight would be greatly appreciated. Thanks in advance.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
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
class payroll {
ifstream fin;
public : string employeename;
char maritalstatus, paystat;
int employeeid, i;
float taxrate;
double hoursworked, overtimehours, regularhours;
double hourlyrate, regularpay, minnp, maxnp;
double avgnetpay, taxamount, netpay, grosspay, overtimepay;
virtual double findgrosspay();
void setvariables(char [], int , char , char , double , double , int );
void findtaxamount();
double findnetpay();
void printheaders();
void printdata();
double highlownetpay(double , double , double );
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;
};// findgrosspay
};
class salaried : public payroll {
public : double findgrosspay() {
regularpay = hourlyrate / 52; //For salaried employees,
// hourlyrate is read in from input as annual salary.
overtimehours = hoursworked - 40;
grosspay = regularpay;
if (overtimehours > 0) {
overtimepay = overtimehours * (regularpay / 40);
grosspay = regularpay + overtimepay;
return grosspay;
}// If
};//findgrosspay
};
payroll::payroll() {
fin.open("payroll16.dat" ); }
payroll::~payroll() {
fin.close(); }
void payroll::setvariables(char aemployeename[], int aemployeeid, char amaritalstatus,
char apaystat, double ahoursworked, double ahourlyrate, int i) {
employeename = aemployeename;
employeeid = aemployeeid;
maritalstatus = amaritalstatus;
paystat = apaystat;
hoursworked = ahoursworked;
hourlyrate = ahourlyrate;
}// setvariables
void payroll::findtaxamount() {
taxrate = .30;
taxamount = grosspay * taxrate;
}// findtaxamount
double payroll::findgrosspay() {
overtimehours = 0;
regularpay = 0;
overtimepay = 0;
grosspay = 0;
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;
}// findgrosspay
double payroll::findnetpay() {
netpay = grosspay - taxamount;
return netpay;
}// findnetpay
double payroll::highlownetpay(double netpay, double maxnp, double minnp) {
if (netpay < minnp) {minnp = netpay;}
if (netpay > maxnp) {maxnp = netpay;}
return minnp, maxnp;
}// highlownetpay
void payroll::printheaders() {
cout<<setw(45)<<"-PAYROLL REPORT-" <<endl<<endl;
cout<<" NAME ID HW OT RT-PAY OT-PAY GROSS"
" TAX NETPAY" <<endl;
cout<<"-----------------------------------------------------------------" <<endl;
}//printheaders
void payroll::printdata() {
cout<<setw(10)<<left<<employeename<<setw(4)<<employeeid;
cout<<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint);
cout<<setw(6)<<right<<hoursworked<<setw(5)<<overtimehours<<setw(8)
<<regularpay<<setw(8)<<overtimepay<<setw(8)<<grosspay<<setw(8)
<<taxamount<<setw(8)<<netpay<<endl;
}//printdata
int main() {
payroll *employee[6], *report;
char aemployeename[14], amaritalstatus, apaystat;
int aemployeeid, i=0;
double ahoursworked, ahourlyrate;
double minnp=1000000, maxnp=0;
double avgnetpay, taxamount, netpay, grosspay, overtimepay;
report->printheaders();
ifstream fin;
fin.open("payroll16.dat" );
while (fin>>aemployeename>>aemployeeid>>amaritalstatus>>apaystat
>>ahoursworked>>ahourlyrate) {
if (apaystat == 's' ) {
employee[i] = new salaried();
employee[i]->setvariables(aemployeename, aemployeeid, amaritalstatus,
apaystat, ahoursworked, ahourlyrate, i);
employee[i]->findgrosspay(); }// If s
if (apaystat == 'h' ) {
employee[i] = new hourly();
employee[i]->setvariables(aemployeename, aemployeeid, amaritalstatus,
apaystat, ahoursworked, ahourlyrate, i);
employee[i]->findgrosspay(); }// If h
employee[i]->findtaxamount();
employee[i]->findnetpay();
employee[i]->highlownetpay(netpay, maxnp, minnp);
employee[i]->printdata();
i++;
}// While
cout<<endl<<"Minimum net pay is: $" <<minnp<<endl;
cout<<"Maximum net pay is: $" <<maxnp<<endl;
fin.close();
} //main
Last edited on Jan 6, 2009 at 11:10pm UTC
Zaita (1576)Jan 6, 2009 at 11:10pm UTC
I would use some debugging
cout statements to ensure your variables are being passed as you have requested.
guestgulkan (481)Jan 6, 2009 at 11:10pm UTC
Are you trying to return two values from the function by value?????
Last edited on Jan 6, 2009 at 11:10pm UTC
archer86 (15)Jan 6, 2009 at 11:10pm UTC
Yes, I'm trying to return maxnp and minnp, it 's the netpay value that's not being passed to the function. maxnp and minnp are being passed.
Faldrax (322)Jan 6, 2009 at 11:10pm UTC
The problem is that the class payroll has a netpay variable - this is the one which is being used in the method findnetpay(). The netpay declared in main is never initialised. Change line 140 to
netpay = employee[i]->findnetpay(); It looks like you are doing something similar for findgrosspay (same varaible in class and main) and possibly elsewhere. It will be worth taking a little time to tidy the varaible up, to be sure it is always doign what you expect.
archer86 (15)Jan 6, 2009 at 11:10pm UTC
I've made several changes and hopefully, straightened out the variable issue. Since C++ won't let me initialize the variables minnp and maxnp in the class payroll declaration, when and where is it proper to initialize them?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
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
class payroll {
ifstream fin;
public : string employeename;
char maritalstatus, paystat;
int employeeid, i;
float taxrate;
double hoursworked, overtimehours, regularhours;
double hourlyrate, regularpay, minnp, maxnp;
double avgnetpay, taxamount, netpay, grosspay, overtimepay;
virtual double findgrosspay();
void setvariables(char [], int , char , char , double , double );
void findtaxamount();
double findnetpay();
void printheaders();
void printdata();
void highlownetpay();
void printminmax();
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;
};// findgrosspay
};
class salaried : public payroll {
public : double findgrosspay() {
regularpay = hourlyrate / 52; //For salaried employees,
// hourlyrate is read in from input as annual salary.
overtimehours = hoursworked - 40;
grosspay = regularpay;
if (overtimehours > 0) {
overtimepay = overtimehours * (regularpay / 40);
grosspay = regularpay + overtimepay;
return grosspay;
}// If
};//findgrosspay
};
payroll::payroll() {
fin.open("payroll16.dat" ); }
payroll::~payroll() {
fin.close(); }
void payroll::setvariables(char aemployeename[], int aemployeeid, char amaritalstatus,
char apaystat, double ahoursworked, double ahourlyrate) {
employeename = aemployeename;
employeeid = aemployeeid;
maritalstatus = amaritalstatus;
paystat = apaystat;
hoursworked = ahoursworked;
hourlyrate = ahourlyrate;
}// setvariables
void payroll::findtaxamount() {
taxrate = .30;
taxamount = grosspay * taxrate;
}// findtaxamount
double payroll::findgrosspay() {
overtimehours = 0;
regularpay = 0;
overtimepay = 0;
grosspay = 0;
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;
}// findgrosspay
double payroll::findnetpay() {
netpay = grosspay - taxamount;
return netpay;
}// findnetpay
void payroll::highlownetpay() {
if (netpay < minnp) {minnp = netpay;}
if (netpay > maxnp) {maxnp = netpay;}
}// highlownetpay
void payroll::printheaders() {
cout<<setw(45)<<"-PAYROLL REPORT-" <<endl<<endl;
cout<<" NAME ID HW OT RT-PAY OT-PAY GROSS"
" TAX NETPAY" <<endl;
cout<<"-----------------------------------------------------------------" <<endl;
}//printheaders
void payroll::printdata() {
cout<<setw(10)<<left<<employeename<<setw(4)<<employeeid;
cout<<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint);
cout<<setw(6)<<right<<hoursworked<<setw(5)<<overtimehours<<setw(8)
<<regularpay<<setw(8)<<overtimepay<<setw(8)<<grosspay<<setw(8)
<<taxamount<<setw(8)<<netpay<<endl;
}//printdata
void payroll::printminmax() {
cout<<endl<<"Minimum net pay is: $" <<minnp<<endl;
cout<<"Maximum net pay is: $" <<maxnp<<endl;
}// printminmax
int main() {
payroll *employee[6], *report;
char aemployeename[14], amaritalstatus, apaystat;
int aemployeeid, i=0;
double ahoursworked, ahourlyrate;
// double minnp=1000000, maxnp=0;
report->printheaders();
ifstream fin;
fin.open("payroll16.dat" );
while (fin>>aemployeename>>aemployeeid>>amaritalstatus>>apaystat
>>ahoursworked>>ahourlyrate) {
if (apaystat == 's' ) {
employee[i] = new salaried();
employee[i]->setvariables(aemployeename, aemployeeid, amaritalstatus,
apaystat, ahoursworked, ahourlyrate);
employee[i]->findgrosspay(); }// If s
if (apaystat == 'h' ) {
employee[i] = new hourly();
employee[i]->setvariables(aemployeename, aemployeeid, amaritalstatus,
apaystat, ahoursworked, ahourlyrate);
employee[i]->findgrosspay(); }// If h
employee[i]->findtaxamount();
employee[i]->findnetpay();
employee[i]->highlownetpay();
employee[i]->printdata();
i++;
}// While
report->printminmax();
fin.close();
} //main
Last edited on Jan 6, 2009 at 11:10pm UTC
Faldrax (322)Jan 6, 2009 at 11:10pm UTC
The standard place to initialise such varaibles is in the class constructor.
maan (9)Jan 6, 2009 at 11:10pm UTC
respected experts, plz tell me the little code for that i want to get a name from the user and then print it on the screen in revers order thanks.
Zaita (1576)Jan 6, 2009 at 11:10pm UTC
maan: We have to continiously tell you. Please create your own topic under the "Beginners" forum. Do not reply to other peoples topics with your own questions.
archer86 (15)Jan 6, 2009 at 11:10pm UTC
I got it to work!! It was all in passing and returning the values of minnp and maxnp to an from the functions, including the print function. Thanks to all for your help! Revised code: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
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
class payroll {
ifstream fin;
public : string employeename;
char maritalstatus, paystat;
int employeeid, i;
float taxrate;
double hoursworked, overtimehours, regularhours;
double hourlyrate, regularpay, minnp, maxnp;
double avgnetpay, taxamount, netpay, grosspay, overtimepay;
virtual double findgrosspay();
void setvariables(char [], int , char , char , double , double );
void findtaxamount();
double findnetpay();
void printheaders();
void printdata();
double minnet(double , int );
double maxnet(double , int );
void printminmax(double , double );
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;
};// findgrosspay
};
class salaried : public payroll {
public : double findgrosspay() {
regularpay = hourlyrate / 52; //For salaried employees,
// hourlyrate is read in from input as annual salary.
overtimehours = hoursworked - 40;
grosspay = regularpay;
if (overtimehours > 0) {
overtimepay = overtimehours * (regularpay / 40);
grosspay = regularpay + overtimepay;
return grosspay;
}// If
};//findgrosspay
};
payroll::payroll() {
fin.open("payroll16.dat" ); }
payroll::~payroll() {
fin.close(); }
void payroll::setvariables(char aemployeename[], int aemployeeid, char amaritalstatus,
char apaystat, double ahoursworked, double ahourlyrate) {
employeename = aemployeename;
employeeid = aemployeeid;
maritalstatus = amaritalstatus;
paystat = apaystat;
hoursworked = ahoursworked;
hourlyrate = ahourlyrate;
}// setvariables
void payroll::findtaxamount() {
taxrate = .30;
taxamount = grosspay * taxrate;
}// findtaxamount
double payroll::findgrosspay() {
overtimehours = 0;
regularpay = 0;
overtimepay = 0;
grosspay = 0;
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;
}// findgrosspay
double payroll::findnetpay() {
netpay = grosspay - taxamount;
return netpay;
}// findnetpay
double payroll::minnet(double minnp, int i) {
if (i == 0) {minnp = 1000000;}
if (netpay < minnp) {minnp = netpay;}
return minnp;
} //minnet
double payroll::maxnet(double maxnp, int i) {
if (i == 0) {maxnp = 0;}
if (netpay > maxnp) {maxnp = netpay;}
return maxnp;
} // maxnet
void payroll::printheaders() {
cout<<setw(45)<<"-PAYROLL REPORT-" <<endl<<endl;
cout<<" NAME ID HW OT RT-PAY OT-PAY GROSS"
" TAX NETPAY" <<endl;
cout<<"-----------------------------------------------------------------" <<endl;
}//printheaders
void payroll::printdata() {
cout<<setw(10)<<left<<employeename<<setw(4)<<employeeid;
cout<<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint);
cout<<setw(6)<<right<<hoursworked<<setw(5)<<overtimehours<<setw(8)
<<regularpay<<setw(8)<<overtimepay<<setw(8)<<grosspay<<setw(8)
<<taxamount<<setw(8)<<netpay<<endl;
}//printdata
void payroll::printminmax(double minnp, double maxnp) {
cout<<endl<<"Minimum net pay is: $" <<minnp<<endl;
cout<<"Maximum net pay is: $" <<maxnp<<endl;
}// printminmax
int main() {
payroll *employee[6], *report;
char aemployeename[14], amaritalstatus, apaystat;
int aemployeeid, i=0;
double ahoursworked, ahourlyrate, minnp, maxnp;
report->printheaders();
ifstream fin;
fin.open("payroll16.dat" );
while (fin>>aemployeename>>aemployeeid>>amaritalstatus>>apaystat
>>ahoursworked>>ahourlyrate) {
if (apaystat == 's' ) {
employee[i] = new salaried();
employee[i]->setvariables(aemployeename, aemployeeid, amaritalstatus,
apaystat, ahoursworked, ahourlyrate);
employee[i]->findgrosspay(); }// If s
if (apaystat == 'h' ) {
employee[i] = new hourly();
employee[i]->setvariables(aemployeename, aemployeeid, amaritalstatus,
apaystat, ahoursworked, ahourlyrate);
employee[i]->findgrosspay(); }// If h
employee[i]->findtaxamount();
employee[i]->findnetpay();
minnp = employee[i]->minnet(minnp, i);
maxnp = employee[i]->maxnet(maxnp, i);
employee[i]->printdata();
i++;
}// While
report->printminmax(minnp, maxnp);
fin.close();
} //main
tyon (1)Jan 6, 2009 at 11:10pm UTC
I was just wondering are you running this on Microsoft Visual C++ 6?
This topic is archived - New replies not allowed.