C++ bughunt - High-score insertion in a vector crashes the program
- by Francisco P.
Hello, everyone!
I have a game I'm working on. My players are stored in a vector, and, at the end of the game, the game crashes when trying to insert the high-scores in the correct positions.
Here's what I have (please ignore the portuguese comments, the code is pretty straightforward :P):
//TOTAL_HIGHSCORES is the max. number of hiscores that i'm willing to store. This is set as 10.
bool Game::updateHiScores()
{
bool stopIterating;
bool scoresChanged = false;
//Se ainda nao existirem TOTAL_HISCORES melhores pontuacoes ou se a pontuacao for melhor que uma das existentes
for (size_t i = 0; i < players.size(); ++i)
{
//&& !(players[i].isAI())
if (players[i].getScoreValue() > 0 && (hiScores.size() < TOTAL_HISCORES || hiScores.back() < players[i].getScore()))
{
scoresChanged = true;
if(hiScores.empty() || hiScores.back() >= players[i].getScore())
hiScores.push_back(players[i].getScore());
else
{
//Ciclo que encontra e insere a pontuacao no lugar desejado
stopIterating = false;
for(vector<Score>::iterator it = hiScores.begin(); it < hiScores.end() && !(stopIterating); ++it)
{
if(*it <= players[i].getScore())
{
//E inserida na posicao 'it' o Score correspondente
hiScores.insert(it, players[i].getScore());
//Verifica se o comprimento do vector esta dentro do desejado, se nao estiver, este e rectificado
if (hiScores.size() > TOTAL_HISCORES)
hiScores.pop_back();
stopIterating = true;
}
}
}
}
}
if (scoresChanged)
sort(hiScores.begin(), hiScores.end(), higher);
return scoresChanged;
}
What am I doing wrong here?
Thanks for your time, fellas.