WHILE Loop Error / instreaming data file (w/functions)

I have a working lab project with a loop error. Code posted in second post. Here's the requirements:

*
- program has to be able to handle file failure
- must loop (prefer to use a while loop here), read in the data, calculates values, etc. and then output the results.

*
The input data file includes

1. a 1 or a 0 indicating that there is a set of employee data following

2. the hourly rate of the employee

3. the humber of hours worked

4. the number of dependents

5. a 1 or a 0 indicating whether the employee is full time (1=full time).

Problem: At first I wouldn't loop back... so I moved the curly brace above the return0;

Now, however, it just runs forever.

Can anyone provide any feedback?


Thanks.
Last edited on
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
234
235
236
237
238
239
240
241
242
243
244
245

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

ifstream in_stream;
ofstream out_stream;


// void open files
void open_files ();

// void function declaration to calculate Overtime Pay
void calculateOvertimePay_function(int hoursWorked, double hourlyRate, double &basePay, int &overTimeHours, int &doubleTimeHours,
                                   double &overTimePay, double &grossPay, double &doubleTimePay);

// function declaration for IRA deposits
double IRA_function(double grossPay);

//function declaration for tax withholdings based on # of dependents
double withold_function(int &dependents, double modifiedGrossPay);

// function declaration to calculate medical benefit cost
double medical_cost_function(int &employmentStatus, double netPay, double &modifiedNetPay);

// function declaration for Payroll Output
void outputPayrollInfo_function(int recordNumber, int hoursWorked, int hourlyRate, int overTimeHours, int doubleTimeHours,
				int dependents, double basePay, double overTimePay, double doubleTimePay,
				double grossPay, double witholdCalc, double modifiedGrossPay, double iraDeductionAmount,
				double medicalBenefitCost, double modifiedNetPay);

//void output
void cout_Function ();



//start of main
int main()

{
int hoursWorked, overTimeHours, doubleTimeHours, dependents, employmentStatus, dataFlag, recordNumber = 0;
double hourlyRate, basePay, grossPay, netPay, overTimePay, doubleTimePay, modifiedGrossPay, modifiedNetPay, witholdCalc, iraDeductionAmount,
medicalBenefitCost;

  open_files ();

  in_stream >> dataFlag;


while (dataFlag == 1)
  {
  recordNumber = recordNumber + 1;

  in_stream >> hourlyRate >> hoursWorked >> dependents >> employmentStatus >> dataFlag;


// function call to calculate Overtime Pay
calculateOvertimePay_function(hoursWorked, hourlyRate, basePay, overTimeHours,
doubleTimeHours, overTimePay, grossPay, doubleTimePay);

// function call for IRA
iraDeductionAmount = IRA_function(grossPay);
modifiedGrossPay = grossPay - iraDeductionAmount;                 // modified gross pay calculation prior to tax

 // function call for withholdings
witholdCalc = withold_function(dependents, modifiedGrossPay);
netPay = modifiedGrossPay - witholdCalc;                        // net pay calculation of modified gross pay

// function call for medical benefits
medicalBenefitCost = medical_cost_function(employmentStatus, netPay, modifiedNetPay);
modifiedNetPay = netPay - medicalBenefitCost;                  //net Pay adjusted for medical benefits

// function call to output Payroll Info
outputPayrollInfo_function(recordNumber, hoursWorked, hourlyRate, overTimeHours, doubleTimeHours,
                            dependents, basePay, overTimePay, doubleTimePay, grossPay,
                            witholdCalc, modifiedGrossPay, iraDeductionAmount, medicalBenefitCost, modifiedNetPay);
// output function
cout_Function();

  }
  
return 0;

}
//end of main



void open_files()                                                                                                         // Open File Function
{
    in_stream.open("employeeData.dat");
  if (in_stream.fail())
    {
        cout << "Input file opening has failed.\n";
        exit(1);
    }


    out_stream.open("payrollData.dat");
  if (out_stream.fail())
    {
        cout << "Output file opening has failed.\n";
        exit(1);
    }
}


// void - function to calculate Overtime Pay

void calculateOvertimePay_function(int hoursWorked, double hourlyRate, double &basePay, int &overTimeHours, int &doubleTimeHours,
                                   double &overTimePay, double &grossPay, double &doubleTimePay)
{
  basePay = 0, overTimeHours = 0, overTimePay = 0, doubleTimeHours= 0, doubleTimePay = 0;


if (hoursWorked > 40)                                                                 //calculating if hours are over 40
{
    basePay = hourlyRate * 40;
    overTimeHours = hoursWorked - 40;
}
if (overTimeHours > 10)                                                               //calculating overtime and double time hours
{
    doubleTimeHours = overTimeHours - 10;
    overTimeHours = 10;
    doubleTimePay = hourlyRate * 2 * doubleTimeHours;                                 // calculating rates for over and double time
    overTimePay = hourlyRate * 1.5 * overTimeHours;
    grossPay = basePay + overTimePay + doubleTimePay;
}
else                                                                                    //accounting for over time below 10 hours
{
    basePay = (hoursWorked - overTimeHours) * hourlyRate;
    overTimePay = overTimeHours * (hourlyRate * 1.5);
    grossPay = basePay + overTimePay + doubleTimePay;
}
}


// IRA function definition.  Calculates deduction based on gross pay
double IRA_function (double grossPay)

{
    double iraDeductionAmount;

    if(grossPay >= 500)
        iraDeductionAmount = grossPay * 0.10;

    else if (grossPay > 400)
        iraDeductionAmount = grossPay * 0.05;

    else
        iraDeductionAmount = 0;

    return(iraDeductionAmount);

}



// Withholding function definition.  Calculates withholding based on # of dependents.
double withold_function (int &dependents, double modifiedGrossPay)

{

 double subtotal;

 double withold = 0;

 if (dependents > 2)
    withold = 0.15;

 else if (dependents == 2)
    withold = 0.18;

 else if (dependents == 1)
    withold = 0.2;

 else if (dependents == 0)
    withold = 0.28;

subtotal = modifiedGrossPay * withold;

return (subtotal);

}



// Medical benefit function definition.  Calculates medical benefits costs based on employment status (1 for FT, 0 for PT)
    double medical_cost_function(int &employmentStatus, double netPay, double &modifiedNetPay)

{
    double medBenCost = 0;                       // initializing costs to zero to account for PT

    if (employmentStatus == 1)
        medBenCost = netPay * .07;

    return (medBenCost);
}


void outputPayrollInfo_function(int recordNumber, int hoursWorked, int hourlyRate, int overTimeHours, int doubleTimeHours,
				int dependents, double basePay, double overTimePay, double doubleTimePay,
				double grossPay, double witholdCalc, double modifiedGrossPay, double iraDeductionAmount,
				double medicalBenefitCost, double modifiedNetPay)

{
  out_stream.setf(ios::fixed);
  out_stream.setf(ios::showpoint);
  out_stream.precision(2);
  out_stream.setf(ios::right);


out_stream << "************************************************" << endl;
out_stream << "    EMPLOYEE RECORD NUMBER  " << recordNumber << endl;
out_stream << "                        " << endl;
out_stream << "    Total Hours Worked = " << hoursWorked << endl;
out_stream << "    Hourly Pay Rate    = $" << hourlyRate << endl;
out_stream << "    Base Pay Rate      = $" << basePay << endl;
out_stream << "                        " << endl;
out_stream << "    Overtime hours at time and a half = " << overTimeHours <<endl;
out_stream << "    Overtime pay at time and half     = $" << overTimePay << endl;
out_stream << "    Overtime hours at double time     = " << doubleTimeHours << endl;
out_stream << "    Overtime pay at double time is    = $" << doubleTimePay << endl;
out_stream << "                         " << endl;
out_stream << "    Gross Pay ...........  = $" << grossPay << endl;
out_stream << "    IRA Deduction Amount   = $" << witholdCalc << endl;
out_stream << "    Modified Gross Pay     = $" << modifiedGrossPay << endl;
out_stream << "    Taxes Withheld         = $" << iraDeductionAmount << endl;
out_stream << "    Medical Benefit Deduct = $" << medicalBenefitCost << endl;
out_stream << "    Net Pay .............  = $" << modifiedNetPay << endl;
out_stream << "************************************************" << endl << endl << endl <<endl;
}



void cout_Function()                                  // Cout Function
{
    cout << "**********************" << endl
         << "Successful Completion!" << endl
         << "**********************" << endl;
  in_stream.close();
  out_stream.close();
}
Last edited on

Information from data file that will be read from "employeeData.dat" and then output to "payrollData.dat":
1
10
40
1
1
1
10
40
2
1
1
10
39
0
0
1
10
50
3
1
1
10
41
3
1
1
10
39
2
0
1
10
39
3
1
1
15
39
2
1
1
15
40
3
1
1
10
41
0
1
1
10
49
2
1
1
10
49
3
1
1
10
50
1
1
1
10
51
2
1
1
10
51
3
1
1
15
41
2
1
0
255
255
255
255

Last edited on
Topic archived. No new replies allowed.