2015-11-06 30 views
0
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++

+0

什么是'np'?它的价值是什么? – NathanOliver

+0

'i'和'j'是它们的'int'还是'unsigned int'或...? – PinkFloyd

+1

请发布相关的有效代码以及错误陈述。 –

回答

1

你不提什么样的运行时错误的,所以这是猜想...

随着np - 1,您的代码不匹配输入;流中还剩下一个非整数。

这意味着cin >> total >> ppl失败,ppl变为零,并且total/ppl是零除。