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;
    }

I am getting a runtime error in my program. This the code fragment where the RTE occurs. Whenever I change the condition j < np-1 to j < np the error gets fixed. What's the matter? I haven't even used any array inside the second for loop for segfault.

5
  • What is np? What is it's value? Commented Nov 6, 2015 at 17:29
  • i and j are they int or unsigned int or ...? Commented Nov 6, 2015 at 17:38
  • 1
    Please post a relevant and valid code along with the error statement. Commented Nov 6, 2015 at 17:40
  • log the values of i and j, please; and tell us their vals when it segfaults. Also, if it holds that you "haven't even used any array in the second for loop", balis a associative container? Commented Nov 6, 2015 at 17:41
  • @Raul That's a bad suggestion. They should reduce the problem to a minimal but complete example using relevant code that reproduces the problem. Commented Nov 6, 2015 at 17:42

1 Answer 1

1

You're not mentioning what kind of runtime error, so this is conjecture...

With np - 1, your code doesn't match the input; there's one non-integer more left in the stream.

This means that cin >> total >> ppl fails, ppl becomes zero, and total / ppl is a division by zero.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.