A. Ray
A. Ray

Reputation: 81

Getting "terminating with uncaught exception of type std::length_error: vector" error C++

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".

Here's my code:

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <iterator>

using namespace std;

string getLetters() {
    string word; // Get user letter, put in variable word
    cout << "Enter a set of letters" << endl;
    cin >> word;
    return word;
}

int getLengthOfWord() {
    int length; // Get length of word
    cout << "Enter the number of letters in the word" << endl;
    cin >> length;
    return length;
}

// Change strings to vectors of chars
vector<char> stringToVector(string word) {
    std::vector<char> v(word.begin(), word.end());
    return v;
}

bool compareVectors(vector<char> userWord, vector<char> 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 < userWord.size(); i++) { // Iterating through the two words
        for (int j = 0; j < 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<char> vector1(stringToVector(word));

    while (wordList.is_open()) {
        getline(wordList, example); // Get word, put it in example variable

        vector<char> 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 << example << endl;
        }
    }
    wordList.close(); // Close stream

    return 0;

}

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 <20 elements.

Thanks for the help! (I'm new to C++ so if there's something I should obviously be doing differently, let me know).

Edit: The file I'm working with is the wordsEn.txt file from this website: http://www-01.sil.org/linguistics/wordlists/english/

Upvotes: 7

Views: 26756

Answers (3)

itsho
itsho

Reputation: 4800

In my case it was a mismatch between C++ standard on two vcxproj projects. I've simply aligned both projects to the same C++ standard (17) and it worked.

project ➤ PropertiesC/C++LanguageC++ Language Standard

Upvotes: 2

A. Ray
A. Ray

Reputation: 81

I was looking in the wrong place the whole time. I looked at the number of words/lines in the file (109582) and changed the

while (wordList.is_open()) {
        getline(wordList, example); // Get word, put it in example variable

        vector<char> 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 << example << endl;
        }
        counter++;
    }

to

while (counter < 109582) {
        getline(wordList, example); // Get word, put it in example variable

        vector<char> 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 << example << endl;
        }
        counter++;
    }

It seems I was getting some sort of overflow error by trying to read in more lines than were available in the file.

Upvotes: 0

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

One issue I see is that you are not erasing the character you claim you want to erase:

listWord.erase(listWord.begin() - 1 + j); 

This does not erase the jth character in the sequence.

The easiest example of this failing is if j == 0 at the start of the loop, and the first character matches.

Just simply do this instead:

listWord.erase(listWord.begin() + j); 

Upvotes: 0

Related Questions