Dan G
Dan G

Reputation: 9

C++ terminate called after throwing an instance of 'std::out_of_range' Simple Averaging code

Comp Sci 201 Student here. I'm running a simple averaging coded but I keep getting this error, any words of wisdom?

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {

int userIn = 0;
int numSum = 0;
cin >> userIn;

vector<int>v;

   for(int i = 0; userIn !=-1;i++) {
      v.at(i) = userIn;
      v.push_back(i);
      cin >> userIn;
   }

   for(unsigned i = 0; i <= v.size(); i+= 3) {
   numSum = v.at(i) + v.at(i+1) + v.at(i+2);
   }

   cout << numSum/3;

}

Keep getting this error terminate called after throwing an instance of

'std::out_of_range' what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)

after using an input of 4 4 4 -

Seems like it's throwing out my vector, but shouldn't my vector be a size of 3 now? Larger than 0?

Thanks, I know I'm a total newbie.

Upvotes: 0

Views: 3780

Answers (3)

Remy Lebeau
Remy Lebeau

Reputation: 597166

There are several problems with your code:

  1. the 1st loop is calling at() on an empty std::vector on the very first iteration, which will throw a std::out_of_range exception that you are not catching.

  2. the 1st loop is not pushing the user's input into the std::vector. It is pushing indexes instead.

  3. even if the 1st loop was OK, the 2nd loop is going past the upper bound of the std::vector, regardless of how many items you push into it. So there is another std::out_of_range exception.

  4. the 2nd loop also assumes the number of items in the std::vector is an even multiple of 3, but the 1st loop does not guarantee that. So there is another std::out_of_range exception waiting to happen.

  5. you are not calculating the average correctly. The final value of numSum will be the sum of only the last 3 values in the std::vector, and then the final average is being calculated by dividing numSum by 3. So, you are outputting the average of only the last 3 values in the std::vector. If you want the average of all values, you need to add all of the values into numSum and then divide it by the total number of values in the std::vector.

Try something more like this instead:

#include <iostream>
#include <vector>

using namespace std;

int main() {

    int userIn = 0;
    int numSum = 0;

    vector<int> v;

    cin >> userIn;
    while (userIn != -1) {
        v.push_back(userIn);
        cin >> userIn;
    }

    if (!v.empty()) {
        for(vector<int>::size_type i = 0; i < v.size(); ++i) {
            numSum += v[i];
        }    
        cout << numSum / v.size();
    }
    else
        cout << "nothing was entered!";

    return 0;
}

Upvotes: 2

Mrinal Verma
Mrinal Verma

Reputation: 359

I m not able to understand what you are doing but there are some errors in your code

1) for using the function 'at' you need to declare your vector with initial size else it would throw an out_of_range error.

2) if you observe carefully you are running your loop with constraint on userIn variable but you are also taking your inputs in for loop with this value, so your userIn variable value is getting modified each time your for loop executes which will lead to infinite loop.so for user input you can use a new variable acc to your need

3)In second for loop your iterator I can go out of bound for ex if vector size is 4 and your i==3 you're for loop according to terminate condition will work as a consequence of this, you will be accessing value at position 3,4,5 whereas 4 and 5 are not defined which will lead to a segmentation fault. by the way happy coding

Upvotes: 0

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33904

No, your vector wont be three, because the error is happening before you do anything:

vector<int>v; // This vector is empty length

for(int i = 0; userIn !=-1;i++) {
  v.at(i) = userIn; // You are attempting to get an index 
                    // from an empty vector. Failure will occur!

Upvotes: 2

Related Questions