程序正在运行,但并非所有内容都在显示中

问题描述 投票:1回答:1

所以基本上,当我运行程序时,仅显示在while循环中。 output_summarypayroll_summary的内容应显示在摘要标题的下方。

Output_summary将显示总工资总额,平均工资总额,最低工资总额和最高工资总额。

[Payroll_summary将显示员工的姓名和他们的个人总薪水。

我曾尝试以几种不同的方式重新排列代码,但唯一改变的是摘要标题出现了。我也将我的代码与我的一个朋友进行了比较,这非常相似,但我的代码无法正常工作。有人有建议吗?

这是我的代码:

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();
} 

我使用了调试器,这是似乎是问题的函数的代码:

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;
}

特别指出第七行

sum += gross_pays[i];

它说未处理异常

摘要输出中的0x002FD463的未处理异常。exe:0xC0000005:访问冲突读取位置0x00900000。

有人可以指导我下一步做什么吗?

c++
1个回答
0
投票

我认为问题出在for循环代码中

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

这里您在for循环的条件部分中写入了1 < num_employees。但是,如果变量num_employees大于1,则1总是小于num_employees。对于每次循环迭代,此结果都为true,因此我们继续增加i值。

在这种情况下,您尝试访问内存的其他位置(在某些迭代之后)。这就是为什么它引发访问冲突的例外。

希望这会有所帮助,谢谢

© www.soinside.com 2019 - 2024. All rights reserved.