7

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/

5
  • One of the 1st essential skills to achieve with programming is to learn how to use your debugger. You step through your program line by line, and find which piece of code triggers the exception. Commented Aug 25, 2015 at 17:03
  • 1
    but doing vector.reserve That does not set the size of the vector. The function you want is vector::resize() Commented Aug 25, 2015 at 17:06
  • Thanks, I changed the function to resize, but even when I resized it to a ridiculous amount, it still gets the error. Commented Aug 25, 2015 at 18:17
  • @A.Ray According to the comments in your code, why do you think there is an escape character at the end of the string to remove? getline(wordList, example); // Get word, put it in example variable and this vector2.erase(vector2.end() - 1); // Erase escape character from end of word? Commented Aug 25, 2015 at 18:24
  • @PaulMcKenzie Using the debugger and looking at the values of elements in the vector. Once the line is read into the string variable and put into a vector, the last element of the vector is a "\r". That was one of my earlier bugs that I had to fix. Commented Aug 25, 2015 at 18:57

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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); 

4 Comments

Thanks for the help, I changed that line to what you suggested. I don't know why I got confused with the indexes... Unfortunately, doing that and resizing the vector instead of reserving still did not help get rid of the error. Any other ideas?
Then please post the data that is causing the issue, or hardcode the data in your question so that others can see what you are working with.
Sorry, I'm new to StackOverflow and still not quite sure of all the information I should be giving. An example output I get is: /Users/alexray/Library/Caches/clion11/cmake/generated/74a9f2b2/74a9f2b2/Debug/CrosswordSolver Enter the number of letters in the word 4 Enter a set of letters sla ails alas albs ales alls alms alps also awls gals labs lads lags lams laos laps lash lass last laws lays leas pals sail sale salt seal slab slag slam slap slat slav slaw slay libc++abi.dylib: terminating with uncaught exception of type std::length_error: vector Process finished with exit code 6
I edited my original post with a link to the .txt file I'm using for my program. My apologies for not including that originally.
0

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.

1 Comment

The problem was probably that you only checked that the file was open, not that it was "okay" (i.e. not past EOF and not bad). You should have used while (getline(wordList, example)). Give that a go (3½ years later lol)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.