Sorting issue? Completed

Aloha, I am using dev c++ and having a slight problem with a sort to a ever building payroll program. The unsorted data come out fine. The sorted data is where I have the issue. I am trying to sort by netpay(np). The sorted data output is there. The np is sorted but the rest of the data doesn't sort with it. Any help in the right direction to solve is apprecited.
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
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>
using namespace std;

 //declaration of variables
 int id[100], hw[100], oth[100], indexOfMin;
 float hr[100], otp[100], rgp[100], gp[100], tax[100],
       np[100], TAXR[100];
 float sumnp=0;
 float avgnp;
 char fname[100][15], lname[100][15], ms[100];
 int i, n=0, j; 
//main function
int main(){
//declaring functions
  int findovt();
  int findgp();
  int findtaxr();
  int findtax();
  int findnp();
  int outputall();
  int selectionSort();
  int sortoutput();
  
  ifstream fin("payrollfl.txt");
  cout<<setiosflags(ios::left)<<"                                Digdug's National Bank"<<endl<<endl;
  cout<<setiosflags(ios::left)<<setw(15)<<"FIRST NAME"<<setw(15)<<"LAST NAME"<<setw(7)<<"EMP ID"<<setw(4)<<"HW"<<setw(6)<<"HR"<<setw(5)<<"STAT"<<setw(5)<<"OTH"<<setw(7)<<"OTP"<<setw(7)<<"REGP"<<setw(9)<<"GROSS"<<setw(7)<<"TAX"<<setw(7)<<"NET"<<endl<<endl;
  cout<<setiosflags(ios::left)<<setw(15)<<"=============="<<setw(15)<<"=============="<<setw(7)<<"======"<<setw(4)<<"==="<<setw(6)<<"====="<<setw(5)<<"===="<<setw(5)<<"==="<<setw(7)<<"======"<<setw(7)<<"======"<<setw(9)<<"========"<<setw(7)<<"======"<<setw(7)<<"======"<<endl;
  while(fin>>fname[n]>>lname[n]>>id[n]>>hw[n]>>hr[n]>>ms[n]) n++;{
 //functions calls
 findovt();
 findgp();
 findtaxr();
 findtax();
 findnp();
 outputall();
 selectionSort();
 sortoutput();
}//WHILE
 system("pause");
 return 0;
}//MAIN 
// function definitions


void findovt(void){
  for(int i=0;i<n;i++){
      if(hw[i]>40){
      oth[i]=hw[i]-40;
      otp[i]=oth[i]*hr[i]*1.5;
      rgp[i]=hw[i]*hr[i];
    }//IF
    else{
     oth[i]=0;
     otp[i]=0;
     rgp[i]=hw[i]*hr[i];
    }//ELSE
  }//FOR
}//FINDOVT


void findgp(void){
  for(int i=0;i<n;i++){
    gp[i]=rgp[i]+otp[i];
  }//FOR
}//FINDGP

void findtaxr(void){
  for(int i=0;i<n;i++){
   if ((gp[i]>1000) && ms[i]=='S'||ms[i]=='s') TAXR[i]=0.35;
   else if ((gp[i]>1000) && ms[i]=='M'||ms[i]=='m') TAXR[i]=0.30;
   else if ((gp[i]>1000) && ms[i]=='H'||ms[i]=='h') TAXR[i]=0.25;
   else if ((gp[i]>800) && ms[i]=='S'||ms[i]=='s') TAXR[i]=0.25;
   else if ((gp[i]>800) && ms[i]=='M'||ms[i]=='m') TAXR[i]=0.20;
   else if ((gp[i]>800) && ms[i]=='H'||ms[i]=='H') TAXR[i]=0.15;
   else if ((gp[i]>500) && ms[i]=='S'||ms[i]=='s') TAXR[i]=0.15;
   else if ((gp[i]>500) && ms[i]=='M'||ms[i]=='m') TAXR[i]=0.10;
   else if ((gp[i]>500) && ms[i]=='H'||ms[i]=='h') TAXR[i]=0.05;
   else TAXR[i]=0.0;
  }//FOR
}//FINDTAXR

void findtax(void){
  for(int i=0;i<n;i++){
     tax[i]=gp[i]*TAXR[i];
  }//FOR
}//FINDTAX

void findnp(void){
  for(int i=0;i<n;i++){
     np[i]=gp[i]-tax[i];
  }//FOR
}//FINDNP

void outputall(void){
   for(int i=0;i<n;i++){
  cout<<fixed;
   cout<<setiosflags(ios::showpoint|ios::fixed|ios::left)<<setw(15)<<fname[i]
       <<setw(15)<<lname[i]<<setw(7)<<id[i]<<setw(4)<<setprecision(1)<<hw[i]
       <<setw(6)<<setprecision(2)<<hr[i]<<setw(5)<<ms[i]<<setw(5)
       <<setprecision(1)<<oth[i]<<setw(7)<<setprecision(2)<<otp[i]<<setw(9)
       <<rgp[i]<<setw(9)<<gp[i]<<setw(7)<<tax[i]<<setw(7)<<np[i]<<endl;
   sumnp=sumnp+np[i];
  }//FOR
   avgnp=sumnp/n;       
   cout<<"THE AVERAGE NETPAY IS: "<<avgnp<<endl;
}//OUTPUTALL

void selectionSort(void){ 
     int i, pass, j; 

     for (pass = 0; pass < i-1; pass++){ 
           indexOfMin=pass; 
           for (j = pass+1; j<i; j++) 
                 if (np[j]<np[indexOfMin]) 
                       indexOfMin=j; 
           swap(np[pass], np[indexOfMin]); 
     } 
}// end selectionSort() 


void sortoutput(void){
   for(int i=0;i<n;i++){
   cout<<setiosflags(ios::showpoint|ios::fixed|ios::left)<<setw(15)<<fname[i]
       <<setw(15)<<lname[i]<<setw(7)<<id[i]<<setw(4)<<setprecision(1)<<hw[i]
       <<setw(6)<<setprecision(2)<<hr[i]<<setw(5)<<ms[i]<<setw(5)
       <<setprecision(1)<<oth[i]<<setw(7)<<setprecision(2)<<otp[i]<<setw(9)
       <<rgp[i]<<setw(9)<<gp[i]<<setw(7)<<tax[i]<<setw(7)<<np[i]<<endl;   
}
}
//end 
Last edited on
Hi, The reason only net pay is sorted is that you are only changing the order of the net pay data in your sort routine

1
2
3
4
5
6
7
8
9
10
11
void selectionSort(void){ 
     int i, pass, j; 

     for (pass = 0; pass < i-1; pass++){ 
           indexOfMin=pass; 
           for (j = pass+1; j<i; j++) 
                 if (np[j]<np[indexOfMin]) 
                       indexOfMin=j; 
           swap(np[pass], np[indexOfMin]);   //Swaps Net pay Only
     } 
}// end selectionSort()  


You need to order all the data in the sort.
You can do this a couple of ways - either add a whole bunch more swaps
- swap(fname[pass],fname[indexOfMin]) etc or change the type declaration to create a struct with all the data in and then have an array of the struct - you would then just swap struct's in the sort.
See http://www.cplusplus.com/doc/tutorial/structures.html for info on Structures if you need it.
Last edited on
Thanks Faldrax, I think you got me heading in the right direction. I put it in struct. first try with out a sort works fine. I update my sort for the struct but having no joy. It times out and shuts down. when it gets to the sorted part. Can I get another poke in the right direction please. Here is my 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
144
145
146
147
148
149
150
151
152
153
154
155
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>
using namespace std;
 //declaration of variables
 struct person{
  int id;
  int hw;
  int oth;
  float hr;
  float otp;
  float rgp;
  float gp;
  float tax;
  float np;
  float TAXR;
  char fname[15];
  char lname[15];
  char ms;  };//STRUCT
 
  int readdata(person[], int);
  void findovt(person[], int);
  void findgp(person[], int);
  void findtaxr(person[], int);
  void findtax(person[], int);
  void findnp(person[], int);
  void outputall(person[], int);
  void selectionSort(person[], int);
  void sortoutput(person[], int);
//main function
int main(void){
  const int MAXSIZE=100;
  int n;
  person employee[MAXSIZE];
//declaring functions

  cout<<setiosflags(ios::left)<<"                                Digdug's National Bank"<<endl<<endl;
  cout<<setiosflags(ios::left)<<setw(15)<<"FIRST NAME"<<setw(15)
<<"LAST NAME"<<setw(7)<<"EMP ID"<<setw(4)<<"HW"
<<setw(6)<<"HR"<<setw(5)<<"STAT"<<setw(5)<<"OTH"<<setw(7)
<<"OTP"<<setw(7)<<"REGP"<<setw(9)<<"GROSS"<<setw(7)
<<"TAX"<<setw(7)<<"NET"<<endl<<endl;
  cout<<setiosflags(ios::left)<<setw(15)<<"=============="<<setw(15)
<<"=============="<<setw(7)<<"======"<<setw(4)<<"==="<<setw(6)
<<"====="<<setw(5)<<"===="<<setw(5)<<"==="<<setw(7)
<<"======"<<setw(7)<<"======"<<setw(9)<<"========"<<setw(7)
<<"======"<<setw(7)<<"======"<<endl;
 //functions calls
 n=readdata(employee, MAXSIZE);
 findovt(employee, n);
 findgp(employee, n);
 findtaxr(employee, n);
 findtax(employee, n);
 findnp(employee, n);
 outputall(employee, n);
 cout<<setiosflags(ios::left)<<"                                SORTED DATA"<<endl<<endl; 
 selectionSort(employee, n);
 sortoutput(employee, n);

 system("pause");
}//MAIN 
// function definitions

int readdata(person emp[], int n){
  ifstream fin("payrollfl.txt");
  n=0;
  while(fin>>emp[n].fname>>emp[n].lname>>emp[n].id>>emp[n].hw>>emp[n].hr>>emp[n].ms){ n++;}
  fin.close();
  return n;
}
  
void findovt(person emp[], int n){
  for(int i=0;i<n;i++){
      if(emp[i].hw>40){
      emp[i].oth=emp[i].hw-40;
      emp[i].otp=emp[i].oth*emp[i].hr*1.5;
      emp[i].rgp=emp[i].hw*emp[i].hr;
    }//IF
    else{
     emp[i].oth=0;
     emp[i].otp=0;
     emp[i].rgp=emp[i].hw*emp[i].hr;
    }//ELSE
  }//FOR
}//FINDOVT


void findgp(person emp[], int n){
  for(int i=0;i<n;i++){
    emp[i].gp=emp[i].rgp+emp[i].otp;
  }//FOR
}//FINDGP

void findtaxr(person emp[], int n){
  for(int i=0;i<n;i++){
   if ((emp[i].gp>1000) && emp[i].ms=='S'||emp[i].ms=='s') emp[i].TAXR=0.35;
   else if ((emp[i].gp>1000) && emp[i].ms=='M'||emp[i].ms=='m') emp[i].TAXR=0.30;
   else if ((emp[i].gp>1000) && emp[i].ms=='H'||emp[i].ms=='h') emp[i].TAXR=0.25;
   else if ((emp[i].gp>800) && emp[i].ms=='S'||emp[i].ms=='s') emp[i].TAXR=0.25;
   else if ((emp[i].gp>800) && emp[i].ms=='M'||emp[i].ms=='m') emp[i].TAXR=0.20;
   else if ((emp[i].gp>800) && emp[i].ms=='H'||emp[i].ms=='H') emp[i].TAXR=0.15;
   else if ((emp[i].gp>500) && emp[i].ms=='S'||emp[i].ms=='s') emp[i].TAXR=0.15;
   else if ((emp[i].gp>500) && emp[i].ms=='M'||emp[i].ms=='m') emp[i].TAXR=0.10;
   else if ((emp[i].gp>500) && emp[i].ms=='H'||emp[i].ms=='h') emp[i].TAXR=0.05;
   else emp[i].TAXR=0.0;
  }//FOR
}//FINDTAXR

void findtax(person emp[], int n){
  for(int i=0;i<n;i++){
     emp[i].tax=emp[i].gp*emp[i].TAXR;
  }//FOR
}//FINDTAX

void findnp(person emp[], int n){
  for(int i=0;i<n;i++){
     emp[i].np=emp[i].gp-emp[i].tax;
  }//FOR
}//FINDNP

void outputall(person emp[], int n){
   for(int i=0;i<n;i++){
  cout<<fixed;
   cout<<setiosflags(ios::showpoint|ios::fixed|ios::left)<<setw(15)<<emp[i].fname
       <<setw(15)<<emp[i].lname<<setw(7)<<emp[i].id<<setw(4)<<setprecision(1)<<emp[i].hw
       <<setw(6)<<setprecision(2)<<emp[i].hr<<setw(5)<<emp[i].ms<<setw(5)
       <<setprecision(1)<<emp[i].oth<<setw(7)<<setprecision(2)<<emp[i].otp<<setw(9)
       <<emp[i].rgp<<setw(9)<<emp[i].gp<<setw(7)<<emp[i].tax<<setw(7)<<emp[i].np<<endl;
  }//FOR
}//OUTPUTALL

void selectionSort(person emp[], int n){ 
     int i, pass, j, indexOfMin; 

     for (pass = 0; pass < i-1; pass++){ 
           indexOfMin=pass; 
           for (j = pass+1; j<i; j++) 
                 if (emp[j].np<emp[indexOfMin].np) 
                       indexOfMin=j; 
           swap(emp[pass].np, emp[indexOfMin].np); 
     } 
}// end selectionSort() 


void sortoutput(person emp[], int n){
   for(int i=0;i<n;i++){
   cout<<setiosflags(ios::showpoint|ios::fixed|ios::left)<<setw(15)<<emp[i].fname
       <<setw(15)<<emp[i].lname<<setw(7)<<emp[i].id<<setw(4)<<setprecision(1)<<emp[i].hw
       <<setw(6)<<setprecision(2)<<emp[i].hr<<setw(5)<<emp[i].ms<<setw(5)
       <<setprecision(1)<<emp[i].oth<<setw(7)<<setprecision(2)<<emp[i].otp<<setw(9)
       <<emp[i].rgp<<setw(9)<<emp[i].gp<<setw(7)<<emp[i].tax<<setw(7)<<emp[i].np<<endl;   
}
}
//end  
Hi, You need to swap the employee structure, not just the net pay, chage line 141 to
swap(emp[pass], emp[indexOfMin]);
Thanks again Faldrax. I knew it to something like that. I was tried a couple of varients but didnt tried that one. Its closer to being done. I made the change it works some what. In the sorted data the first 2 struct are blank then I get 3 out of 5 sorted. Not sure what the prob is I will try to fig out. Ofcourse any nudge in the right direction would be nice.
Hi, looking at line 136, the outer loop for the sort, you are checking that pass < i-1, but i is never initialised in that function!
Since emp[] has n elements, should the check be pass < n-1 ?
Thanks Faldrax. I will keep that in mind for next time. I switched my sort to a bubble sort and it works great now. Just want to say thanks again for the nudges.
Topic archived. No new replies allowed.