Trouble with sorting

i am having trouble sorting the netpays of a payroll program. The program will not compile and I believe the problem is within the function def./called.
Can you take a look and give me some hints? I am lost as to how to fix this. Thanks!


 
void sortdata(char[],double[],double[],double[]);


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



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
int main() {
    payroll *employee[6], *report;
    char alastname[14],lastname[14], ams, apaystatus;
    int aemployeeid,n, i=0;

    double ahoursworked, ayearlysalary, ahourlyrate, minnp, maxnp, ataxamount,  anetpay, aovertimepay, taxamount[10000000],  netpay[100000000], overtimepay[100000000];

    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, ataxamount,  anetpay, aovertimepay);
              employee[i]->findgrosspay(); }
           if (apaystatus == 'h') {
              employee[i] = new hourly();
              employee[i]->settingthevariables(alastname, aemployeeid, ams, apaystatus, ahoursworked, ahourlyrate, ayearlysalary, ataxamount,  anetpay, aovertimepay);
              employee[i]->findgrosspay();}
           employee[i]->findtaxamount();
           employee[i]->findnetpay();
           employee[i]->sortdata(lastname[i],overtimepay[i],taxamount[i],netpay[i]);
What are your compile errors? I see nothing wrong myself. Yet.
My compile error is :

 
no matching function for call to `payroll::sortdata(char&, double&, double&, double&)'  

.
.
.
.
.
 
 candidates are: void payroll::sortdata(char*, double*, double*, double*) 


So I believe it is the way I called my sort function. But I have tried what seems to be every other possibility and still receive the same error. Is there another way I can define the sort function/ variables in the sort function?
I thought I'd just pop down here to answer this question quickly.

You don't need the [i]s in line 25 when you're calling your function, in fact they're expected not to be there. What you're doing for each argument is passing one element from an array when a whole array is expected. Get rid of them and you'll fix that error. :)

Also, instead of using char[]s for your alastname and lastname variables... might I suggest a string? And instead of massive arrays, might I suggest std::vectors or std::deques?

-Albatross
Last edited on
Albatross- thanks. I got rid of the [i]'s and the program compiles now, but I get a brief black screen before it crashes. I used char and double []'s in my variables bc its the only way I could get the sort function to work.

What is a std::deques?
I don't know why your program is crashing, although if I had to guess, it would be because of one of two things (maybe both, but most likely the second):

● An out-of-bounds error, where your trying to read/write memory outside of your array, possibly giving you the good old segmentation fault.
● Your system is unequipped to handle 3 10 million-element arrays of 64-bit floating point integers. Remember: the block of memory has to be contiguous in an array.

Using an std::vector, you could mostly avoid the second problem, and make the first less somewhat likely to happen.

An std::deque is a... generalized version of a vector. I'd suggest using a vector, unless you need really efficient access to both the first and last element of a deque, or if you're sure you're going to be storing a very large amounts of data.

-Albatross
Last edited on
Thanks. I have changed the variable spaces- now the headers are printed before it crashes. I am looking now to see if your first pt is causing my program to crash. Can you take a quick look at my code? I am just out of ideas.

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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#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, double, double, double);
void sortdata(char[],double[],double[],double[]);
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();  }

payroll* p_report;
payroll report;
payroll p_settingthevariables;
payroll p_findgrosspay;
payroll p_findtaxamount ;          
payroll p_findnetpay ;          
payroll p_sortdata;    
payroll p_minnet;          
payroll p_maxnet; 
       
  
           
           
           


void payroll::settingthevariables(char alastname[], int aemployeeid, char ams, 
char apaystatus, double ahoursworked, double ahourlyrate, double ayearlysalary, double ataxamount, double anetpay, double aovertimepay) {
     lastname = alastname;
     employeeid = aemployeeid;
     ms = ams;
     paystatus = apaystatus;
     hoursworked = ahoursworked;
     hourlyrate = ahourlyrate;
     yearlysalary=ayearlysalary;
     taxamount=ataxamount;
     netpay=anetpay;
     overtimepay= aovertimepay;
}
   
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::sortdata( char lastname[] ,double overtimepay[],double taxamount[],  double netpay[] ) {
    int i,j,n;

    char *alastname[n]; 
    double *aovertimepay[n]; 
    double *ataxamount[n];
    double *anetpay[n];
    double *temp, *tempb, *tempc; 
    char *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



void payroll::printheaders(){
     

    cout << "------------------------------------------------------------------------------" << endl;
    cout << "LAST NAME      OT PAY         TAX            NETPAY"   << endl;
    cout << "-------------------------------------------------------------------------------" << endl;}

void payroll::printdata()    {

     
 	    cout<<setw(15)<<left<<lastname;
        cout<<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint);
        cout<<setw(15)<<overtimepay<<setw(15)<<taxamount<<setw(15)<<netpay<<endl; 
             
}//printdata

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

int main() {
     p_report = &report;
    payroll employee, report;
    char alastname[14],lastname[14], ams, apaystatus;
    int aemployeeid,n, i=0;
  
    double ahoursworked, ayearlysalary, ahourlyrate, minnp, maxnp, ataxamount,  anetpay, aovertimepay, taxamount[n],  netpay[n], overtimepay[n];

    report.printheaders();
    
    ifstream fin;
    fin.open("2142011.in");

    while (fin>>alastname>>aemployeeid>>ams>>apaystatus>>ahoursworked>>ahourlyrate>>ayearlysalary) {
         
           employee.settingthevariables(alastname, aemployeeid, ams, apaystatus, ahoursworked, ahourlyrate, ayearlysalary, ataxamount,  anetpay, aovertimepay);
           employee.findgrosspay(); 
           employee.findtaxamount();
           employee.findnetpay();    
           employee.sortdata(lastname,overtimepay,taxamount,netpay);
           minnp = employee.minnet(minnp, i);
           maxnp = employee.maxnet(maxnp, i);
           employee.printdata();
           i++; 
          report.printminmax(minnp, maxnp);
     
          fin.close(); 
          system ("pause");}
} 
Topic archived. No new replies allowed.