Sorting multiple columns

Can you guys take a look at the below and give me some guidance? I need to sort the netpays and recieve a " a function-definition is not allowed here before '{' token " error on the last line.

Thanks

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

int  readalldata( long int[ ], int[ ], float[ ], float[ ], const int ); 
void findovertimehours( int[ ], int[ ], int );
void findovertimepay( int[ ], float[ ], float[ ], int ); 
void findregularhours( int[ ], int[ ], int );
void findregularpay( int[ ], float[ ], float[ ], int );
void findgrosspay( float[ ], float[ ], float[ ], int );
void findtaxrate( float[ ], float[ ], float[ ], int );
void findtaxamount( float[ ], float[ ], float[ ], int );
void findnetpay( float[ ], float[ ], float[ ], int );
void printalldata( long int[ ], int[ ], float[ ], float[ ], float[ ], float[ ],float[ ], int );


     
main(){
const int MAXSIZE = 50;          
int n;
long int id[ MAXSIZE ];    
int hoursworked[ MAXSIZE ], overtimehours[ MAXSIZE ];
int regularhours[ MAXSIZE ], numbers[ MAXSIZE];
float hourlyrate[ MAXSIZE ], regularpay[ MAXSIZE ], ms[ MAXSIZE ]; 
float overtimepay[ MAXSIZE ], grosspay[ MAXSIZE ];
float taxrate[ MAXSIZE ],  taxamount[ MAXSIZE ],netpay[ MAXSIZE] ;

float sumofnetpay[ MAXSIZE ], avgofnetpay [ MAXSIZE ];
        
n = readalldata( id, hoursworked, hourlyrate, ms, MAXSIZE ); 
findovertimehours( hoursworked, overtimehours, n );
findovertimepay( overtimehours, hourlyrate, overtimepay, n );
findregularhours( hoursworked, regularhours, n  );
findregularpay( regularhours, regularpay, hourlyrate, n );
findgrosspay( regularpay, overtimepay, grosspay, n ); 
findtaxrate( grosspay, taxrate, ms, n );
findtaxamount( grosspay, taxamount, taxrate, n );
findnetpay( grosspay, netpay, taxamount, n );


printalldata( id, hoursworked, hourlyrate, overtimepay,grosspay, taxamount, netpay, n );
}   

int readalldata( long int id[ ], int hoursworked[ ], float hourlyrate[ ], float ms[ ], int n ){ifstream fin("payrolltest.in");
n=0;
while(fin>>id[n]>>hoursworked[n]>>hourlyrate[n]>>ms[n]) n++;
fin.close( );
return n;
}
void findovertimehours( int hoursworked[ ], int overtimehours[ ], int n ){
for( int i = 0 ; i < n ; i++){
if( hoursworked[i] > 40 ) overtimehours[i] = hoursworked[i] - 40;
else overtimehours[i] = 0;
}
}
void findovertimepay( int overtimehours[ ], float hourlyrate[ ],float overtimepay[ ], int n ){
for( int i = 0 ; i < n ; i++){
overtimepay[i] = overtimehours[i] * hourlyrate[i] * 1.5;
}
}
void findregularhours( int hoursworked[ ], int regularhours[ ], int n ){
for( int i = 0 ; i < n ; i++){
if( hoursworked[i] > 40 ) regularhours[i] = 40;
else regularhours[i] = hoursworked[i];
}
}
void findregularpay( int regularhours[ ], float regularpay[ ],float hourlyrate[ ], int n ){
for( int i = 0 ; i < n ; i++ ){
regularpay[i] = regularhours[i] * hourlyrate[i];
}
}
void findgrosspay( float regularpay[ ], float overtimepay[ ], float grosspay[ ],int n ){
for( int i = 0 ; i < n ; i++){
grosspay[i] = regularpay[i] + overtimepay[i];   
}
}
     
void findtaxrate( float grosspay[ ], float taxrate[ ], float ms[ ], int n ){
for( int i = 0 ; i < n ; i++){
if( grosspay[i] > 1000 ) taxrate[i] = 0.3;
else if(grosspay[i] > 800 ) taxrate[i] = 0.2;
else if(grosspay[i] > 500 ) taxrate[i] = 0.1;
else taxrate[i] = 0;
if ((ms[i]>0)) taxrate[i]+.05;
else if((ms[i]>2)) taxrate[i]-.05;
else taxrate[i] = taxrate[i];

}
}
     
void findtaxamount( float grosspay[ ], float taxamount[ ],float taxrate[ ], int n ){
for( int i = 0 ; i < n ; i++){
taxamount[i] = grosspay[i] * taxrate[i];
}
}
    
void findnetpay( float grosspay[ ], float netpay[ ], float taxamount[ ], int n){
for( int i = 0 ; i < n ; i++){
netpay[i] = grosspay[i] - taxamount[i];
}
}
   
 int sort( long int id[],  int hoursworked[], 
         float hourlyrate[], float overtimehours[], float overtimepay[], float regularpay[],
         float grosspay[], float taxrate[], float netpay[], int n){
    int i,j;    
    for(int i=0; i<n-1; i++){
        for(j=n-1; j>i; j--){
            if(netpay[j]<netpay[j-1]){
               float hold = ( id[j],  hoursworked[j], 
                             hourlyrate[j], overtimehours[j], overtimepay[j], regularpay[j],
                             grosspay[j], taxrate[j], netpay[j]);
              
               ( id[j],  hoursworked[j], 
               hourlyrate[j], overtimehours[j], overtimepay[j], regularpay[j],
               grosspay[j], taxrate[j], netpay[j]) = ( id[j-1], hoursworked[j-1], 
               hourlyrate[j-1], overtimehours[j-1], overtimepay[j-1], regularpay[j-1], grosspay[j-1], taxrate[j-1], netpay[j-1]);
               ( id[j-1],  hoursworked[j-1], 
                hourlyrate[j-1], overtimehours[j-1], overtimepay[j-1], regularpay[j-1],
                grosspay[j-1], taxrate[j-1], netpay[j-1]) = hold;
          }//
       }//
    }//


      cout<<setw(8)<<"ID"<<setw(6)<<"STAT"<<setw(4)<<"HW"<<setw(5)<<"HR"<<setw(4)<<"OTH"<<setw(6)<<"OTP"<<setw(8)<<"REGP"<<setw(8)<<"grosspay"<<setw(6)<<"TAX"<<
      setw(8)<<"NET"<<endl;
      
      cout<<setw(8)<<left<<"====="<<setw(6)<<"===="<<setw(4)<<"==="<<
      setw(5)<<"==="<<setw(4)<<"==="<<setw(6)<<"==="<<setw(8)<<"====="<<setw(8)<<"====="<<setw(6)<<"===="
      <<setw(3)<<"====="<<endl;
      
      
      for(i=0;i<n;i++){
       cout<<setw(8)<<id[i]<<setw(6)<<setw(4)<<hoursworked[i]
         <<setw(5)<<hourlyrate[i]<<setw(4)<<overtimehours[i]<<setw(6)<<overtimepay[i]<<setw(8)<<regularpay[i]
         <<setw(8)<<grosspay[i]<<setw(6)<<setw(1)<<"$"<<netpay[i]
         <<endl<<endl;    
       }


void printalldata(long int id[], int hoursworked[], 
                  float hourlyrate[], float overtimepay[], 
                  float grosspay[], float taxamount[], float netpay[], int n){}





system ("pause");}
No comments, strange indentation in your code, you're trying to pass entire arrays instead of using pointers, you're trying to statically set the distance between your colums so if one changes they ALL have to then be changed, you're declaring variables in the MIDDLE of functions... I'm glad you used the code brackets but you need to do some serious work on your formatting my friend.

The Error you're getting is probably because of the {} at the end of line 145 making that *gag*system(...) call an orphan.
Last edited on
ok, I have amended my work with structures, but now it doesnt sort. Can you take a look at my sort code?

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
 #include<iostream>
#include<fstream>  
#include <iomanip>  	// file input output stream
	using namespace std; 
	struct person{
           char ms;
	      long int id;
	      int    hoursworked;
	      int    overtimehours;
	      int    regularhours;
	      float hourlyrate;
	      float regularpay;
	      float overtimepay;
	      float grosspay;
	      float taxrate;
	      float taxamount;
          float netpay;             };    // structure declaration
int  readalldata( person[ ], const int );           // function prototypes
	void findovertimehours( person[ ], int ); 
	void findovertimepay( person[ ], int ); 
	void findregularhours( person[ ], int );
	void findregularpay( person[ ], int );
	void findgrosspay( person[ ], int );
	void findtaxrate( person[ ], int );
	void findtaxamount( person[ ], int );
	void findnetpay( person[ ], int );
	void printalldata( person[ ], int );
	
	int sort(person[ ], int);
	
	
	       int main( ){
      const int MAXSIZE = 100;   	// for maximum of 100 employees
      int n;             //declaration of variables
      person employee[ MAXSIZE ];        
      n = readalldata( employee, MAXSIZE ); //functions calls
	      findovertimehours( employee, n );
	      findovertimepay( employee, n );
	      findregularhours( employee, n  );
      findregularpay( employee, n );
      findgrosspay( employee, n );
	      findtaxrate( employee, n );    
      findtaxamount( employee, n );
      findnetpay( employee, n );
      printalldata( employee, n );     
      sort(employee,n);   
	      return 0; 
}//MAIN
// function definitions
 int readalldata( person emp[ ], int n ){
     ifstream fin( "employee9.in" );
      n = 0;
     while( fin >> emp[ n ].id >> emp[ n ].hoursworked >> emp[ n ].hourlyrate ){   
          n++;   }//WHILE
     fin.close( );
      return n;
 }//READALLDATA
 void findovertimehours( person emp[ ], int n ){
     for( int i = 0 ; i < n ; i++){
           if( emp[ i ].hoursworked > 40 )
                emp[ i ].overtimehours = emp[ i ].hoursworked - 40;
          else
               emp[ i ].overtimehours = 0;
     }//FOR
 }//FINDOVERTIMEHOURS
 void findovertimepay( person emp[ ], int n ){
      for( int i = 0 ; i < n ; i++){
          emp[ i ].overtimepay = emp[ i ].overtimehours * emp[ i ].hourlyrate * 1.5;
      }//FOR
 }//FINDOVERTIMEPAY
 void findregularhours( person emp[ ], int n ){
     for( int i = 0 ; i < n ; i++){
           if( emp[ i ].hoursworked > 40 ) emp[ i ].regularhours = 40;
         else   emp[ i ].regularhours = emp[ i ].hoursworked;
     }//FOR
}//FINDREGULARHOURS
 void findregularpay( person emp[ ], int n ){
      for( int i = 0 ; i < n ; i++ ){
          emp[ i ].regularpay = emp[ i ].regularhours * emp[ i ].hourlyrate;
     }//FOR
}//FINDREGULARPAY
 void findgrosspay( person emp[ ], int n ){
     for( int i = 0 ; i < n ; i++){
          emp[ i ].grosspay = emp[ i ].regularpay + emp[ i ].overtimepay;   
      }//FOR
}//FINDGROSSPAY
 void findtaxrate( person emp[ ], int n ){
    for( int i = 0 ; i < n ; i++){
          if( emp[ i ].grosspay > 1000.00 ) emp[ i ].taxrate = 0.30; 
          else if( emp[ i ].grosspay >800.00 ) emp[ i ].taxrate = 0.20;
          else if( emp[ i ].grosspay > 500.00 ) emp[ i ].taxrate = 0.10;
          else emp[ i ].taxrate = 0.0;
          
             if (emp[ i ].ms=='S') emp[ i ].taxrate=(emp[ i ].taxrate+.05);
                            else if (emp[ i ].ms=='s') emp[ i ].taxrate=(emp[ i ].taxrate+.05);
                            else if (emp[ i ].ms=='H' && emp[ i ].grosspay > 500.00 ) emp[ i ].taxrate=(emp[ i ].taxrate-.05);
                            else if (emp[ i ].ms=='h' && emp[ i ].grosspay > 500.00 ) emp[ i ].taxrate=(emp[ i ].taxrate-.05);
                            else emp[ i ].taxrate=(emp[ i ].taxrate*1);
                          
                            
      }//FOR
}//FINDTEXRATE
 void findtaxamount( person emp[ ], int n ){
     for( int i = 0 ; i < n ; i++){
           emp[ i ].taxamount = emp[ i ].grosspay * emp[ i ].taxrate;
      }//FOR
 }//FINDTAXAMOUNT
void findnetpay( person emp[ ], int n ){
      for( int i = 0 ; i < n ; i++){
          emp[ i ].netpay = emp[ i ].grosspay - emp[ i ].taxamount;
      }//FOR
 }//FINDNETPAY
 
 
 
int sort( person emp[ ], int n ){
    int i,j;    
    for(int i=0; i<n-1; i++){
        for(j=n-1; j>i; j--){
            if(emp[j].netpay <emp[j-1].netpay ){
               person hold =  emp[j];
              
               emp[j] = emp[j-1];
                emp[j-1] = hold;
          }//
       }//
    }//
}

void printalldata( person emp[ ], int n ){
     cout << "EMP ID" << "\t" << "HOURS" << "\t" << "RATE " << "\t " 
            << "OVERPAY " << "\t" << "GROSSPAY" << "\t" << "TAX  " << "\t" 
           << "  NETPAY  " << endl;
     for( int i = 0 ; i < n; i++){
          cout << " " << emp[ i ].id << "\t " << emp[ i ].hoursworked 
                  << "\t" << emp[ i ].hourlyrate << "\t " << emp[ i ].overtimepay 
                  << "\t\t"  << emp[ i ].grosspay << "\t\t" << emp[ i ].taxamount 
                 << "\t " << emp[ i ].netpay << endl;
    }//FOR
  system ("pause");}//PRINTALLDATA
 
 // end source code
It looks ok. Could we see the input file?
Dude break your code into little chunks; this way is digging your grave.


My input is

1234 43 100.00
2345 40 120.75
3456 35 110.50

I also added ".netpay" to emp[j] and emp[j-1], but still doesnt sort
Well, try printing the data again, after you sort it ;)

EDIT:

cplusplusrookie wrote:
I also added ".netpay" to emp[j] and emp[j-1], but still doesnt sort

Don't. Your sort function is fine as it is here. ".netpay" should only appear in the control part of the if statement.
Last edited on
Can you clarify? I believe my printalldata block is after my sort
Indeed, the definition of your print function is after the one of your sort function.
But take a look at your main, where you call them:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    const int MAXSIZE = 100; // for maximum of 100 employees
    int n; //declaration of variables
    person employee[ MAXSIZE ];

    n = readalldata( employee, MAXSIZE ); //functions calls

    findovertimehours( employee, n );
    findovertimepay( employee, n );
    findregularhours( employee, n  );
    findregularpay( employee, n );
    findgrosspay( employee, n );
    findtaxrate( employee, n );
    findtaxamount( employee, n );
    findnetpay( employee, n );

    printalldata( employee, n ); //<- here, you print the data
    sort(employee,n); //<- here, you sort the data
    return 0; //<- and here, you quit
}

Last edited on
omg youre right. I amended it and now it works. THANK YOU. I am off to sort by pointers now
Topic archived. No new replies allowed.