Not a complete analysis, but a quick read through the code shows a lot of repetition and duplication of code.
For example, there are two functions,
input_employee_data()
and
read_data()
each of which does effectively the same thing. There are two loops attempting to read and process the file, at lines 106 and 141. The same calculations, for example
calculate_gross_pay()
are duplicated in these two separate processing blocks.
It would probably be a good idea to consider doing all of this just once, rather go to the trouble of writing the same code all over again.
However, in the immediate situation, after reaching the end of file (one hopes!) on the first pass, before attempting to re-read, the file must be readied for the second pass. That means resetting the status flags, (use
infile.clear();
) and repositioning to the start (use
infile.seekg (0);
).
http://www.cplusplus.com/reference/ios/ios/clear/
http://www.cplusplus.com/reference/istream/istream/seekg/
Usually I would issue a warning about the dire perils endangered by the use of
while (!infile.eof())
but in this case the code is structured such that it is at least feasible that it could work correctly - though I'd reserve judgement on that until I'd tried to compile and run it with the appropriate test data.
However, personally I'd still prefer to write a file access function which returns a
bool
rather than
void
, to indicate when a record has or has not been retrieved from the file. Then any such concerns over possible misuse of eof are removed.
Edit:
One thing which sticks out as being out of place is the for loop at lines 133-138
133 134 135 136 137 138
|
for (i = 0; i < num_emps; i++)
{
outfile << std::left << std::setw(20) << emp_names[i] << " "
<< std::right << std::setw(8) << emp_grosses[i]
<< std::endl;
}
| |
Why is it out of place? Well, firstly it is nested inside the loop where you are reading each record from the file. That means if it did work, it would output one record, then two records, then three records and so on. Secondly, it is out of place because at that stage,
num_emps
is zero, and none of the arrays have been assigned any values.