No data output

So I am writing a payroll program and need to sort the netpay using pointers. I believe my sort statements are correct, but the data still do not render in the output file. Can you guys give me some pointers? the in put is

Alison Cameron 43 100.00
Peter Barcia 40 120.75

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


	struct person{
          char ms;
          char firstname;
          char lastname;
	      long int id;
	      int    hoursworked;
	      int    overtimehours;
	      int    regularhours;
	      float hourlyrate;
	      float regularpay;
	      float overtimepay;
	      float grosspay;
	      float taxrate;
	      float taxamount;
          float netpay;             };    
int  readalldata( person[ ], const int );           
	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 sortbypointers(person[ ], int);
	void printalldata( person[ ], int );
	
	       int main( ){
              
      const int MAXSIZE = 100;   	
      int n;             
      person employee[ MAXSIZE ];        
      n = readalldata( employee, MAXSIZE ); 
	      findovertimehours( employee, n );
	      findovertimepay( employee, n );
	      findregularhours( employee, n  );
      findregularpay( employee, n );
      findgrosspay( employee, n );
	      findtaxrate( employee, n );    
      findtaxamount( employee, n );
      findnetpay( employee, n );
      sortbypointers(employee,n);
    printalldata(employee,n);
         
	      return 0; 
}

 int readalldata( person emp[ ], int n ){
     ifstream fin( "payroll.in" );
      n = 0;
     while( fin >> emp[ n ].firstname >> emp[ n ].lastname >> emp[ n ].hoursworked >> emp[ n ].hourlyrate ){   
          n++;   }
     fin.close( );
      return n;
 }
 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;
     }
 }
 void findovertimepay( person emp[ ], int n ){
      for( int i = 0 ; i < n ; i++){
          emp[ i ].overtimepay = emp[ i ].overtimehours * emp[ i ].hourlyrate * 1.5;
      }
 }
 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;
     }
}
 void findregularpay( person emp[ ], int n ){
      for( int i = 0 ; i < n ; i++ ){
          emp[ i ].regularpay = emp[ i ].regularhours * emp[ i ].hourlyrate;
     }
}
 void findgrosspay( person emp[ ], int n ){
     for( int i = 0 ; i < n ; i++){
          emp[ i ].grosspay = emp[ i ].regularpay + emp[ i ].overtimepay;   
      }
}
 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);
                          
                            
      }
}
 void findtaxamount( person emp[ ], int n ){
     for( int i = 0 ; i < n ; i++){
           emp[ i ].taxamount = emp[ i ].grosspay * emp[ i ].taxrate;
      }
 }
void findnetpay( person emp[ ], int n ){
      for( int i = 0 ; i < n ; i++){
          emp[ i ].netpay = emp[ i ].grosspay - emp[ i ].taxamount;
      }
 }
 
 void sortbypointers(person emp[ ], int n){ 
  
     int i,j;
     float *p[n];
     char *q[n];
     char *k[n];
     double temp;
      char tempa;
       char tempb;
     int sortedflag=0;
 
for(i=0;i<n;i++){ 

  p[i]=&emp[ i ].netpay; 
  q[i]=&emp[ i ].firstname; 
  k[i]=&emp[ i ].lastname; 

while (!sortedflag){
   sortedflag=1;
   
   for(j=0;j<n-i;j++ ){
      if (*p[j]>*p[j+1]){ 
        temp=*p[j];
        *p[j]=*p[j+1];
          *p[j+1]=temp;
          
        tempa=*q[j];
        *q[j]=*q[j+1];
        *q[j+1]=tempa;
        
        tempb=*k[j];
        *k[j]=*k[j+1];
         *k[j+1]=tempb;
         
     sortedflag=0;    } // if


 
     } // for
     }  // while
   
}                   
}

void printalldata( person emp[ ], int n ){
     cout<<setw(16)<<"FIRST NAME"<<setw(18)<<"LAST NAME"<<setw(8)<<"NET"<<endl;
     
   
for( int i = 0 ; i < n; i++){
   cout<<setw(16)<<emp[ i ].firstname<<setw(18)<<emp[ i ].lastname<<setw(8)emp[ i ].netpay<<endl;
               
                  
                  }
  system ("pause");}

 
Last edited on
"output file"

What output file? I don''t see any output file being created or used.
I was referring to this as the output:
1
2
3
4
5
6
7
8
9
void printalldata( person emp[ ], int n ){
     cout<<setw(16)<<"FIRST NAME"<<setw(18)<<"LAST NAME"<<setw(8)<<"NET"<<endl;
     
   
for( int i = 0 ; i < n; i++){
   cout<<setw(16)<<emp[ i ].firstname<<setw(18)<<emp[ i ].lastname<<setw(8)emp[ i ].netpay<<endl;
               
                  
                  }
Any thoughts?
Does this lesson require you to implement your own sort algorithm?

What should the output look like?

What does it look like now?
Firstly that it didn't compile.

bad5.cpp:169: error: expected ‘;’ before ‘emp’
bad5.cpp:173: error: ‘system’ was not declared in this scope


Having fixed those, I then noticed that after line 41, n is still zero. I suggest you start debugging to see where it doesn't do what you expect it to.
Last edited on
Thanks.

I am supposed to sort by pointers in this assignment. The output should include first/last names and net pay, sorted by netpay. Right now only the headers (first/last names and netpay render).

i am just confused as to how to solve the n error. I thought I had closed the block where n was declared to be 0.
Can you guys look at this once more? I realized n was used in arrays and pointers and I made some changes, but the data still wont render

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


	struct person{
           char ms;
	      long int id;
	      char firstname;
          char lastname;
	      int  hoursworked;
	      int  overtimehours;
	      int  regularhours;
	      float hourlyrate;
	      float regularpay;
	      float overtimepay;
	      float grosspay;
	      float taxrate;
	      float taxamount;
          float netpay;             }; 
             
int  readalldata( person[ ], const int );           
	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 sortbypointers(person emp[ ], int);
	void printalldata( person[ ], int );
		
int main( ){
      const int MAXSIZE = 100;   	
      int n;             
      person employee[ MAXSIZE ];        
      n = readalldata( employee, MAXSIZE ); 
	      findovertimehours( employee, n );
	      findovertimepay( employee, n );
	      findregularhours( employee, n  );
          findregularpay( employee, n );
          findgrosspay( employee, n );
	      findtaxrate( employee, n );    
          findtaxamount( employee, n );
          findnetpay( employee, n );
          sortbypointers(employee,n);
          printalldata( employee, n );     
         
      return 0; 
}// main

int readalldata( person emp[ ], int n ){
    n = 0;
     ifstream fin( "payrolldavid.in" );
     
    while(  fin >>  emp[ n ].firstname >> emp[ n ].lastname >> emp[ n ].hoursworked >> emp[ n ].hourlyrate ) n++;   //while
     fin.close( );
      return n;
 }//read data
 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;
     }
 }
 void findovertimepay( person emp[ ], int n ){
      for( int i = 0 ; i < n ; i++){
          emp[ i ].overtimepay = emp[ i ].overtimehours * emp[ i ].hourlyrate * 1.5;
      }
 }
 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;
     }
}
 void findregularpay( person emp[ ], int n ){
      for( int i = 0 ; i < n ; i++ ){
          emp[ i ].regularpay = emp[ i ].regularhours * emp[ i ].hourlyrate;
     }
}
 void findgrosspay( person emp[ ], int n ){
     for( int i = 0 ; i < n ; i++){
          emp[ i ].grosspay = emp[ i ].regularpay + emp[ i ].overtimepay;   
      }
}
 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);
                          
                            
      }
}
 void findtaxamount( person emp[ ], int n ){
     for( int i = 0 ; i < n ; i++){
           emp[ i ].taxamount = emp[ i ].grosspay * emp[ i ].taxrate;
      }
 }
void findnetpay( person emp[ ], int n ){
      for( int i = 0 ; i < n ; i++){
          emp[ i ].netpay = emp[ i ].grosspay - emp[ i ].taxamount;
      }
 }
 
 
 
 void sortbypointers(person emp[ ], int e){ 

     int i,j;
     float *p[e];
     char *q[e];
     char *k[e];
     double temp;
      char tempa;
       char tempb;
     int sortedflag=0;
 
for(i=0;i<e;i++){ 

  p[i]=&emp[ i ].netpay; 
  q[i]=&emp[ i ].firstname; 
  k[i]=&emp[ i ].lastname; 

while (!sortedflag){
   sortedflag=1;
   
   for(j=0;j<e-i;j++ ){
                     
      if (*p[j]>*p[j+1]){ 
        temp=*p[j];
        *p[j]=*p[j+1];
        *p[j+1]=temp;
          
        tempa=*q[j];
        *q[j]=*q[j+1];
        *q[j+1]=tempa;
        
        tempb=*k[j];
        *k[j]=*k[j+1];
        *k[j+1]=tempb;
         
     sortedflag=0;    } // if


 
     } // for
     }  // while
   
}                   
}

void printalldata( person emp[ ], int n ){
     cout<<setw(16)<<"FIRST NAME"<<setw(18)<<"LAST NAME"<<setw(8)<<"NET"<<endl;
     for( int i = 0 ; i < n; i++){
     cout<<setw(16)<<emp[ i ].firstname<<setw(18)<<emp[ i ].lastname<<setw(8)<<emp[ i ].netpay<<endl;
               
                  }// for
  system ("pause");}// print

 
Any thoughts on the above?
use a swap function instead of the mess at 143-153

1
2
3
4
5
6
7
template <typename T>
void swap(T &a, T &b)
{
   T temp = a;
   a = b;
   b = temp;
}


then call them with
1
2
3
swap(*p[j], *p[j+1]);
swap(*q[j], *q[j+1]);
swap(*q[j], *q[j+1]);


Trust me. Professors and teachers rather see a function do this stuff. I remember classmates losing multiple points because they didn't use a swap function. Also it just looks nicer.
Thanks. Do you have any thoughts on changing the def. of firstname/lastname? I believe this is why my program wont render
Topic archived. No new replies allowed.