title: C++ Vector subscript out of range Line 1201
body: <p>I am working on a program that takes in grades from the user and returns the max, min, avg, median, and standard deviation. I keep encountering this error whenever I try to run my code:</p>
  
  <p>File: c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector</p>
  
  <p>Line: 1201</p>
  
  <p>Expression: Vector subscript out of range</p>
  
  <p>Could someone tell me what I'm doing wrong? No matter what I do, I can't seem to fix it. Completely new to coding in C++, so if you could explain in-depth that would be extremely helpful.</p>
  
  <p>This is the block of code it is referring to in the vector file:</p>
  
  <pre><code>#if _ITERATOR_DEBUG_LEVEL == 2
  if (size() &lt;= _Pos)
  {   // report error
      _DEBUG_ERROR("vector subscript out of range");
      _SCL_SECURE_OUT_OF_RANGE;
  }
  </code></pre>
  
  <p>Specifically pointing to this line:</p>
  
  <pre><code>    _DEBUG_ERROR("vector subscript out of range");
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  using namespace std;
  
  void enterGrades()
  {
      int count = 0;//initialize count
      int grade;//initialize grade variable
      int maxGrade = 0;
      vector&lt;int&gt; gradeList;//initialize vector
      vector&lt;int&gt;::iterator gradeListIt;//initialize iterator
      do {
          cout &lt;&lt; "Enter grades [0 to exit]: ";//enter grades
          cin &gt;&gt; grade;//user input
          count += 1;//for each grade, increase count
          gradeList.push_back(grade);//append grades to vector    
      } while (grade != 0);
  };
  
  int maxNum(vector&lt;int&gt; gradeList)
  {
      int largest = gradeList[0];//initialize largest number
      int length = gradeList.size();
      gradeList.resize(length);
      for (int value = 0; value &lt; length; value++)//traverse vector
      {
          if (gradeList[value] &gt; largest)//if current value &gt; largest
              largest = gradeList[value];//set largest to that value
      }
      return largest;//return largest
  
  };
  
  int minNum(vector&lt;int&gt; gradeList)
  {
      int smallest = gradeList[0];
      int length = gradeList.size();
      gradeList.resize(length);
      for (int value = 0; value &lt; length; value++)
      {
          if (gradeList[value] &lt; smallest)
              smallest = gradeList[value];
      }
      return smallest;
  };
  
  int avgNum(vector&lt;int&gt; gradeList)
  {
      int total = 0;
      int length = gradeList.size();
      gradeList.resize(length);
      for (int value = 0; value &lt; length; value++)
      {
          total += value;
      }
      return total / length;
  
  };
  
  //int stdDev (vector&lt;int&gt;&amp; gradeList)
  //{
  //    int variance = 0;
  //    int avg = avgNum(vector&lt;int&gt;&amp; gradeList);
  //    int length = gradeList.size();
  //    for(int value = 1; value &lt; length; value++)
  //    {
  //            variance = variance + pow(value - avg, 2);
  //    }
  //    variance = pow(variance / length, 0.5);
  //    return variance;
  //    
  //};
  
  
  
  int main()
  {
      vector&lt;int&gt; gradeList;//initialize vector
      vector&lt;int&gt;::iterator gradeListIt;//initialize iterator
      enterGrades();//
      cout &lt;&lt; "Maximum grade is: " &lt;&lt; maxNum(gradeList) &lt;&lt; endl;
      cout &lt;&lt; "Minimum grade is: " &lt;&lt; minNum(gradeList) &lt;&lt; endl;
      cout &lt;&lt; "Average grade is: " &lt;&lt; avgNum(gradeList) &lt;&lt; endl;
  
  }
  </code></pre>
  
  <p>Also, in my main function; can I call other functions like I did in my output statements?</p>