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 7605663
In Process

The Archive Base Latest Questions

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

I have been working on creating an RPG combat sim for code-practice. A single

  • 0

I have been working on creating an RPG combat sim for code-practice. A single ‘battle’ constitutes a 3D vector based on number of rounds,number of fighter and number of Rolls (per fighter in each round) in that order. After hours of searching I have put together this code. I am aware there are easier ways to accomplish this (boost / matrix etc..) but I want to finish this and learn where my STL::vector manipulation is going wrong

#include <vector>
#include <algorithm>

using namespace std;

class Combat{
    private:
        int numberOfRounds;
        int numberOfCombatants;
        int numberOfRolls;
        int sidesDie;            
        vector <vector <vector <int> > > result;

    public:
        void printMenu();
        void battle();
        void printResult();
        int roll(int die);
};
int Combat::roll(int die)
{
    die=sidesDie;
    srand(time(0));
    int r=(1+rand()%die);
    return r;

void Combat::battle(){
    cout<<setw(10)<<" Computing results of battle ...\n";
    int i,j,k;
    for (i=0;i<numberOfRounds;++i){
        cout<<"Round number "<<i+1;
        for(j=0;j<numberOfCombatants;++j){
            for(k=0;k<numberOfRolls;++k){
                result[i][j].push_back(roll(sidesDie));

            }
            cout<<endl;
        }

        cout<<endl;

      }
}

The code above is supposed to create a 3D vector ‘result’ which should store dice-rolls inside cells per player per round. It crashes during execution without showing an error. i suspect the bug is in the way i am storing values inside the 3d vector

  • 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-31T00:13:29+00:00Added an answer on May 31, 2026 at 12:13 am

    You are correct – your result variable is a vector of vector of vector of ints. In your battle loop, you are attempting to push_back into the innermost vector, but the outer two vectors don’t have any contents defined yet.

    You’ll need to push_back an entry for i and j as well (of the appropriate subtype, e.g. vector < vector < int > >, and then vector < int >, to use the vector in this fashion.

    edit

    For instance:

    int i,j,k;
    vector<vector<int>> iType;
    vector<int> jType;
    for (i=0;i<numberOfRounds;++i)
    {
        cout<<"Round number "<<i+1;
        result.push_back( iType );
        for(j=0;j<numberOfCombatants;++j)
        {
            result[i].push_back( jType );
            for(k=0;k<numberOfRolls;++k)
            {
                result[i][j].push_back(roll(sidesDie));
    
            }
            cout<<endl;
        }
    
        cout<<endl;
    
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on some legacy C++ code that uses variable length structures
I have been working on creating a kpi that compares the rank of a
I have been working on a Android Web Services program that uses a number
I have been creating and working on different websites. Many a times a get
I have been working on creating an application that sends a string from an
I've been working on creating seo friendly urls for my site and have done
I have a website which I have been working on creating very rapidly, and
In the past years I have been working on different projects that required creating
I have been working on creating a e-commerce store using Magento and I think
I have been working on creating a PHP email form recently and cannot manage

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.