嵌套for循环运行时错误C++
问题描述:
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
循环内部使用任何数组来实现段错误。嵌套for循环运行时错误C++
答
你不提什么样的运行时错误的,所以这是猜想...
随着np - 1
,您的代码不匹配输入;流中还剩下一个非整数。
这意味着cin >> total >> ppl
失败,ppl
变为零,并且total/ppl
是零除。
什么是'np'?它的价值是什么? – NathanOliver
'i'和'j'是它们的'int'还是'unsigned int'或...? – PinkFloyd
请发布相关的有效代码以及错误陈述。 –