Outputting .txt file into code

We are supposed to use an OutFile in order to show a timecard program. But instead of getting the desired result:

http://img265.imageshack.us/img265/1358/screendhu.png

But I only get 3 of the lines of output code to show up:

http://es.tinypic.com/r/35bf0pd/5

...Here is the code that I used:

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

using namespace std;

int main()
{
    int id,hours;
    float rate,regpay,otpay,total;
    ifstream  payroll;
    ofstream outFile;
    payroll.open("mypayroll.txt");
    outFile.open("payroll.txt");

        cout <<right <<setprecision(2)<<fixed;
        cout <<setw(10) <<"Emp. Id"
             <<setw(10) <<"Hours"
             <<setw(10) <<"Pay Rate"
             <<setw(10) <<"Reg. Pay"
             <<setw(10) <<"Ot Pay"
             <<setw(10) <<"Total Pay" <<"\n\n";

      outFile<<right <<setprecision(2)<<fixed;
      outFile<<setw(10) <<"Emp. Id"
             <<setw(10) <<"Hours"
             <<setw(10) <<"Pay Rate"
             <<setw(10) <<"Reg. Pay"
             <<setw(10) <<"Ot Pay"
             <<setw(10) <<"Total Pay" <<"\n\n";
        
        payroll >> id
              >> hours
              >> rate;
     while ( ! payroll.eof())
{
    payroll >> id
            >> hours
            >> rate;

    if (hours<=40)
    {
           regpay=hours*rate;
           otpay=0;
    }
    else
    {
            regpay=hours*rate;
            otpay=(hours-40)*rate*1.5;
    }
            total=regpay+otpay;


        cout << setw(10) <<id
             << setw(10) <<hours
             << setw(10) <<rate
             << setw(10) <<regpay
             << setw(10) <<otpay
             << setw(10) <<total <<endl;
     outFile << setw(10) <<id
             << setw(10) <<hours
             << setw(10) <<rate
             << setw(10) <<regpay
             << setw(10) <<otpay
             << setw(10) <<total <<endl;

     payroll >> id
            >> hours
            >> rate;
    payroll.close();
    outFile.close();


}
    return 0;
}


And here is what reads on the "mypayroll.txt" file...so all the lines should show up, but only line 1-22-33 show up:

id, hours, rate
1, 20, 25.00
12, 46, 10.00
22, 40, 12.00
27, 40, 11.00
33, 42, 10.75

It calculates everything correctly, I am just not sure how to get it all to show up...and I'm not quite sure how to add the code at the bottom of the top picture either.

Any help is greatly appreciated! I tried to explain it the best I could...but I'm still very new at this.
you have extra "payroll >> id >> hours >> rate;". when ever you use that it moves to cursor for reading the file therefore you skip lines by using it too many times. to fix ur problem delete line 32 - 34 and 67 - 69. also when i ran the program i didnt line number 1, i dunno how you did
Thanks that actually worked! But do you know how to add up the number of employees and everything like it has on the first picture?
just create variables before the while loop and these variables will hold the total of the columns. set each of them to 0. when youre reading the values from the file (or after you calculate a value, such as overtime pay) just add the value onto your total value each time and after the while loop you should be able to display however the values however you want.

Step by step example for pay total:

1. Create a pay total variable before the while loop (total variables should be the same type as the single variable). Set the total to 0.

2. In the while loop, add the calculated value of the employees pay onto the total

3. After while loop, display the value however you want

Do this for every total value

P.S. for employee total just add one onto the total every time the while loop loops
Topic archived. No new replies allowed.