【发布时间】:2016-02-07 22:12:59
【问题描述】:
for(i=0;i<np;i++){
cin >> temp_str;
pos = find(names.begin(), names.end(), temp_str) - names.begin();
cin >> total >> ppl;
giving.push_back(make_pair(pos, total));
amt_getting = total / ppl;
bal[pos] += total - (amt_getting * ppl);
for(j = 0; j < np - 1; j++){ /**** Error due to this loop's condition******/
cin >> temp_str;
pos = find(names.begin(), names.end(), temp_str) - names.begin();
bal[pos] += amt_getting;
}
我的程序出现运行时错误。这是 RTE 发生的代码片段。每当我将条件 j < np-1 更改为 j < np 时,错误就会得到修复。怎么了?我什至没有在第二个for 循环中使用任何数组来处理段错误。
【问题讨论】:
-
什么是
np?它的价值是什么? -
i和j是int或unsigned int还是 ...? -
请附上相关且有效的代码以及错误声明。
-
请记录 i 和 j 的值;并在出现段错误时告诉我们他们的 val。另外,如果它认为你“甚至没有在第二个 for 循环中使用任何数组”,
bal是一个关联容器? -
@Raul 这是一个糟糕的建议。他们应该使用重现问题的相关代码将问题减少到最小但完整的示例。
标签: c++ for-loop error-handling runtime-error