Перепутал srand () и vector :: iterator - PullRequest
0 голосов
/ 05 марта 2012

Я написал тренировочный код для «битвы», который позволяет вам выбирать количество бойцов, количество раундов и количество бросков кубиков на бойца за раунд, сохраняя результат в массиве трехмерных векторов.Запасная часть работает;однако функция printResult () не работает (я поставил // перед ней в main ()), и srand () тоже не работает.Полная программа ниже для удобства:

#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;

}

Ответы [ 2 ]

0 голосов
/ 05 марта 2012

У вас есть две проблемы, которые вы должны решить отдельно (и которые вы должны были решить отдельно, прежде чем объединить их в одну базу кода). Локи Астери уже решила проблему с srand().

Похоже, что round - это вектор Combatant, который является вектором чего-то другого, но посмотрите здесь:

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 голосов
/ 05 марта 2012

Вызывайте srand () только один раз в приложении.

int main()
{
    srand(time(0));
    // STUFF
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...