Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 806279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:13:08+00:00 2026-05-15T00:13:08+00:00

I have a game I’m working on. My players are stored in a vector,

  • 0

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

    if (hiScores.empty())
        checkForValues = true;
    else
        checkForValues = (hiScores.size() < TOTAL_HISCORES || hiScores.back() < players[i].getScore());
    if (players[i].getScoreValue() > 0  && checkForValues)
        {
            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.

EDIT:

I ended up simplifying my code to this:

bool scoresChanged;

vector<Score> hiScoresCopy = hiScores;

for (size_t i = 0; i < TOTAL_PLAYERS; ++i)
    hiScoresCopy.push_back(players[i].getScore());

sort(hiScoresCopy.begin(), hiScoresCopy.end(), higher);

scoresChanged = (hiScores != hiScoresCopy);

if (scoresChanged)
{
    if (hiScoresCopy.size() > TOTAL_HISCORES)
        hiScoresCopy.resize(TOTAL_HISCORES);
    hiScores = hiScoresCopy;
}

return scoresChanged;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:13:09+00:00Added an answer on May 15, 2026 at 12:13 am

    You don’t say exactly where the code crashes or what the nature of the crash so I shall guess.

    This test is the wrong way around.

    it < hiScores.end() && !(stopIterating)
    

    In one common case your iterator it will be invalidated in the same clause that you set stopIterating to true.

    You could do this (!= is more idiomatic than < for iterators although both work for vectors).

    !stopIterating && it != hiScores.end()
    

    Alternatively, after your insertion you could just have break; statement and do away with the stopIterating variable. I think that this would be clearer for most readers of the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to code a global lookup table of sorts. I have game
I'm working on a Windows game, and I have this: bool game_cont; LRESULT WINAPI
I have a game I am working on that has homing missiles in it.
I have a game that I am working on in a C# console application,
i have a game that contain two viewcontroller, when the user end the game
I have a game I am working on and am storing the amount of
I have Game collection in my DB: var game = { players: [{username:user1, status:played},
I have a game that uses a shared preference to record the users high
I'm developing a game. I want to have game entities each have their own
Update The reason I couldn't update properly was I didn't have game's jar file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.