0

So basically when I run the program, only what shows up is in the while loop. What output_summary and payroll_summary does should show up underneath the summary header. <- this is what is supposed to happen

Output_summary would show total gross pay, average gross pay, minimum gross pay, and maximum gross pay.

Payroll_summary would show the employee's names and what their individual gross pay is.

I've tried rearranging the code several different ways but the only thing that changes is that the summary header shows up. I also have compared my code with a friend of mine and it's very similar yet mine fails to work. Does anyone have suggestions?

Here is my code:

int main()
{
    std::string full_name,
        first_name,
        last_name;

    double hours,
        regular_hours,
        overtime_hours,
        hourly_rate,
        gross_pay,
        taxes,
        net_pay,
        deduction,
        average,
        sum,
        gp_max,
        gp_min;

    int num_employees = 0;
    std::string full_names[SIZE];
    double gross_pays[SIZE];

    std::ifstream infile;  // input stream for reading from a file
    std::ofstream outfile; // output stream for writing to a file

    // Open the input file stream
    infile.open("c:\\temp\\employeeinfo.txt");
    if (!infile.is_open()) {
        std::cout << "Cannot open input file" << std::endl;
        exit(EXIT_FAILURE);
    }

    // Open the file output stream
    outfile.open("c:\\temp\\report.txt");
    if (!outfile.is_open()) {
        std::cout << "Cannot open output file" << std::endl;
        exit(EXIT_FAILURE);
    }

    // Displaying the header once 
    show_header(outfile);
    show_header(std::cout);

    // Attempt to read the first record from the input file
    input_data(infile, first_name, last_name, hours, hourly_rate, deduction);

    // add a message for when the file is empty

    // add test conditions for employee less than size
    while (!infile.eof() && num_employees < SIZE) {  
        // Do not need an input section

        // Processing Section
        full_name = join_names(first_name, last_name);
        compute_oth(hours, regular_hours, overtime_hours);
        gross_pay = compute_gp(regular_hours, overtime_hours, hourly_rate);
        taxes = compute_taxes(gross_pay);
        net_pay = compute_np(gross_pay, taxes, deduction);

        // Output Section
        display_results(std::cout, full_name, regular_hours, overtime_hours, 
                        hourly_rate, gross_pay, taxes, net_pay, deduction);
        display_results(outfile, full_name, regular_hours, overtime_hours, 
                        hourly_rate, gross_pay, taxes, net_pay, deduction);

        // Store Data in Arrays
        full_names[num_employees] = full_name;
        gross_pays[num_employees] = gross_pay;
        num_employees += 1;

        //Attempt to read another record from the input file
        input_data(infile, first_name, last_name, hours, hourly_rate, deduction);
    }

    // Processing Summary Information
    sum = sum_gp(gross_pays, num_employees);
    average = average_gp(gross_pays, num_employees);
    gp_max = max_gp(gross_pays, num_employees);
    gp_min = min_gp(gross_pays, num_employees);
    sort_gp(gross_pays, num_employees, full_names);

    // Output Summary Information
    show_summary_header(std::cout);
    output_summary(std::cout, sum, average, gp_max, gp_min);
    payroll_summary(std::cout, num_employees, full_names, gross_pays);

    show_summary_header(outfile);
    output_summary(outfile, sum, average, gp_max, gp_min);
    payroll_summary(outfile, num_employees, full_names, gross_pays);

    // Close the input file stream
    infile.close();

    // Close the output file stream
    outfile.close();
} 

I used a debugger and this is the code for the function that seems to be the problem:

double sum_gp(double gross_pays[], int num_employees)
{
    int i;
    double sum;

    sum = 0;
    for (i = 0; 1 < num_employees; i++) {
        sum += gross_pays[i];
    }

    return sum;
}

It specifically pointed out the seventh line

sum += gross_pays[i];

It says Exception Unhandled

Unhandled exception at 0x002FD463 in summary output.exe: 0xC0000005: Access violation reading location 0x00900000.

Could someone guide me as what to do next?

yoon k
  • 9
  • 2
  • 2
    `while (!infile.eof()...)` is not a recommended construct. Many things can go wrong. – 3Dave Dec 16 '19 at 04:01
  • @3Dave Thank you for your response! I changed it to just infile but the same thing still happens. – yoon k Dec 16 '19 at 04:08
  • 2
    here is info on why not to use eof [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Dec 16 '19 at 04:08
  • I believe you will have to use a debugger to figure this out. – drescherjm Dec 16 '19 at 04:12
  • What does your declaration of `SIZE` look like? Not necessarily important to the question, but it could become important if you are doing it wrong--especially if there are no other good suspects. –  Dec 16 '19 at 04:30
  • @Chipster `#define SIZE 10 ` and I currently have three things on the input file – yoon k Dec 16 '19 at 04:33
  • @yoonk `for (i = 0; 1 < num_employees; i++) {` -- See something strange in that loop construct? You could avoid stuff like this without writing a loop: `return std::accumulate(gross_pays, gross_pays + num_employees, 0.0);` – PaulMcKenzie Dec 16 '19 at 04:34
  • @Yoonk ah, well that should be fine then. Forget that last comment. –  Dec 16 '19 at 04:37
  • ***Could someone guide me as what to do next?*** Break into the debugger and see what line of your code caused the access violation by switching the `Stack Frame` to your code. – drescherjm Dec 16 '19 at 04:39
  • @PaulMcKenzie I'm not exactly sure but does it have to do with `num_employees`? – yoon k Dec 16 '19 at 04:42
  • `for (i = 0; 1 < num_employees; i++)` you used `1` instead of `i` for the loop condition. that would be a clear reason for an access violation. However you should have seen that (i was too large) in your debugger after the access violation. – drescherjm Dec 16 '19 at 04:43
  • @PaulMcKenzie thanks to one of the comments I see what is wrong now. Thank you, I will keep that in mind for the future! – yoon k Dec 16 '19 at 04:49
  • @drescherjm ohh I see now. Thank you! – yoon k Dec 16 '19 at 04:52

1 Answers1

0

I think the problem is with the for loop code

 for (i = 0; 1 < num_employees; i++) {
    sum += gross_pays[i];
 }

Here you have written 1 < num_employees in the condition part of the for loop. But 1 is always less than num_employees provided the variable num_employees is greater than 1. This evaluates to true for each loop iteration and we keep on increasing the i value.

In such case, you are trying to access other location of memory(after some iterations). That is why it is throwing an exception of access violation.

Hope this helps, Thanks