程序运行,但并非所有内容都显示出来

Program runs but not everything is showing up

本文关键字:显示 运行 程序      更新时间:2023-10-16

所以基本上当我运行程序时,只有显示的内容在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。

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

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

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

在这里,您已经在 for 循环的条件部分中编写了1 < num_employees。但 1 始终小于 num_employees 前提是变量num_employees大于 1。对于每个循环迭代,这都是 true,并且我们继续增加i值。

在这种情况下,您正在尝试访问内存的其他位置(经过一些迭代(。这就是为什么它抛出访问冲突的异常。

希望这有帮助, 谢谢