Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::string constructor throws std::out_of_range exception

Using VS 2012.

I was making hangman. Anyway, I had a function to get a std::string that was the same length as the current word being guessed, but filled with underscores. (as in, blanks).

The function:

std::string getBlankWord(std::vector<std::string> words, int currentWordIndex)
{
    return std::string(words[currentWordIndex], '_');
}

The line it is called on:

currentGuessingString = getBlankWord(words, index);

words is an std::vector, index is an int. words.size() = 22, and index = 0, so I don't know how calling, in this case, words[0] could be the culprit. Anyway, something on this line throws std::out_of_range exception, but I cannot find it.

Thanks.


1 Answers

I think what you really want here is more like:

return std::string(words[currentWordIndex].size(), '_');
like image 151
Jerry Coffin Avatar answered Apr 30 '26 12:04

Jerry Coffin