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

  • Home
  • SEARCH
  • 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 7494709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:43:58+00:00 2026-05-29T17:43:58+00:00

I have a raffle C++ program that I use to draw out of a

  • 0

I have a “raffle” C++ program that I use to “draw out of a hat”. I receive an EXC_BAD_ACCESS signal when I try to use it, though. Here is the function:

vector<int> speedRaffle(vector<Player>players,int pNum){
    vector<int> spdtics,order;
    int ticnum,randy;
    vector<int>::iterator iter = spdtics.begin();
    for (int k=0;k<pNum;k++){
        for (int i=0; i<pNum; i++) {
            for (int j=0; j<pow(players[i].speed,2); j++){
                for (int io=0; io<order.size(); io++) {
                    if(order[io]!=i){
                        spdtics.push_back(i);
                        ticnum++;
                    }
                }
            }
        }
        randy=random() % ticnum;
        for(int i=0;i<randy;i++){
            iter++;

        }
        order[k]=*iter; //Thread 1: Program received signal: "EXC_BAD_ACCESS". 
        iter=spdtics.begin();

    }
return order;
}

This function should take all of the players’ speeds and square them. Then, it puts that many (the squares of speeds) “raffle tickets” into spdtics. It then randomly draws one “ticket” from spdtics, and puts the number of the player who owned the ticket into order. Then, it repeats again until all players have been drawn, not drawing the same player twice. It returns the order in which the players won.

The class Player contains an int speed. I call this function like this:

order=speedRaffle(players,pNum);

where players is vector and pNum is int. What am I doing wrong?

  • 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-29T17:43:59+00:00Added an answer on May 29, 2026 at 5:43 pm

    1. You are trying to access element at index k in empty vector order

    It crashes because vector order is empty when you call order[k] = *iter;, you should use push_back function instead: order.push_back(*iter);.

    2. You use loop for “moving” iterator instead of simple advance call

    advance(iter, randy - 1); has same effect as this loop: for(int i=0;i<randy;i++){ iter++; }.

    3. You call pow in every single iteration

    for (int j=0; j<pow(players[i].speed,2); j++)
    

    Note, that this would be much faster:

    int maxspeed = pow(players[i].speed,2);
    for (int j = 0; j < maxspeed; j++)
    

    4. Elements in vector can be accessed directly by using index
    You don’t need any iterator at all in this case.

    5. Passing vector by value instead of passing it by reference

    vector<int> speedRaffle(vector<Player>players,int pNum)
    

    Note, that copy of vector players is created every time you call this function. You don’t want to do that. You also don’t want to change this vector inside of function, so declaring this argument as const would be much better:

    vector<int> speedRaffle(const vector<Player>& players, int pNum)
    

    6. Your code does not do what you need it to do

    “It should take all of the players’ speeds and square them. Then, it puts that many (the squares of speeds) “raffle tickets” into spdtics. It then randomly draws one “ticket” from spdtics, and puts the number of the player who owned the ticket into order. Then, it repeats again until all players have been drawn, not drawing the same player twice. It returns the order in which the players won.”

    According to this, your function should look like this:

    vector<int> speedRaffle(vector<Player>& players)
    {
        // create vector of tickets:
        set<int> ticketOwners;
        vector<int> spdtics;
        for (int i = 0; i < players.size(); i++)
        {
            ticketOwners.insert(i);
            int maxspeed = pow(players[i].speed,2);
            for (int j = 0; j < maxspeed; j++)
            {
                spdtics.push_back(i);
            }
        }
        // draw ticket for every player:
        vector<int> order;
        while (!ticketOwners.empty())
        {
            set<int>::const_iterator to;
            int randy;
            do
            {
                randy = random() % spdtics.size();
                to = ticketOwners.find(spdtics[randy]);
            }
            while (to == ticketOwners.end());
            spdtics.erase(spdtics.begin() + randy);
            order.push_back(*to);
            ticketOwners.erase(to);
        }
        return order;
    }
    

    Also note that you don’t need pNum argument if it’s equal to players.size().

    Hope this will help you.

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

Sidebar

Related Questions

I have a raffle draw program that used to have many commands and components,
Have an app that can use tts to read text messages. It can also
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =
Have someone tried out DeCAL in Delphi 2009? I'm thinking about upgrading from 2007,
Have a matrix report now that has Position, Hours and Wages for a location
Have you tried to use SharePoint with version control such as Perforce (or Subversion),
Have a bunch of WCF REST services hosted on Azure that access a SQL
Good Morning Stackoverflow-ka-teers, I am creating a raffle form that allows you to enter
have Googled the cr*p out of this one so apologies if the answer is
have a php code like this,going to convert it in to C#. function isValid($n){

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.