PLEASE HELP program only reading first line of txt file

Mar 3, 2015 at 1:16am
I need help. I wrote this code. It takes information from a txt file and calculates pay, tax, and net pay. However, I cant get it to read more than one line. It only reads the first line! Please help!!

This is the text file
101456 H 20 6.57
100045 S 81994.12
100321 H 45 23.50
101987 H 39 15.76
100486 S 116935.65
100357 H 50 18.19
102003 H 30 12.75
123887 S 226345.43
110441 S 74783.28
101119 H 35 22.22
114532 S 152573.23
100003 H 40 19.00
101285 H 60 29.95
105539 S 130284.54
176531 S 88546.77
101119 H 38 43.15

This is the 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
77
78
79
 // Written By ML 2/20/2015
 // This program calculates gross pay for each employee
 // It also calculates tax's held and net pay
 #include <iostream>
 #include <fstream>
 #include <string>

 using namespace std;

 int main()
 {
 int EmployeeID, Hours;
 double Gross, Tax, Pay, Net, Salary;
 char Classification;
 //Open Folder to get information
 //Employee ID, Code for classification, hours, salary
 ifstream infile;
 infile.open("payData.txt");
 cout << "Reading from the file" << endl;
 // If it is the incorrect file
 if (!infile)
 {
 cout << "Unable to Open File" << endl;
 exit(1); // Stop System
 }
 while (!infile.eof())
 {
 {
 infile >> EmployeeID >> Classification;
 if (Classification == 'H')
 {
 infile >> Hours >> Pay;
 }
 else
 {
 infile >> Salary;
 }
 }
 // Do Calculations Gross
 {
 if (Classification == 'H')
 {
 if (Hours > 40)
 Gross = (40 * Pay) + ((Hours - 40)* (Pay * 1.5));
 else
 Gross = Pay * Hours;
 }
 else
 Gross = Salary / 26;
 }
 // Do Calculations for Tax Withheld
 {
 if (Gross < 935)
 Tax = Gross * .15;
 else if (Gross < 2880)
 Tax = ((Gross - 935) * .28) + 140.25;
 else if (Gross < 4200)
 Tax = ((Gross - 2800) * .33) + 680.55;
 else if (Gross > 4200)
 Tax = Gross * .4;
 }
 // Do Calculations for Net
 {
 Net = Gross - Tax;
 }

 }

 // Write Cout statments for user
 {
 cout << "Employee ID" << " " << "Gross" << " " << "Tax" << " " << "Net" << endl;
 cout << EmployeeID << " " << Gross << " " << Tax << " " << Net << endl;
 }
 //Close the file
 infile.close();

 system("Pause");
 return 0;
 }
Mar 3, 2015 at 9:05am
You should indent your code better. Then you'd see that // Write Cout statments for user is outside of the loop. This way you process all lines, but the final calculation is only for the last one.
Mar 4, 2015 at 1:18am
Okay so that needs to be inside the brackets?
Mar 4, 2015 at 6:59am
Yes, inside the braces that belongs to the while loop. infile.close(); remains outside the loop, where it is now.
Topic archived. No new replies allowed.