Program to calculate payroll continuously runs with no output.

This is my first semester of computer science and the program i wrote has no errors in at least that its telling me. I think it might be stuck in one of the loops but i cant figure out which one and its not printing out the information i would like at the end. I cannot find out anything that i did wrong and why it will not put out the results.

The text files that i am referencing in my code look like this.

HourlyPay.txt
L18 10.55
L19 11.87

and

TimeSheet.txt
10948 Carpenter Stewart 503-21-3387 L31 40.7
10949 Pleasance Marie 433-82-7832 L27 35

thank you for any help on this.
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
  #include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

typedef struct {
    string empID;
    string first;
    string last;
    string social;
    string payGrade;
    double hours;
} employee;

int main()
{
    ifstream time;
    ifstream hoursPay;
    string payGrade1;
    int payRate;
    double totalPayout = 0.00;
    int recordCnt = 0;

    time.open("TimeSheet.txt");


    if (time.fail())
    {
        cout << "Error: Cannot open file.";
        return 1;
    }

    while (!time.eof()) {
        string payCode;
        double rate = 0;
        int found = 0;
        recordCnt++;

        employee emp;
        time >> emp.empID;
        time >> emp.first;
        time >> emp.last;
        time >> emp.social;
        time >> emp.payGrade;
        time >> emp.hours;


        hoursPay.open("HourlyPay.txt");

        if (hoursPay.fail())
        {
            cout << "Error: Cannot open file. 1" << endl;
            time.close();
            return 1;
        }

        while (!hoursPay.eof())
        {
            hoursPay >> payGrade1 >> payRate;

            if (emp.payGrade == payGrade1)
            {
                found = 1;
                break;
            }

        }

        payRate = payRate * found;
        totalPayout += emp.hours * rate;

        cout << setw(10) << left << emp.empID;
        cout << setw(7) << left << emp.payGrade;
        cout << setw(10) << right << emp.hours * payRate;
        cout << setw(10) << right << emp.social;
        cout << " ";
        cout << left << emp.last;
        cout << " ";
        cout << left << emp.first;
        cout << endl;

        cout << recordCnt << " records found.";
        cout << " $ " << totalPayout << "total amount for payroll.";
        cout << endl;


        hoursPay.close();
        time.close();









    }


}





There are several things wrong with your file reading attempt.

The first problem is that you're using eof() to control the read loops, this will usually produce erroneous data. You should use the actual read to control the loops.

1
2
3
4
5
6
7
    employee temp;
    while(time >> emp.empID >> emp.first;
                     >> emp.last >> emp.social 
                     >> emp.payGrade >> emp.hours)
    {
          // The rest of the logic here.
    }



Next you appear to be closing both files at the end of the loop, but you haven't yet finished reading the first file. I suggest you have two structures, one to contain the information from the each of the files and store the information in a vector/array of each structure. Then you won't need to read each file more than once, just iterate thru the vector/array instead.



The reason for the infinite loop is that you are closing the time file inside of the first loop, and checking that the file is still readable by using time.eof(). Since time closed before hitting it's end, eof never breaks the loop.

It's a better idea to check a file is readable with time.good().

There's another problem with the second file, each time you open a file it opens back at the beginning line (or call it position zero). This would all work better if you did it in a single loop instead of nested loops.
1
2
3
4
5
6
7
8
9
10
11
12
// Sudo Code, don't try to compile
ifstream time("filePath");
ifstream pay("otherFilePath");
while(time.good() && pay.good())
{
    // get a line from time file
    // get a line from pay file
    // cout all the info

}
time.close();
pay.close();



Edit: Dang it, beaten by a couple of minutes. Jlb's method works well too, and the recommend on vectors is very much useful if the assignment asks for more than just merge and output the two files. Hard to do more without a full description of the assignment.
Last edited on
Topic archived. No new replies allowed.