stuck on loop

My assignment has me making a wage calculator. It requires me to have an input and output file. Then input file follows as such....

1
10
39
0
0
1
15
41
2
1
1
10
49
2
1
1
10
51
2
1
0

the zero at the end signifies that the loop will stop. The inputted numbers go into variables in this order... cont(continue loop), rate , hours, dependents, fulltime. They then repeat for the next employee. The loop isnt outputting each employee, only the last employee
1, 10, 51, 2 ,1 . i have tried several different kinds of loops and parameters but still cant figure it out. any help would be great. thank you



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

using namespace std;

/* This function calculates taxes withheld based on gross pay,
    and the number of dependents entered by the user */

double computeWithholding(int dependents, double gross)

{
    double withheldAmount;

    if(dependents > 2)
        withheldAmount = 0.15;

    else if (dependents = 2)
        withheldAmount = 0.18;

    else if (dependents = 1)
        withheldAmount = 0.2;

    else                                //  no dependents
        withheldAmount = 0.28;


    withheldAmount = gross * withheldAmount;


    return (withheldAmount);

}



// This function calculates the IRA deduction based on gross pay

double computeIRAdeduction(double gross)

{
    double deductionAmount;

    if(gross >= 500)
        deductionAmount = gross * 0.10;

    else if (gross >= 400)
        deductionAmount = gross * 0.05;


    else
        deductionAmount = 0;


    return(deductionAmount);

}


double modNetPay(double netPay)	// pass by value, reconsider this when we review pass by reference
{
    double newNetPay;

    newNetPay = netPay - (netPay * 0.7);
    return(newNetPay);

}



// declaration for the function that computes taxes withheld


double computeWithholding(int dependents, double gross);

double computeIRAdeduction(double gross);

double modNetPay(double netPay);


int main()
{
    int hours = 0, empnumber = 1, overTimeHours=0, doubleTimeHours=0, dependents=0, fullTime =0, counter = 0;

    double  hourlyRate = 0, basePay=0, overTimePay=0, doubleTimePay=0, grossPay=0, modGrossPay=0, withholding=0, depositAmount=0, netPay=0;



    ifstream in_stream;


    in_stream.open("infile.dat");
    if(!in_stream)
    {
        cout << "infile.dat could not be accessed!" << endl;
        return 0;
    }

    while (!in_stream.eof())
    {
        in_stream >> counter >> hourlyRate >> hours >> dependents >> fullTime;


        overTimePay = 0;				// initialize
        overTimeHours = 0;

        doubleTimePay = 0;				// initialize
        doubleTimeHours = 0;

        empnumber ++;


        if (hours > 40)				// Test for overtime first
        {

            basePay = hourlyRate * 40;

            overTimeHours = hours - 40;

            if (overTimeHours > 10)			// If there's double time
            {
                doubleTimeHours = overTimeHours - 10;
                overTimeHours = 10;

                doubleTimePay = hourlyRate * 2 * doubleTimeHours;
                overTimePay = hourlyRate * 1.5 * overTimeHours;
            }

            else					// There's no double time
                overTimePay = hourlyRate * 1.5 * overTimeHours;



            grossPay = basePay + overTimePay + doubleTimePay;

        }


        else
        {
            // There's no overtime at all
            grossPay = hours * hourlyRate;
            basePay = grossPay;
        }




//  This calls the IRA deduction function and reduces gross pay
//  before withholding taxes are calculated

        depositAmount = computeIRAdeduction(grossPay);

        modGrossPay = grossPay - depositAmount;






// This calls the withholding amount function

        withholding = computeWithholding(dependents, modGrossPay);


        netPay = modGrossPay - withholding;


        if (fullTime == 1)
            modNetPay(netPay);




        //  OUTPUT SECTION


        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);


        ofstream out_stream;

        out_stream.open("outfile.dat");
        if(!out_stream)
        {
            cout << "outfile.dat failed!" << endl;
            return 0;
        }


        out_stream <<"Employee Number " << empnumber << endl
                   << "Total Hours Worked = " << hours <<endl
                   << "Hourly pay rate = $" << hourlyRate <<endl
                   << "Base pay = $" << basePay << endl
                   << "Overtime hours at time and a half = " << overTimeHours<< endl
                   << "Overtime pay at time and a half = $" << overTimePay<< endl
                   << "Overtime hours at double time = " << doubleTimeHours<< endl
                   << "Overtime pay at double time = $" << doubleTimePay<< endl
                   << "Gross pay = $" << grossPay<< endl
                   << "IRA Deduction = $" << grossPay<< endl
                   << "Modified Gross Pay = $" << modGrossPay<< endl
                   << "Taxes withheld = $" << withholding<< endl
                   << "Net pay = $" << netPay<< endl
                   << endl;

        in_stream >> counter >> hourlyRate >> hours >> dependents >> fullTime;

        if (counter != 1)
        {
            break;
        }
    }
    cout << "Process Successful......\n\n\n";
    return 0;
}

Last edited on
anyone?
this code
1
2
3
4
5
6
7
8
        ofstream out_stream;

        out_stream.open("outfile.dat");
        if(!out_stream)
        {
            cout << "outfile.dat failed!" << endl;
            return 0;
        }


should be at the top of main. Currently it is within your main loop. When you open a file for output if the file already exists the data is erased. So every time you out_stream.open("outfile.dat"); you erase any previous employee data
Ah thank you. Had a couple other problems as well. Thanks for the help
Topic archived. No new replies allowed.