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?
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
User contributions licensed under CC BY-SA 3.0