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
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.