vector subscript out of range
Mar 11, 2017 at 9:36pm UTC
Vector subscript out of range error.
any HINTS on why I'm getting this error?
updating this post for a new vector subscript error.
Been trying to debug this for an hour,
it seems that the nextLetter.clear(); is causing the error. But why???
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
std::string Wordpair::makeSentence(std::vector<std::pair<std::string, char >> completePair, int level)
{
std::srand(std::time(0));
std::string finishedSentence;
std::string endOfString;
std::vector<char > nextLetter;
do
{
nextLetter.clear();
if (endOfString.size() > level)
{
endOfString.erase(0, 1);
}
for (int i = 0; i < completePair.size(); i++)
{
if (endOfString == completePair[i].first)
{
nextLetter.push_back(completePair[i].second);
}
}
std::random_shuffle(nextLetter.begin(), nextLetter.end());
endOfString += nextLetter[0];
finishedSentence += nextLetter[0];
} while (nextLetter[0] != '.' );
return finishedSentence;
}
I believe I figured it out. It wasn't the clear(),
it was trying to shuffle an empty vector.
a few simple if statements and it was solved.
Last edited on Mar 12, 2017 at 1:52am UTC
Mar 11, 2017 at 9:54pm UTC
In lines 19-20 it's possible that nextLetter is still empty.
Topic archived. No new replies allowed.