I am writing a payroll program with inheritance for a school assignment that has the following requirments:
Expand the Employee Payroll program to include hourly based and salary based employees. This phase uses an array of employee objects, inheritance for different classes of employees, and polymorphism for salary computation. The 52 week yearly salary as well as number of overtime hours worked by a salary based employee is given). For salary based employees, to find the regular (gross) pay for a week, divide the salary by 52. To compute the overtime pay for a salary based employee, first find the hourly rate by dividing the gross pay by 40, and then compute overtime pay. For every employee, overtime pay, tax amount, and net pay must also be computed. In addition, the program should find the minimum and maximum net pay of all employees as well as sort the employees based on their net pay (ascending order).
This is my code:
#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
class payroll{
ifstream fin;
public: string employeename;
char paystat;
int n;
float taxrate;
double hoursworked, overtimehours, regularhours;
double hourlyrate,regularpay,totalnetpay,minnp,maxnp;
double avgnetpay,taxamount,netpay,grosspay,overtimepay;
virtual double findgrosspay();
void setvariables(char[],int,char,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();
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::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<100;i++)cout<<*p[i]<<" ";
}//sortfunction
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)<<stat<<
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
findavgnetpay();
cout<<"The average net pay for "<<n<<" employees is $"<<avgnetpay<<endl;
}//PRINTREPORT
int main(){
payroll employee[6],report;
employee[0].printreport();
ifstream fin("salariedemployees.in");
while(fin>>aemployeename>>apaystat>>ahoursworked>>ahourlyrate);{
if(apaystat=='s'){
employee[n]=new salaried();
employee[n]->setvariables(aemployeename,apaystat,ahoursworked,ahourlyrate);
employee[n]->findgrosspay(); }//if s
if(apaystat=='h'){
employee[n]=new hourly();
employee[n]->setvariables(aemployeename,apaystat,ahoursworked,ahourlyrate);
employee[n]->findgrosspay(); }//if h
n++;
}//WHILE
sortnetpay(netpay,n);
fin.close();
system ("pause");
}//MAIN
[B]I have been working on this for a week, and am really stumped! I spent 10 hours on it yesterday getting it to where it's at now. These are my errors:
C:\Dev-Cpp\Module 6 again.cpp In member function `void payroll::sortbypointers()':
101 C:\Dev-Cpp\Module 6 again.cpp cannot convert `double' to `double*' in assignment
C:\Dev-Cpp\Module 6 again.cpp In member function `void payroll::printdata()':
128 C:\Dev-Cpp\Module 6 again.cpp expected primary-expression before '<<' token
C:\Dev-Cpp\Module 6 again.cpp In function `int main()':
151 C:\Dev-Cpp\Module 6 again.cpp `aemployeename' undeclared (first use this function)
151 C:\Dev-Cpp\Module 6 again.cpp `apaystat' undeclared (first use this function)
151 C:\Dev-Cpp\Module 6 again.cpp `ahoursworked' undeclared (first use this function)
151 C:\Dev-Cpp\Module 6 again.cpp `ahourlyrate' undeclared (first use this function)
153 C:\Dev-Cpp\Module 6 again.cpp `n' undeclared (first use this function)
164 C:\Dev-Cpp\Module 6 again.cpp `netpay' undeclared (first use this function)
164 C:\Dev-Cpp\Module 6 again.cpp `sortnetpay' undeclared (first use this function)
Your help is kindly and greatly appreciated.
/B]
p is an array of 100 pointers. On line 101 you are attempting to assign a double to each array element. If that's what you want, then change the array on line 97 to an array of 100 doubles instead of an array of pointers (just remove the star). Then you also have to remove the star on line 102. line 99: variable temp needs to be changed from a pointer to just a double.
You are using std::string in the class but failed to include header file at the top of your program.
You have an extra semicolon in couple of places, e.g.
// the following effectively first reads all the available input and only after that advances further ...
while(fin>>aemployeename>>apaystat>>ahoursworked>>ahourlyrate);{ If you would have a do/while construct, then you'd need the semicolon, i.e.
do
{
// something
}
while(something) <strong>;</strong>Thank you both. When I changed everything you suggested Ancient Dragon, and made line 99 say double temp; (removed the star), and added #include, I still have these errors:
C:\Dev-Cpp\Module 6 again.cpp In member function `void payroll::sortbypointers()':
107 C:\Dev-Cpp\Module 6 again.cpp invalid type argument of `unary *'
107 C:\Dev-Cpp\Module 6 again.cpp invalid type argument of `unary *'
115 C:\Dev-Cpp\Module 6 again.cpp invalid type argument of `unary *'
C:\Dev-Cpp\Module 6 again.cpp In member function `void payroll::printdata()':
129 C:\Dev-Cpp\Module 6 again.cpp expected primary-expression before '<<' token
C:\Dev-Cpp\Module 6 again.cpp In function `int main()':
152 C:\Dev-Cpp\Module 6 again.cpp `aemployeename' undeclared (first use this function)
152 C:\Dev-Cpp\Module 6 again.cpp `apaystat' undeclared (first use this function)
152 C:\Dev-Cpp\Module 6 again.cpp `ahoursworked' undeclared (first use this function)
152 C:\Dev-Cpp\Module 6 again.cpp `ahourlyrate' undeclared (first use this function)
154 C:\Dev-Cpp\Module 6 again.cpp `n' undeclared (first use this function)
165 C:\Dev-Cpp\Module 6 again.cpp `netpay' undeclared (first use this function)
165 C:\Dev-Cpp\Module 6 again.cpp `sortnetpay' undeclared (first use this function)
This is what my code looks like now:
#include<fstream>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class payroll{
ifstream fin;
public: string employeename;
char paystat;
int n;
float taxrate;
double hoursworked, overtimehours, regularhours;
double hourlyrate,regularpay,totalnetpay,minnp,maxnp;
double avgnetpay,taxamount,netpay,grosspay,overtimepay;
virtual double findgrosspay();
void setvariables(char[],int,char,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();
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::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<100;i++)cout<<*p[i]<<" ";
}//sortfunction
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)<<stat<<
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
findavgnetpay();
cout<<"The average net pay for "<<n<<" employees is $"<<avgnetpay<<endl;
}//PRINTREPORT
int main(){
payroll employee[6],report;
employee[0].printreport();
ifstream fin("salariedemployees.in");
while(fin>>aemployeename>>apaystat>>ahoursworked>>ahourlyrate){
if(apaystat=='s'){
employee[n]=new salaried();
employee[n]->setvariables(aemployeename,apaystat,ahoursworked,ahourlyrate);
employee[n]->findgrosspay(); }//if s
if(apaystat=='h'){
employee[n]=new hourly();
employee[n]->setvariables(aemployeename,apaystat,ahoursworked,ahourlyrate);
employee[n]->findgrosspay(); }//if h
n++;
}//WHILE
sortnetpay(netpay,n);
fin.close();
system ("pause");
}//MAINline 107: remove the stars
if (p[j]>p[j+1]){
>>I think maybe I want to keep the pointer array to sort???
you don't need pointers.
Ok, that makes sense to me. However, my variables say they are undeclared? I set the variables on line 16...
1) It never pays to write more than one function/method at a time and then try to debug it. The only exception would be the need to write a default constructor and a method to display the variable members of the class if you don't use your debugger.
2) If you do try doing everything at once and you make a mistake in one part of the program you are likely to make the same mistake again later in the program. The first two errors reported in post #3 are leftovers from changing the array p from type double * to type double. Understanding that p[] will be of type declared for p is a basic concept I frequently forget, and I suspect we all do from time to time.
3) I suspect the error on line 129 reported in post #3 is because stat isn't a member of payroll, but I haven't fixed it with my compiler to prove it
4) The errors for line 151 mean you have to declare a variable so that it is in scope before you try to use it. None of the variables listed are declared in main() before you try to use them.
You haven't declared the variables you use in the main()'s while loop ...
while(fin>>aemployeename>>apaystat>>ahoursworked>>ahourlyrate){
You still an extra semicolon in the while loop of ... payroll::printreport()
You have a member variable paystat but not stat
I changed stat to paystat, and it fixed that problem. In the while loop of payroll::printreport() I took out the semicolon after n=0......so it now reads n=0,totalnetpay=0;.....
I am confused as to where and how I have to declare the variables........
I am confused as to where and how I have to declare the variables........
Before you use them, i.e.
int main()
{
...
string aemployeename;
char apaystat;
double ahoursworked, ahourlyrate;
while(fin>>aemployeename>>apaystat>>ahoursworked>>ahourlyrate)
...Thank you. I also see that I need to declare n in the paystat while loop because I have employee[n].......I tried using char, int, double, float, and all of them gave me more errors.
I declared n by putting int n=0; before string aemployee in the main, but, it made more of a mess.......
I fixed an error by adding double netpays; to main, but, still have the problem with n being undeclared. I declared n in my class.....I am not sure what to do. Can anyone kindly help? It is greatly appreciated!
#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[],int,char,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();
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::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::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
findavgnetpay();
cout<<"The average net pay for "<<n<<" employees is $"<<avgnetpay<<endl;
}//PRINTREPORT
int main(){
payroll employee[6],report;
employee[0].printreport();
double netpay;
string aemployeename;
char apaystat;
double ahoursworked, ahourlyrate;
ifstream fin("salariedemployees.in");
while(fin>>aemployeename>>apaystat>>ahoursworked>>ahourlyrate){
if(apaystat=='s'){
employee[n]=new salaried();
employee[n]->setvariables(aemployeename,apaystat,ahoursworked,ahourlyrate);
employee[n]->findgrosspay(); }//if s
if(apaystat=='h'){
employee[n]=new hourly();
employee[n]->setvariables(aemployeename,apaystat,ahoursworked,ahourlyrate);
employee[n]->findgrosspay(); }//if h
n++;
}//WHILE
fin.close();
system ("pause");
}//MAINThis is my current code:
#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[],int,char,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();
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::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::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
findavgnetpay();
cout<<"The average net pay for "<<n<<" employees is $"<<avgnetpay<<endl;
}//PRINTREPORT
int main(){
payroll *employee[6], *report;
employee[0].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
Any thoughts?
These are my errors:
C:\Dev-Cpp\Module 6 AB.cpp In function `int main()':
150 C:\Dev-Cpp\Module 6 AB.cpp `printreport' has not been declared
150 C:\Dev-Cpp\Module 6 AB.cpp request for member of non-aggregate type before '(' token
161 C:\Dev-Cpp\Module 6 AB.cpp no matching function for call to `payroll::setvariables(char[14], char&, double&, double&)'
note C:\Dev-Cpp\Module 6 AB.cpp:16 candidates are: void payroll::setvariables(char*, int, char, char, char, double, double)
166 C:\Dev-Cpp\Module 6 AB.cpp no matching function for call to `payroll::setvariables(char[14], char&, double&, double&)'
note C:\Dev-Cpp\Module 6 AB.cpp:16 candidates are: void payroll::setvariables(char*, int, char, char, char, double, double)
line 150: you need to use pointer operator instead of dot operator because employee is an array of pointers.
Same line (150): That is going to crash your program big time because employy is an array of uninitialized pointers. All those pointers have to be initialized (allocated) before you can use them.