Program stopped working....

Hi! I am writing a payroll program using class, and also finding the average of the employees net pay. I have been working on this program for weeks, and when I compile, I keep getting a pop up from windows saying that the program has stopped working and will terminate :(. I am using Dec-C++ 4.9.9.2 Beta version. Here is my program:

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
#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
class payroll{
      ifstream fin;
      int n;
      char employeename[20];
      int hoursworked,overtime;
     double hourlyrate,overtimepay,regularpay,grosspay,taxrate,taxamount,netpay;
      double totalnetpay,avgnetpay;
      void calculategrosspay();
      void calculatetax();
      void calculatenetpay();
      void findavgnetpay();
      void printheadings();
      void printdata();
      public:payroll();
      ~payroll();
      void printreport(); };
      payroll::payroll(){
      fin.open("payrollclass.in"); }//CONSTRUCTOR
      payroll::~payroll(){
      fin.close(); }//DESTRUCTOR
  void payroll::calculategrosspay(){
       if(hoursworked > 40){
       overtime=hoursworked-40;
       regularpay=hoursworked*hourlyrate;
       overtimepay=overtime*(hourlyrate*1.5);
       grosspay=regularpay+overtimepay; }//IF
       else{ grosspay=hoursworked*hourlyrate; 
       regularpay=grosspay; 
       overtimepay=0;}//ELSE
        }//CALCULATEGROSSPAY
  void payroll::calculatetax(){
       taxrate=.30;
       taxamount=grosspay*taxrate; }//CALCULATETAX
  void payroll::calculatenetpay(){
       netpay=grosspay-taxamount;
       totalnetpay=totalnetpay+netpay;
         }//CALCULATENETPAY
 void payroll::findavgnetpay(){
   avgnetpay= totalnetpay/n;
}//FINDAVGNETPAY                                
  void payroll::printheadings(){
       cout<<setw(40)<<"-PAYROLL REPORT-"<<endl;
       cout<<"---------------------------------------------------------"<<endl;
       cout<<" NAME     HW     HR     OT-PAY  GROSS   TAX   NETPAY"<<endl;
       cout<<"---------------------------------------------------------"<<endl;
       }//PRINTHEADINGS
       void payroll::printdata(){
            cout<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint);
            cout<<setw(6)<<employeename<<setw(6)<<hoursworked<<setw(8)<<
            hourlyrate<<setw(8)<<overtimepay<<setw(8)<<grosspay<<
            setw(8)<<taxamount<<setw(8)<<netpay<<endl; }//PRINTDATA
     void payroll::printreport(){
     n=0; totalnetpay=0;
          printheadings();
          while(fin>>employeename>>hoursworked>>hourlyrate){
          calculategrosspay();
          calculatetax();
          calculatenetpay();
          printdata();
          n++; }//WHILE
          
          findavgnetpay();
  cout<<endl<<"The average net pay for "<<n<<" employees is "<<avgnetpay<<endl;
          
          }//PRINTREPORT
          
          
          int main(){
               payroll employee;
               employee.printreport();
               system ("pause");
               }//MAIN 

Can anyone help? Thanks kindly!

Holly
Last edited on
closed account (z05DSL3A)
Can you edit your post, and put code tags around your code. i.e. highlight your code and click the # format button on the right of the editor.

Spending a little time formatting your code would also help other read it.
Last edited on
closed account (z05DSL3A)
I had to format it because it was still hard to read.

Do you get any ouput from it at all? There may be a problem with the data file.

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
#include<fstream>
#include<iostream>
#include<iomanip>

using namespace std;

//---------------------------------------------------------------------------\\ 
class payroll
{
    ifstream fin;
    int n;
    char employeename[20];
    int hoursworked,overtime;
    double hourlyrate,overtimepay,regularpay,grosspay,taxrate,taxamount,netpay;
    double totalnetpay,avgnetpay;
    
    void calculategrosspay();
    void calculatetax();
    void calculatenetpay();
    void findavgnetpay();
    void printheadings();
    void printdata();

public:
    payroll();
    ~payroll();
    void printreport(); 
};

//---------------------------------------------------------------------------\\ 
payroll::payroll()
{
      fin.open("payrollclass.in"); 
}

//---------------------------------------------------------------------------\\       
payroll::~payroll()
{
      fin.close(); 
}

//---------------------------------------------------------------------------\\   
void payroll::calculategrosspay()
{
    if(hoursworked > 40)
    {
        overtime = hoursworked - 40;
        regularpay = hoursworked * hourlyrate;
        overtimepay = overtime * (hourlyrate * 1.5);
        grosspay = regularpay + overtimepay; 
    }
    else
    { 
        grosspay = hoursworked * hourlyrate; 
        regularpay = grosspay; 
        overtimepay = 0;
    }
}

//---------------------------------------------------------------------------\\ 
void payroll::calculatetax()
{
       taxrate = .30;
       taxamount = grosspay * taxrate; 
}

//---------------------------------------------------------------------------\\ 
void payroll::calculatenetpay()
{
    netpay = grosspay - taxamount;
    totalnetpay = totalnetpay + netpay;
}

//---------------------------------------------------------------------------\\ 
void payroll::findavgnetpay()
{
   avgnetpay = totalnetpay / n;
}   

//---------------------------------------------------------------------------\\ 
void payroll::printheadings()
{
    cout << setw(40)<<"-PAYROLL REPORT-"<<endl;
    cout << "---------------------------------------------------------" << endl;
    cout << " NAME     HW     HR     OT-PAY  GROSS   TAX   NETPAY"   << endl;
    cout << "---------------------------------------------------------" << endl;
}

//---------------------------------------------------------------------------\\ 
void payroll::printdata()
{
    cout << setprecision(2) << setiosflags(ios::fixed|ios::showpoint);
    cout << setw(6) << employeename << setw(6) << hoursworked << setw(8) <<
            hourlyrate << setw(8) << overtimepay << setw(8) << grosspay <<
            setw(8) << taxamount << setw(8) << netpay << endl; 
}

//---------------------------------------------------------------------------\\ 
void payroll::printreport()
{
    n = 0; 
    totalnetpay = 0;
    printheadings();
    while(fin >> employeename >> hoursworked >> hourlyrate)
    {
        calculategrosspay();
        calculatetax();
        calculatenetpay();
        printdata();
        n++; 
    }

    findavgnetpay();
    cout << endl << "The average net pay for " << n << " employees is " << avgnetpay << endl;

}
          
//---------------------------------------------------------------------------\\ 
int main()
{
    payroll employee;
    employee.printreport();
    system ("pause");
} 
//---------------------------------------------------------------------------\\ 
Last edited on
I do get output, but, it only computes correctly for part of the data. I experimented and created a new data file, saved it, changed it in my program, and the program executed with the old data file, and incorrect info. Any ideas? Thanks!
closed account (z05DSL3A)
The only thing that I have done to your code is changed the file open line to
fin.open("c:\\payrollclass.in", ifstream::in );
giving it a full path to the file so there is no confusion as to what file it may open.

I'm asumming your data file is a simple text file along the lines of:

Wolf 40 50.7
Frank 37 24.5
Jenny 50 10.2
sample payrollclass.in

I have compiled an tested the code with the above sample and it all seems to work.

I'm not overly familiar with Dec-C++ but I think that you can do a rebuild all from the Execute menu.

Something is wrong.....I made that change, and my program still stops working. I have no idea what to do. I am using Dev C++ 4.9.9.2 with MingW beta. Thank you for your help.
closed account (z05DSL3A)
OK,

Go to the Options menu and select Compiler Options. Click on the Linker tab and check Generate debugging information. Click on OK.

Now compile your code (Execute->compile) and run the debugger (Execute->Debug). You should get a command line with a prompt (gdb). type run and press enter.

You should get somthing along the lines of:

(gdb) run
run
Starting program: \DOCUME~1\XXXXXX\MYDOCU~1\Test/PROJEC~1.EXE
7c900000:ntdll.dllntdll.dll: No such file or directory.
7c800000:C:/WINDOWS/system32/kernel32.dll
77c10000:C:/WINDOWS/system32/msvcrt.dll
-PAYROLL REPORT-
---------------------------------------------------------
NAME HW HR OT-PAY GROSS TAX NETPAY
---------------------------------------------------------
Wolf 40 50.70 0.00 2028.00 608.40 1419.60
Frank 37 24.50 0.00 906.50 271.95 634.55
Jenny 50 10.20 153.00 663.00 198.90 464.10

The average net pay for 3 employees is 839.42


Copy and paste the result...







When I did that I had the same output as before, but lines of what looked like errors. I tried to copy those, but, the debugger would not let me.
closed account (z05DSL3A)
You should be able to right click on the title bar of the debugger and in the context menu select Edit->Select All then press return. You should then be able to paste the result into your post.

You need to tell us what the errors are, so if copy and paste fails manualy copy at least some of the error code. As I have said I can not get your code to fail (yet).
-PAYROLL REPORT-
---------------------------------------------------------
NAME HW HR OT-PAY GROSS TAX NETPAY
---------------------------------------------------------
Adam 40 10.00 0.00 400.00 120.00 280.00
Matt 40 10.00 0.00 400.00 120.00 280.00
Karen 45 10.00 75.00 525.00 157.50 367.50
Paula 40 10.00 75.00 400.00 120.00 280.00
Paul 45 15.00 112.50 787.50 236.25 551.25
Erica 40 10.00 112.50 400.00 120.00 280.00

The average net pay for 6 employees is


That is the output screen I get.
Last edited on
closed account (z05DSL3A)
Hmm...

with an input file of

Adam 40 10.00
Matt 40 10.00
Karen 45 10.00
Paula 40 10.00
Paul 45 15.00
Erica 40 10.00


I get the follwing output:

-PAYROLL REPORT-
---------------------------------------------------------
NAME HW HR OT-PAY GROSS TAX NETPAY
---------------------------------------------------------
Adam 40 10.00 0.00 400.00 120.00 280.00
Matt 40 10.00 0.00 400.00 120.00 280.00
Karen 45 10.00 75.00 525.00 157.50 367.50
Paula 40 10.00 0.00 400.00 120.00 280.00
Paul 45 15.00 112.50 787.50 236.25 551.25
Erica 40 10.00 0.00 400.00 120.00 280.00

The average net pay for 6 employees is 339.79


It looks like the OT-Pay for Paula and Erica has gone wrong in your run.

Is 'Erica' the last entry in the input file?

The only thing I can suggest is that you go to the directory where the project is and delete the .o files and the .exe and try a rebuild. From past things that you have said, such as you changed the file name but it still used the original file, it sound like either the build is failing to produce a new .exe or the wrong .exe is being run each time. You could also try creating a new project with a different name and copy the code across.
Thank you! Ok, I started a new program, copied and pasted, then executed.....here is my output:

-PAYROLL REPORT-
---------------------------------------------------------
NAME HW HR REGP OT-PAY GROSS TAX NETPAY
---------------------------------------------------------
Adam 40 10.00 400.00 0.00 400.00 120.00 280.00
Matt 40 10.00 400.00 0.00 400.00 120.00 280.00
Karen 45 10.00 450.00 75.00 525.00 157.50 367.50
Paula 40 10.00 400.00 0.00 400.00 120.00 280.00
Paul 45 15.00 675.00 112.50 787.50 236.25 551.25
Erica 40 10.00 400.00 0.00 400.00 120.00 280.00
The average net pay for 6 employees is $ 1.#J
Press any key to continue . . .



What do you think is wrong with the average net pay?? Thanks so much!
I figured it out with help! I had to take out the int n; so that the findavgnetpay call would work. Thanks!
Topic archived. No new replies allowed.