程序运行,但并非所有内容都显示出来
Program runs but not everything is showing up
所以基本上当我运行程序时,只有显示的内容在while循环中。output_summary
和payroll_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
值。
在这种情况下,您正在尝试访问内存的其他位置(经过一些迭代(。这就是为什么它抛出访问冲突的异常。
希望这有帮助, 谢谢
相关文章:
- 代码使用向量成功运行,但使用数组显示错误
- 为什么当 vector 为空时会显示运行时错误?
- libcurl :显示正在运行的上传和下载速率
- 任何人都可以弄清楚这段代码如何显示运行错误?它打印无限时间 -1 以及正确答案
- 程序运行,但并非所有内容都显示出来
- 为什么当我在 c++ 中运行函数时,我的代码显示数字 53
- C++ .我正在尝试通过使用开关内的数组来获取用户输入,但是当我运行代码时,它显示分段错误?
- 如何检查输入是否为字符并显示它是"invalid input",否则它将按原样运行
- 在代码仍在 C++ QT 中运行时显示打印输出
- C++代码编译,但在 Zorin OS 上运行时显示错误
- 如何在函数运行时逐个显示列表项
- 由于错误,c ++代码无法在Visual Studios上运行,但不显示任何错误
- 在语法文件上运行C 的ANTL4解析器显示错误33:缺少代码生成模板非局部trrefheader
- 如何在背景中运行vtkrenderwindow而不向用户显示
- 程序在运行时不会显示任何内容,只显示'0'值
- 当我做出语句 root->right->right = newnode(7);注释,编译器显示运行时错误。谁能解释为什么?
- 尝试拆分循环链表.显示输出但崩溃.在 IDEone 中显示运行时错误
- 如何创建多边形以在Cocos2DX中显示运行号
- C 程序对我有用,但在线显示运行时错误
- 使用 const char 数组参数分析 constexpr 显示运行时执行