0

I'm having some trouble with STL lists. What I want to do in my program is take the players initial and put it into an array at the spot that they got from when I divided up 20 numbers (0-19). Can anyone tell me why line 151 (in this post line 146)does what it does? It's showing some numbers that aren't supposed to be there. Also I'm trying to get line 150(in this post line 145) (currently commented out) to do the whole players initial thing, can anyone help with that? Anyways, here's the code:

/* ISU program created by Matt
   in grade 12 programming. */
#include <iostream>
#include <list>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;
class humanPlayer
     {
     public:                                                             
          humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum); 
          ~humanPlayer(){};  
 
          void addToList(unsigned int countryToAdd) {countriesOwned.push_back(countryToAdd);} 
          void showList();
          int getSpecificListEntry(unsigned int memberSpot); 
          int listSize() {return countriesOwned.size();}
          void removeFromList(unsigned int countryToRemove) {countriesOwned.remove(countryToRemove);}
          bool checkIfInList(unsigned int countryToCheck);
 
          string getPlayerName() {return playerName;}  
          string getPlayerInitial() {return playerInitial;}
 
          int getPlayerTurnNum() {return playerTurnNum;}
          void setPlayerTurnNum(unsigned int newPlayerTurnNum);
 
     private:
          list<int> countriesOwned;
          string playerName;
          string playerInitial;
          unsigned int playerTurnNum;
     };
     humanPlayer::humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum)
          {
          playerName = thePlayersName;
          playerTurnNum = thePlayersTurnNum;
          playerInitial = thePlayersName[0];
          }
 
     bool humanPlayer::checkIfInList(unsigned int countryToCheck)
          {
          if(find(countriesOwned.begin(), countriesOwned.end(), countryToCheck) != countriesOwned.end())
               {
               return true;
               }
          else
               {
               return false;
               }
          }
 
     void humanPlayer::showList()
          {
          copy(countriesOwned.begin(),countriesOwned.end(),ostream_iterator<int>(cout,", "));
          cout << endl;
          } 
 
     void humanPlayer::setPlayerTurnNum(unsigned int newPlayerTurnNum)
          {
          playerTurnNum = newPlayerTurnNum;
          }
 
     int humanPlayer::getSpecificListEntry(unsigned int memberSpot)
          {
          list<int>::iterator iter;
          iter = countriesOwned.begin();
          (*iter) += memberSpot;
          return (*iter);
          }  
 
 
int getName();
void countryDivider(), renderBoard();
unsigned int numOfPlayers;
list<humanPlayer> jailHouse;
vector<int> virginCountries;
string playersInitialList[21] = " ";
 
int main()
     {
     getName();
     countryDivider();
     renderBoard();
     cout << "\n"; system("PAUSE");
     }  
 
int getName()
     {
     cout << "How many players will there be? ";
     cin >> numOfPlayers;
     cout << endl;
 
     for(unsigned int i=0 ; i<numOfPlayers ; i++)
          {
          char name[256];
          cin.ignore(255,'\n');
          cout << "What is player " << (i+1) << "'s name? ";
          cin.get(name,256);
          jailHouse.push_back(humanPlayer(name,(i+1)));
          } 
     }   
 
void countryDivider()
     {
     list<humanPlayer>::iterator iter;
     unsigned int counter = 0;
 
     for(unsigned int i=0 ; i<20 ; i++)
          {
          virginCountries.push_back(i);
          }
 
     random_shuffle(virginCountries.begin(), virginCountries.end()); 
 
     while(counter != 20)
          {
          for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
               {
               (*iter).addToList(virginCountries.at(0));
               virginCountries.erase(virginCountries.begin());
               counter += 1;
               if(counter == 20)
                    {
                    break;
                    }
               }
          }
 
     //test
     for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
          {
          (*iter).showList();
          }
     }
 
void renderBoard()
     {
     list<humanPlayer>::iterator iter;
 
     for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
          {
          for(unsigned int i=0 ; i<(*iter).listSize() ; i++)
               {
               //playersInitialList[(*iter).getSpecificListEntry(i)] = (*iter).getPlayerInitial();
               cout << (*iter).getSpecificListEntry(i) << " - ";
               }
          }
 
 
     for(unsigned int i=0 ; i<21 ; i++)
          {
          cout << playersInitialList[i] << ", ";
          }
     }
1
Contributor
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Matt Tacular
0

Scratch that, I was going WAY over complicated, I revised the part where the countries are divided and it works now, just in case you're wondering, this is what I was trying to do:

/* ISU program created by Matt
   in grade 12 programming. */
#include <iostream>
#include <list>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;
class humanPlayer
     {
     public:                                                             
          humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum); 
          ~humanPlayer(){};  
 
          void addToList(unsigned int countryToAdd) {countriesOwned.push_back(countryToAdd);} 
          void showList();
          int getSpecificListEntry(unsigned int memberSpot); 
          int listSize() {return countriesOwned.size();}
          void removeFromList(unsigned int countryToRemove) {countriesOwned.remove(countryToRemove);}
          bool checkIfInList(unsigned int countryToCheck);
 
          string getPlayerName() {return playerName;}  
          string getPlayerInitial() {return playerInitial;}
 
          int getPlayerTurnNum() {return playerTurnNum;}
          void setPlayerTurnNum(unsigned int newPlayerTurnNum);
 
     private:
          list<int> countriesOwned;
          string playerName;
          string playerInitial;
          unsigned int playerTurnNum;
     };
     humanPlayer::humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum)
          {
          playerName = thePlayersName;
          playerTurnNum = thePlayersTurnNum;
          playerInitial = thePlayersName[0];
          }
 
     bool humanPlayer::checkIfInList(unsigned int countryToCheck)
          {
          if(find(countriesOwned.begin(), countriesOwned.end(), countryToCheck) != countriesOwned.end())
               {
               return true;
               }
          else
               {
               return false;
               }
          }
 
     void humanPlayer::showList()
          {
          copy(countriesOwned.begin(),countriesOwned.end(),ostream_iterator<int>(cout,", "));
          cout << endl;
          } 
 
     void humanPlayer::setPlayerTurnNum(unsigned int newPlayerTurnNum)
          {
          playerTurnNum = newPlayerTurnNum;
          }
 
     int humanPlayer::getSpecificListEntry(unsigned int memberSpot)
          {
          list<int>::iterator iter;
          iter = countriesOwned.begin();
          (*iter) += memberSpot;
          return (*iter);
          }  
 
 
int getName();
void countryDivider(), renderBoard();
unsigned int numOfPlayers;
list<humanPlayer> jailHouse;
vector<int> virginCountries;
string playersInitialList[20] = " ";
 
int main()
     {
     getName();
     countryDivider();
     renderBoard();
     cout << "\n"; system("PAUSE");
     }  
 
int getName()
     {
     cout << "How many players will there be? ";
     cin >> numOfPlayers;
     cout << endl;
 
     for(unsigned int i=0 ; i<numOfPlayers ; i++)
          {
          char name[256];
          cin.ignore(255,'\n');
          cout << "What is player " << (i+1) << "'s name? ";
          cin.get(name,256);
          jailHouse.push_back(humanPlayer(name,(i+1)));
          } 
     }   
 
void countryDivider()
     {
     list<humanPlayer>::iterator iter;
     unsigned int counter = 0;
 
     for(unsigned int i=0 ; i<20 ; i++)
          {
          virginCountries.push_back(i);
          }
 
     random_shuffle(virginCountries.begin(), virginCountries.end()); 
 
     while(counter != 20)
          {
          for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
               {
               (*iter).addToList(virginCountries.at(0));
               playersInitialList[virginCountries.at(0)] = (*iter).getPlayerInitial();
               virginCountries.erase(virginCountries.begin());
               counter += 1;
               if(counter == 20)
                    {
                    break;
                    }
               }
          }
 
     //test
     for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
          {
          (*iter).showList();
          }
     }
 
void renderBoard()
     {
     for(unsigned int i=0 ; i<21 ; i++)
          {
          cout << playersInitialList[i] << ", ";
          }
 
     }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.