title: Vector subscript out of range. Line 1140. C++
body: <p>I'm reading in a file that is a question pool that has the type of question, chapter, how many points it is worth, the question and the answer. This bit of code is checking to see if the min and max chapters(from user input) are in range(from a file of unknown size). I know that it adds an extra line at the end of the vector, which is causing the error, but how can I fix it? The code is:</p>
  
  <pre><code>void checker(int min, int max, string file) {
  
          ifstream myfile;
          string line;
          vector&lt;int&gt; numlist;
  
          myfile.open(file);
          while (myfile.is_open()) {
              if (!getline(myfile, line)) {
                  break;
              } else {
                  vector&lt;string&gt; chap = split_string(line);
                  int chapter = str2int(chap[2]);
                  numlist.push_back(chapter); //This is where the error is. Makes vector go out of range.
              }
          }
  
          int small = 1000;
          int large = 0;
          for (size_t i = 0; i &lt; numlist.size(); i++) {
              if (numlist[i] &lt; small) {
                  small = numlist[i];
              }
          }
          for (size_t i = 0; i &lt; numlist.size(); i++) {
              if (numlist[i] &gt; large) {
                  large = numlist[i];
              }
          }
          if (min &gt; max) {
              cout
                  &lt;&lt; "Error: Please enter a number lower than or equal to the maximum chapter: "
                  &lt;&lt; endl;
              cin &gt;&gt; min;
              cout &lt;&lt; endl;
          } else if (min &lt; small) {
              cout
                  &lt;&lt; "Error: Please enter a number bigger than or equal than the minimum chapter ("
                  &lt;&lt; small &lt;&lt; "): " &lt;&lt; endl;
              cin &gt;&gt; min;
              cout &lt;&lt; endl;
          } else if (max &gt; large) {
              cout
                  &lt;&lt; "Error: Please enter a number bigger than or equal than the maximum chapter ("
                  &lt;&lt; large &lt;&lt; "): " &lt;&lt; endl;
              cin &gt;&gt; max;
              cout &lt;&lt; endl;
          }
          myfile.close();
  }
  </code></pre>