Sorting issue

Can you guys take a look at the below? I am to calc netpay given a in file and sort by netpay. The calc's work out, but my netpay doesnt sort. Can you give me some pointers on how to fix the sorting?



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
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

class payroll {
ifstream fin;
public: string lastname; 
char ms, paystatus;
int employeeid, i;
float taxrate;
double hoursworked, overtimehours, regularhours, hourlyrate, regularpay, minnp, maxnp, taxamount, netpay, grosspay, overtimepay, yearlysalary;
virtual double findgrosspay();
double findnetpay(), maxnet(double, int), minnet(double, int);
void settingthevariables(char[], int, char, char, double, double, double);
void sortbypointers();

void printheaders();
void printdata();

void findtaxamount();
void printminmax(double, double);
payroll();
~payroll();
};

class hourly : public payroll {
public: double findgrosspay() { 
        if (hoursworked > 40 ) {
           overtimehours = hoursworked - 40;
           regularpay = 40 * hourlyrate;
           overtimepay = 1.5* hourlyrate * overtimehours;
           grosspay = overtimepay + regularpay  ; }
        else {
            grosspay = hoursworked * hourlyrate;
            regularpay = grosspay; }
            return grosspay; }; };

class salaried : public payroll {
public: double findgrosspay() { 
        regularpay = yearlysalary/52; 
        overtimehours = hoursworked - 40; 
        grosspay = regularpay;
        if (hoursworked > 0) {
        overtimehours = hoursworked - 40;
           overtimepay = 1.5* overtimehours * (regularpay/40);
           grosspay = regularpay + overtimepay;
return grosspay;
        }// If
};//findgrosspay
};

payroll::payroll() {
fin.open("2142011.in"); }
payroll::~payroll() {
fin.close();  }

void payroll::settingthevariables(char alastname[], int aemployeeid, char ams, 
char apaystatus, double ahoursworked, double ahourlyrate, double ayearlysalary) {
     lastname = alastname;
     employeeid = aemployeeid;
     ms = ams;
     paystatus = apaystatus;
     hoursworked = ahoursworked;
     hourlyrate = ahourlyrate;
     yearlysalary=ayearlysalary;
}
   
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 = 1.5* overtimehours * hourlyrate;
        grosspay = regularpay + overtimepay; }
     else
        grosspay = hoursworked * hourlyrate;
        regularpay = grosspay;
return grosspay;
}

double payroll::findnetpay()  {
     netpay = grosspay - taxamount;
return netpay;
}

double payroll::minnet(double minnp, int i) {
     if (i == 0) {minnp = 10000000;}
     if (netpay < minnp) {minnp = netpay;}
return minnp;
} 

double payroll::maxnet(double maxnp, int i) {
     if (i == 0) {maxnp = 0;}
     if (netpay > maxnp) {maxnp = netpay;}
return maxnp;
} 





void payroll:: sortbypointers(){ 
     cout << "Before sorting by pointer:" << endl;    
     double p[100];
     int i,j,n;
  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]<<" ";}



void payroll::printheaders(){

    cout << "------------------------------------------------------------------------------" << endl;
    cout << "NAME      ID     HW      OTW    REG PAY  OT PAY  GROSS   TAX    NETPAY"   << endl;
    cout << "-------------------------------------------------------------------------------" << endl;}

void payroll::printdata()    {
 	 cout<<setw(10)<<left<<lastname<<setw(4)<<employeeid;
   cout<<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint);
 	 cout<<setw(8)<<right<<hoursworked<<setw(8)<<overtimehours<<setw(8)
     <<regularpay<<setw(8)<<overtimepay<<setw(8)<<grosspay<<setw(8)
     <<taxamount<<setw(8)<<netpay<<endl;  
}

void payroll::printminmax(double minnp, double maxnp) {
     cout<<endl<<"The Max. NETPAY is $"<<maxnp<<endl;
     cout<<"The Min. NETPAY is $"<<minnp<<endl;
}

int main() {
    payroll *employee[6], *report;
    char alastname[14], ams, apaystatus;
    int aemployeeid, i=0;
    double ahoursworked, ayearlysalary, ahourlyrate, minnp, maxnp;
    
    report->printheaders();
    
    ifstream fin;
    fin.open("2142011.in");

    while (fin>>alastname>>aemployeeid>>ams>>apaystatus>>ahoursworked>>ahourlyrate>>ayearlysalary) {
           if (apaystatus == 's') {
              employee[i] = new salaried();
              employee[i]->settingthevariables(alastname, aemployeeid, ams, apaystatus, ahoursworked, ahourlyrate, ayearlysalary);
              employee[i]->findgrosspay(); }
           if (apaystatus == 'h') {
              employee[i] = new hourly();
              employee[i]->settingthevariables(alastname, aemployeeid, ams, apaystatus, ahoursworked, ahourlyrate, ayearlysalary);
              employee[i]->findgrosspay(); }
           employee[i]->findtaxamount();
           employee[i]->findnetpay();
           minnp = employee[i]->minnet(minnp, i);
           maxnp = employee[i]->maxnet(maxnp, i);
        
           employee[i]->printdata();
           i++;
     }
     
     report->printminmax(minnp, maxnp);
     
     fin.close(); system ("pause");
} 
Any thoughts on this? Greatly appreciated
Can you please edit your code to only include the section that is causing you problems. And definitions of objects it relies on.
Yes. Please see below:

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
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

class payroll {

void sortbypointers();

void payroll:: sortbypointers(){ 
     cout << "Before sorting by pointer:" << endl;    
     double p[100];
     int i,j,n;
  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]<<" ";}


are you trying to print strings?
Yeah I am trying to print the employee's last name, hours, grosse/ otpay, and netpay. Sorted by netpays
Use the header file (It provides the best strings):
#include<string>

Another thing is that remember that "pointers have to point to something". My compiler is saying that you were using "*report" without initialization. That really bad

Your indentation is horrible and makes this pretty much unreadable.

You're logic is completely wrong. All you are doing (incorrectly) is sorting an array of the current employees netpay + 1 (iterated). This does not iterate over the various Employees at all.

I think you need to break your logic up alot more, think about what you want to achieve and work through it step by step.

http://www.cplusplus.com/doc/tutorial/
Read through the ones of Classes, Arrays and Functions. This will give you a better understanding of how to solve this problem.
Ok, can you guys look at my sorting function? I am having trouble calling it at the end.

I recieve this error
"no matching function for call to `payroll::sortdata(char[14], double&, double&, double&, int&)' "

How can I resolve this?



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
void payroll:: sortdata( string lastname[] ,double overtimepay[],double taxamount[],  double netpay[], int n ) {
    int i,j; 
    string *alastname[n]; 
    double *aovertimepay[n]; 
    double *ataxamount[n];
    double *anetpay[n];
    double *temp, *tempb, *tempc; 
    string *tempa; 
    int sorted = 0; 

      for( i = 0; i < n; i++ ){
         alastname[i] = lastname + i; 
         aovertimepay[i] = overtimepay + i;
         ataxamount[i] = taxamount +i;
         anetpay[i] = netpay + i; } //for
    
 while (!sorted){
         sorted = 1;
   
   for( j = 0; j < n - 1; j++ ){  
      if ( *anetpay[j] > *anetpay[j+1] ){ 

         temp = anetpay[j];
         anetpay[j] = anetpay[j+1];
         anetpay[j+1] = temp;
         
         tempa = alastname[j];
         alastname[j] = alastname[j+1];
         alastname[j+1] = tempa;  
         
         tempb = aovertimepay[j];
         aovertimepay[j] = aovertimepay[j+1];
         aovertimepay[j+1] = tempb; 
         
         tempc = ataxamount[j];
         ataxamount[j] = ataxamount[j+1];
         ataxamount[j+1] = tempc; sorted = 0;
          
              }//if
         }//for
 }//while
}//sort


.
.
.
.
.

 employee[i]->sortdata(alastname,aovertimepay, ataxamount,anetpay, n);
Last edited on
Any thoughts guys?
Topic archived. No new replies allowed.