0
#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
          
          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

Thank you! Now the problem seems to be that the program stops working. When I execute the program, I get a blank black output screen then a windows popup that says the program has stopped working and will close. Did I fix the code properly?

1

At least a couple of severe errors ...

...
payroll *employee[6], *report;
// here you call printreport() through an uninitialized/unallocated pointer, 
// it is a big no-no 
employee[0]->printreport();
int i=0;
char aemployeename[14],apaystat;
double ahoursworked, ahourlyrate, minnp,maxnp, netpays[6];
void sortnetpays(double netpays[], int i);
// here you do it again ...
report->printheaders(); 
...

Last but not least, I'd really suggest you to format the code in order to make it readable. You'd be doing a favour to yourself first and foremost, I think.

Votes + Comments
A lot of people underestimate the benefit of code-formatting
0

Would the corrections be to make the code printreport(); and printheaders();?

I do need to clean up my code, and will as soon as I have a couple of hours to sit and do it. I am trying to get the program to execute correctly. Thank you kindly.

0

Would the corrections be to make the code printreport(); and printheaders();?

I do need to clean up my code, and will as soon as I have a couple of hours to sit and do it. I am trying to get the program to execute correctly. Thank you kindly.

You'd probably just want to delete those two faulty lines altogether, since you don't have any data gathered yet.

0

When I deleted those lines the program compiled without errors, but, all I got is a blank black output screen. How can I get my data to printout? Thank you.

0

Ok, when I executed the following, the output screen printed the headers and then a pop up screen says program stopped working. Can anyone help? Thank you :).

#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
          
          findavgnetpay();
          cout<<"The average net pay for "<<n<<" employees is $"<<avgnetpay<<endl;
          }//PRINTREPORT
          int main(){
               payroll *employee[6], *report;
               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
0

main.cpp
*** {BD Software Proxy c++ v3.43a for gcc} STL Message Decryption is ON! ***
main.cpp:6: warning: `payroll' has virtual functions but non-virtual destructor
main.cpp:31: warning: `hourly' has virtual functions but non-virtual destructor
main.cpp:45: warning: `salaried' has virtual functions but non-virtual
destructor
main.cpp: In member function `virtual double payroll::findgrosspay()':
main.cpp:73: warning: control reaches end of non-void function
main.cpp: At global scope:
main.cpp:73: error: extra `;'
*** Errors occurred during this build ***

Did I miss a post of the input file?

0

Whoa!

payroll *employee[6], *report;
   employee[6]->printreport();

You've got employee as an uninitialized array of pointers. And in employee[6]->printreport(); no such critter as employee[6] exists.

0

This is where I am stuck. If I remove the stars I have all sorts of errors because of the ->. I am not sure what to do to fix the problem so that my output will print? Thank you.

0

Can anyone please help? Thank you very much :).

You might post the current (nicely formatted ;)) code and explain in more detail what you are stuck with...

0

Ok, I think I cleaned up my code so it is easier to read. The problem I am having is that the only data that is printing out are my headers. My input file is not being pulled in, and none of my other cout statements are printing either. The compiler executes, prints out the headers and then says the program stopped working. Can anyone please help? I do not understand what to do at this point. Thanks greatly!

#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;
               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
0

Same issue:

Whoa!

payroll *employee[6], *report;
   employee[6]->printreport();

You've got employee as an uninitialized array of pointers. And in employee[6]->printreport(); no such critter as employee[6] exists.

0
employee[6]->printreport();
report->printheaders();

You cannot make the above function calls because the pointers are not pointing to allocated objects. I.e. you need to do e.g.

report = new payroll();
// ... now you can utilize the 'report' pointer 
report->printheaders();

Note that the indexes are zero-based i.e. if you have employee[ [B]6[/B] ] , then the last valid index is 5 (not 6).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.