I/O, output problem

Hello All.

I am having trouble with producing a report file containing results from a read file.

The entire code is posted on the link below.
https://github.com/ashley51/payroll_prog/blob/master/Lab14_Payroll_INFILE_COPY.cpp

It somehow only processes one line from the bottom of the file it reads from but it doesn't display the last and first name, the rest of the values are correct.

How do I modify the coding in order to read the rest of the data from the file and continuously process the rest of the data and display the list of processed employees under summary?
Have you considered a class or a structure to hold the information for your employees? Then use a vector of this class or structure to hold the information for each employee for later processing.

Your current code only has one set of variables that you keep overwriting when reading the file.

I can't seem to figure out why the
'output_payroll_summary' function is not displaying any data.
The for loop doesn't seem to be receiving any data...

It can correctly display the calculated data for the first table containing:
full_name, regular_hours, overtime_hours, gross_pay, net_pay, tax and deductions.


I would really appreciate some assistance on how/where to correct my coding.

Latest code.
https://github.com/ashley51/payroll_prog/blob/master/Lab14_Payroll_INFILE_COPY.cpp
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.