title: Getting "terminating with uncaught exception of type std::length_error: vector" error C++
body: <p>I'm writing a program to help solve crossword puzzles. So I'm getting a word from a text list of all words in the english language, making each one a vector of chars, and comparing that vector to a vector of whatever starting letters I have. It runs fine and gives me good output, but every time I'm getting an error "libc++abi.dylib: terminating with uncaught exception of type std::length_error: vector".</p>
  
  <p>Here's my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;string&gt;
  #include &lt;iterator&gt;
  
  using namespace std;
  
  string getLetters() {
      string word; // Get user letter, put in variable word
      cout &lt;&lt; "Enter a set of letters" &lt;&lt; endl;
      cin &gt;&gt; word;
      return word;
  }
  
  int getLengthOfWord() {
      int length; // Get length of word
      cout &lt;&lt; "Enter the number of letters in the word" &lt;&lt; endl;
      cin &gt;&gt; length;
      return length;
  }
  
  // Change strings to vectors of chars
  vector&lt;char&gt; stringToVector(string word) {
      std::vector&lt;char&gt; v(word.begin(), word.end());
      return v;
  }
  
  bool compareVectors(vector&lt;char&gt; userWord, vector&lt;char&gt; listWord, int length) {
  
      if (listWord.size() != length) // Make sure the word from the list is the right length
      {
          return false;
      }
  
      int counter = 0; // Counter
  
      for (int i = 0; i &lt; userWord.size(); i++) { // Iterating through the two words
          for (int j = 0; j &lt; listWord.size(); j++) {
              if (listWord[j] == userWord[i]) { // If the letters match
                  listWord.erase(listWord.begin() - 1 + j); // Erase the letter from the word
                  counter++; // Increase counter
                  break; // Break out of for loop
              }
          }
      }
  
      if (counter == userWord.size()) { // If there were as many matches as letters in user set
          return true;
      }
      else {
          return false;
      }
  
  }
  
  int main() {
  
      string example; // variable to put words
      ifstream wordList; // New ifstream object
      wordList.open("/Users/alexray/Dropbox/C++ Practice/WordJumbleSolver/wordsEn.txt"); //open word list
  
      int length = getLengthOfWord(); // Get user input
      string word = getLetters();
  
      vector&lt;char&gt; vector1(stringToVector(word));
  
      while (wordList.is_open()) {
          getline(wordList, example); // Get word, put it in example variable
  
          vector&lt;char&gt; vector2(stringToVector(example)); // Make word from list a vector
          vector2.erase(vector2.end() - 1); // Erase escape character from end of word
          if(compareVectors(vector1, vector2, length)) { // compare the vectors
              cout &lt;&lt; example &lt;&lt; endl;
          }
      }
      wordList.close(); // Close stream
  
      return 0;
  
  }
  </code></pre>
  
  <p>From googling around, I thought that it was a matter of my vector wasn't initially large enough to handle some of the words, but doing vector.reserve(some_number) before assigning a value to the vector didn't help anything. Also, I couldn't imagine that a vector would have any problems with &lt;20 elements.</p>
  
  <p>Thanks for the help! (I'm new to C++ so if there's something I should obviously be doing differently, let me know).</p>
  
  <p><em>Edit</em>: The file I'm working with is the wordsEn.txt file from this website: <a href="http://www-01.sil.org/linguistics/wordlists/english/" rel="noreferrer">http://www-01.sil.org/linguistics/wordlists/english/</a></p>