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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:23:23+00:00 2026-05-30T18:23:23+00:00

I have written a practice code for a ‘battle’ that allows you to select

  • 0

I have written a practice code for a ‘battle’ that allows you to select number of combatants, number of rounds and number of dice-rolls per fighter per round storing the result into a 3D vector array. The storage part is working; however, the printResult() function is botched (i have put a // before it in main() ) and the srand() isnt working either. The complete program is below for convenience:

#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <ctime>
#include <iterator>
#include <vector>
#include <algorithm>

using namespace std;

class Combat{
    private:
        int numberOfRounds;
        int numberOfCombatants;
        int numberOfRolls;
        int sidesDie;
        vector <int> rollz;
        vector <vector <int> >combatant;
        vector <vector <vector <int> > > round;
    public:
        void printMenu();
        void battle();
        void printResult();
        Combat(int, int , int, int );
        Combat(int );
        Combat();
        int roll();
        int roll(int die);
};
void Combat::printMenu()
{
    cout<<setw (10)<<"Welcome to the Combat Menu";
    cout<<'\n'<<setw (10)<<"Choose:";
    cout<<'\n'<<setw (10)<<"Number of combatants: ";
    cin>>numberOfCombatants;
    cout<<'\n'<<setw (10)<<"Die sides:";
    cin>>sidesDie;
    cout<<'\n'<<setw (10)<<"Enter number of rounds: ";
    cin>>numberOfRounds;
    cout<<setw(10)<<"Enter number of rolls (per combatant per round):";
    cin>>numberOfRolls;
}
Combat::Combat(){
    numberOfRounds=8;
}
Combat::Combat(int x){
    x=numberOfRounds;
}
Combat::Combat(int rnds,int cmb,int rll, int sides){
    numberOfRounds=rnds;
    numberOfCombatants=cmb;
    numberOfRolls=rll;
    sidesDie=sides;
}
int Combat::roll(int die)
{
    die=sidesDie;
    srand(time(0));
    int r=(1+rand()%die);
    return r;

}
int Combat::roll(){
    srand(time(0));
    int r=(1+rand()%6);
    return r;
  }
void Combat::battle(){
    cout<<setw(10)<<" Computing results of battle ...\n";
    int i,j,k;
    for (i=0;i<numberOfRounds;++i){
        cout<<"\nRound number "<<i+1;
        round.push_back(combatant);
        for(j=0;j<numberOfCombatants;++j){
            cout<<"\nCombatant number "<<j+1;
            combatant.push_back(rollz);
            cout<<endl;

            for(k=0;k<numberOfRolls;++k){
                rollz.push_back(roll(sidesDie));
                cout<<rollz.at(k)<<'\t';
            }
        }
        cout<<endl<<endl;
    }

    cout<<endl;
}

void Combat::printResult(){
    cout<<endl;
    vector< vector <vector<int> > >::const_iterator it1;
    int combt, counter=0;
    for (it1=round.begin();it1 !=round.end();++it1){
        ++counter;

        cout<<"\nRound number "<<counter<<endl;
        for(vector<vector<int> >::const_iterator it2=combatant.begin();it2!=combatant.end();++it2){
            ++combt;
            cout<<"\nCombatant "<<combt<<" hits ";
                  for(vector<int>::const_iterator it3=rollz.begin();it3!=rollz.end();++it3){
                cout<<*it3<<'\t';
            }
        }
    }
}

int main ()
{
    Combat fight;
    fight.printMenu();
    fight.battle();
    //fight.printResult();
    cout<<endl;

}
  • 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-30T18:23:24+00:00Added an answer on May 30, 2026 at 6:23 pm

    You have two problems you should tackle separately (and which you should have solved separately before you put them together into one code base). Loki Asteri has already addressed the problem with srand().

    It looks as if round is a vector of Combatant, which is a vector of something else, but look here:

    void Combat::printResult(){
        ...
        for (it1=round.begin();it1 !=round.end();++it1){
            ++counter;
            cout<<"\nRound number "<<counter<<endl;
            // You never use counter or it1 again.
            // You iterate over the same combatant for each it1:
            for(vector<vector<int> >::const_iterator it2=combatant.begin();
                it2!=combatant.end();++it2){
                ++combt;
                cout<<"\nCombatant "<<combt<<" hits "; // bad variable name at best
                // And now you iterate over the same rollz 
                // for every entry in combatant.
                for(vector<int>::const_iterator it3=rollz.begin();
                    it3!=rollz.end();++it3){
                    cout<<*it3<<'\t';
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some simple OO code I've written that I'm playing with: //define a
I'm refactoring some code and I have written a method that modifies a Dictionary
I have written an AIR Application that downloads videos and documents from a server.
I have written some code in my VB.NET application to send an HTML e-mail
I have written a DLL that uses MS Word to spell check the content
I have written something that uses the following includes: #include <math.h> #include <time.h> #include
I have written a watir script that downloads files. One of the files it
I have written a piece of code which writes either to console or to
I have a bit of Java code that outputs an XML file to a
I have written a MVP project where the View is a WinForm that implements

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.