Problem with variables passing values

Hi all,
The following payroll program assignment compiles and runs, reads input file and outputs the headers, employeeid, and hoursworked fields but no fields that have had math done on them. I believe there is a problem with the way the variables are passing the values. I also commented out for now report->printavgnet because it generates Microsoft errors.Any advice 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
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

class payroll {
ifstream fin;
public: char employeename[14], maritalstatus, paystat;
        int employeeid, i;
        float taxrate;
        double hoursworked, overtimehours, regularhours;
        double hourlyrate, regularpay;
        double totalnetpay, avgnetpay, taxamount, netpay, grosspay, overtimepay;
        virtual double findgrosspay();
        void setvariables(char[], int, char, char, double, double, int);
        void findtaxamount();
        void findnetpay();
        void printheaders();
        void printdata();
        void printavgnet();
        payroll();
        ~payroll();
//        void printreport();
};

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() { 
        grosspay = regularpay / 52;
        overtimehours = hoursworked;
        if (overtimehours > 0) {
           overtimepay = overtimehours * (regularpay / 52 / 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[i] = aemployeename[i];
     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; 
}// findgrosspay

void payroll::findnetpay()  {
     netpay = grosspay - taxamount;
     totalnetpay = totalnetpay + netpay;
}// findnetpay

void payroll::printavgnet() {
     avgnetpay = totalnetpay / i;
     cout<<endl<<"The average net pay for "<<i <<" employees is "
     <<setw(8)<<setprecision(2)<<fixed<<left<<showpoint<<avgnetpay<<endl;
}// printavgnet
     
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 totalnp = 0, averagenp = 0, minnp=1000000, maxnp=0;
    double totalnetpay, 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]->findgrosspay(); }// If s
           if (apaystat == 'h') {
              employee[i] = new hourly();
              employee[i]->findgrosspay(); }// If h
           employee[i]->setvariables(aemployeename, aemployeeid, amaritalstatus, 
                                        apaystat, ahoursworked, ahourlyrate, i);
           employee[i]->findtaxamount();
           employee[i]->findnetpay();
           employee[i]->printdata();
           i++;
     }// While
//     report->printavgnet();
     fin.close();
} //main 


Thanks again.
On Line 126 and 131 just after you create a new salaried or hourly employee
you immediately call employee[1]->findgrosspay().
At this point the newly created employee class variables are just garbage values because nowhere in the constructor(s) do you set default values for variables like grosspay, hoursworked, etc...

I think you should call employee[i]->setvariables() function first BEFORE calling employee[i]->findgrosspay() function.
Note also that the findgrosspay() function for salaried employee has a problem - where is the regularpay value calculated - you use it (regularpay/52) but where does it get it's value from?

also payroll::findgrosspay() should return a value.
Thanks guestgulkan, you hit it on the nose. Now the output is correct except for the employeename. I figure it has to do with having to declare it as a string instead of a char.

Thanks again.
Topic archived. No new replies allowed.