Program crashing

I am writing a payroll program that sorts netpays- it compiles but crashes immediately. Do you guys have any thoughts as to why this is happening??

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
221
222
223
224
225
226
227
228
229
230
231
232
233
#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[6], *report;
    char alastname[14],lastname[14], ams, apaystatus;
    int aemployeeid,n, i=0;
  
    double ahoursworked, ayearlysalary, ahourlyrate, minnp, maxnp, ataxamount,  anetpay, aovertimepay, taxamount[100000],  netpay[100000], overtimepay[100000];

    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,overtimepay,taxamount,netpay);
       
      
           minnp = employee[i]->minnet(minnp, i);
           maxnp = employee[i]->maxnet(maxnp, i);
           employee[i]->printdata();
           i++; 
     }
      

          report->printminmax(minnp, maxnp);
     
          fin.close(); 
          system ("pause");
} 
Without more information I would guess it has something to do with the large arrays you are allocating on the stack (line 198). Or it could be all of the uninitialized variables floating around (e.g. lines 126/128). I'm surprised that actually compiles. There are also missing returns, missing headers, etc.

My compiler emits a number of warnings when compiling this code. Are you not seeing any warnings with your compiler?

Have you considered writing unit tests for your classes? http://legalizeadulthood.wordpress.com/2009/07/04/c-unit-tests-with-boost-test-part-1/

What line is the program crashing at? That will point you to the problem.
I edited the arrays in line 198 to be 1000 each. I am using Dev C++ and do not have any warnings. Thus its hard to see where the problem is. It does print the header so I am guessing the problem is within the while loop. Where am I missing returns?

What compiler are you using?
I am using GCC.

What is the value of n at line 128 supposed to be?

1
2
3
4
5
6
7
8
9
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; 


I'm sure we covered all this over in http://www.cplusplus.com/forum/beginner/36791/#msg199308

Keep this bit: payroll report; but move it to the first line of your main function. You will note that you also try to create a pointer named report. This gives you two objects with the same name, one shadowing the other. That's not good. Frankly, there's no reason for you to be using pointers here, especially since you don't really understand them. Get it working, and then play with pointers.

You've still got a path through the salaried::findgrosspay function that will never hit the return. Your return grosspay; is inside the code executed only

if (hoursworked > 0)

so if that condition is not true, you will not reach the return statement.

Dump Dev-C++, it's just bad.


At this point in the code:

report->printheaders();

I'm not convinced that report is pointing at anything; you create it on line 194 and then never point it at anything. Under my MinGW build, your huge stack arrays still trash the stack, but Linux is happy to run the whole thing all the way through without segfaulting.
Last edited on
@OP: GCC is a compiler, Dev-C++ is an IDE. You can't make much of a comparison between the two especially since I'm pretty sure that Dev-C++ supports GCC, I know that wxDev-C++ does. I think PanGalactic was being a bit literal here or he answered out of reflex but was still correct because of how you worded your question.

You may not see the same warnings because Dev-C++ is out of date with the excepted standard this is why Moschops is saying to drop it. Even wxDev-C++ has some bugs making it for the most part unsuitable for Windows XP SP3 and above. I have moved over to Code::Blocks and despite some minor annoyances have found it an exceptable alternative.

http://www.codeblocks.org/downloads

I tell people to avoid Visual Studio because it holds your hand through some stuff that is important to know, mostly linking to 3rd party libs. But if you want to go that route go here:

http://www.microsoft.com/visualstudio/en-us/products/2010-editions/express
Thanks for the help. I changed the beginning of the main block to

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main() {
   payroll employee[6];
    payroll report;
    payroll settingthevariables;
    payroll findgrosspay;
    payroll findtaxamount ;          
    payroll findnetpay ;          
    payroll sortdata;    
    payroll minnet;          
    payroll maxnet; 
    char alastname[14],lastname[14], ams, apaystatus;
    int aemployeeid,n, i=0;
    double ahoursworked, ayearlysalary, ahourlyrate, minnp, maxnp, ataxamount,  anetpay, 
    aovertimepay, taxamount[100],  netpay[100], overtimepay[100];
    report.printheaders();


and the while loop to
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
  while (fin>>alastname>>aemployeeid>>ams>>apaystatus>>ahoursworked>>ahourlyrate>>ayearlysalary) {
           if (apaystatus == 's') {
           
              payroll salaried();
              employee[i].settingthevariables(alastname, aemployeeid, ams, apaystatus, ahoursworked, ahourlyrate, ayearlysalary, ataxamount,  anetpay, aovertimepay);
              employee[i].findgrosspay(); }
           if (apaystatus == 'h') {
              payroll 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,overtimepay,taxamount,netpay);
       
      
           minnp = employee[i].minnet(minnp, i);
           maxnp = employee[i].maxnet(maxnp, i);
           employee[i].printdata();
           i++;  
     }
      

          report.printminmax(minnp, maxnp);
     
          fin.close(); 
          system ("pause");


But it still crashes. I believe the error was in my sort function and I have amended it to

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


It now doesnt compile due to "invalid conversion from `char*' to `char'" But I dont know how to fix this swap. is there another way I could write this sort function?
Topic archived. No new replies allowed.