In my program I have a loop with a variable that shows the players name, they choose something then it goes back to display their name and all it shows is arrows.... why? I'll post my code, run it and enter 3 or 4 players are playing, give them some names, then choose option 4: "Current world affairs.", then instead of the players name, it will just show these wierd arrows, why?

/**************************** 
 * ISU program (text risk)  *
 * created by Matthew       *
 * 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(), attackFunction(humanPlayer &), fortifyFunction(humanPlayer &);
void countryDivider(), renderBoard(), mainTurn(), worldAffairsFunction();
bool isCountryAttached(int countryToCheck, int listToCheck);
unsigned int numOfPlayers;
list<humanPlayer> jailHouse;
list<humanPlayer>::iterator iter;
vector<int> virginCountries;
string playerInitialList[20] = " ";
int countryPopulationArray[20] = {1};
int countryAttachedXX[20][6] = {
               {1,3,-1,-1,-1,-1},
               {0,2,3,4,-1,-1},
               {1,4,5,13,-1,-1},
               {0,1,4,6,-1,-1},
               {1,2,3,5,6,7},
               {2,4,7,-1,-1,-1}, 
               {3,4,7,8,-1,-1},
               {4,5,6,8,-1,-1},
               {6,7,9,-1,-1,-1},
               {8,10,11,-1,-1,-1},
               {9,11,12,-1,-1,-1},
               {9,10,12,-1,-1,-1},
               {10,11,-1,-1,-1,-1},
               {2,14,15,-1,-1,-1},
               {13,15,16,19,-1,-1},
               {13,14,16,17,-1,-1},
               {14,15,17,18,19,-1},
               {15,16,18,-1,-1,-1},
               {16,17,19,-1,-1,-1},
               {14,16,18,-1,-1,-1}};
int main()
     {
     cout << "Welcome to text risk, the rules are the same as the board game,\n" 
          << "minus risk cards, and an altered fortification rule set.  \n"
          << "First off ";
 
     getName();
 
     cout << endl << "The countries are now going to be randomly divided between the " 
          << numOfPlayers << " of you:" << endl;
     countryDivider();
 
     renderBoard();
     mainTurn();
 
     cout << "\n"; system("PAUSE");
     }
 
void mainTurn()
     {
     int initialTurnChoice = 0;
     while(true)
          {
          for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
               {
               bool turn = true;
               while(turn == true)
                    {
                    cout << endl << "Do you, " << (*iter).getPlayerName() << ", wish to: " << endl
                         << "(1) Attack someone." << endl
                         << "(2) Fortify somewhere." << endl
                         << "(3) Don't do anything to anyone." << endl
                         << "(4) Display current world affairs." << endl;
                    cin >> initialTurnChoice;
 
                    switch(initialTurnChoice)
                         {
                         case 1: attackFunction(*iter);
                                 break;
                         case 2: fortifyFunction(*iter);
                                 break;
                         case 3: turn = false;
                                 break;
                         case 4: worldAffairsFunction();
                                 break;
                         }
                    }
               }
          }
     }
 
int getName()
     {
     cout << "how many players will there be? ";
     cin >> numOfPlayers;
     cout << endl;
 
     while(true)
          {
          if(numOfPlayers < 2)
               {
               cout << endl << "You cannot play risk with less than two players, \n"
                    << "it's just not going to work.  So how many people are\n"
                    << "actually playing risk, total? ";
               cin >> numOfPlayers;
               }
          if(numOfPlayers > 8)
               {
               cout << endl << "Have you ever tried to play risk with more than 8 players?\n"
                    << "Cut a couple people and then say how many people are going to\n"
                    << "play risk, total: ";
               cin >> numOfPlayers;
               }
          if(numOfPlayers > 1)
               {
               if(numOfPlayers < 9)
                    {
                    break;
                    }
               }
          }
 
     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()
     {
     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));
               playerInitialList[virginCountries.at(0)] = (*iter).getPlayerInitial();
               virginCountries.erase(virginCountries.begin());
               counter += 1;
               if(counter == 20)
                    {
                    break;
                    }
               }
          }
 
     //test
     for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
          {
          cout << (*iter).getPlayerName() << ": ";
          (*iter).showList();
          }
     }
 
void renderBoard()
     {
     cout << endl
          << "         __     ____" << endl
          << "   N.A. |  \\   /    ." << endl
          << "         \\__|  \\ 2  |          ____" << endl
          << "  ____________  |" << playerInitialList[2] << " /  __  Eu  / |  |" << endl
          << " /   |  1  |  \\  \\|  /13\\ ___/  |  |" << endl
          << "/  0 |__" << playerInitialList[1] << "__|_/  |\\   ." << playerInitialList[13] << "_// 14  /   |" << endl
          << "|  " << playerInitialList[0] << " |  3 | 4|__|5\\      /  " << playerInitialList[14] << "  /    |" << endl
          << " \\/ \\|__" << playerInitialList[3] << "_|_" << playerInitialList[4] << "__|_" << playerInitialList[5] << "_\\ __ |_/|_/| 19 /" << endl
          << " /   |  6  | 7   /  /15\\ __/  | " << playerInitialList[19] << " /" << endl
          << "     |__" << playerInitialList[6] << "__|_" << playerInitialList[7] << "_/    ." << playerInitialList[15] << "_/ | 16 |  /" << endl
          << "        \\ 8  |          /  _" << playerInitialList[16] << " | |" << endl
          << "         \\ " << playerInitialList[8] << " \\         /\\ / \\/\\ |" << endl
          << "           \\ \\        /17|18/  ||" << endl
          << "            _\\_      |" << playerInitialList[17] << "_/ ." << playerInitialList[18] << "   \\|" << endl
          << "      S.A._/ 9 \\__" << endl
          << "         /___" << playerInitialList[9] << "____|" << endl
          << "         |   |    /" << endl
          << "          \\ 10|11/" << endl
          << "           |" << playerInitialList[10] << " |" << playerInitialList[11] << "/" << endl
          << "           |__|/" << endl
          << "           |12/" << endl
          << "           |" << playerInitialList[12] << "/" << endl
          << "           |/" << endl;
     } 
int attackFunction(humanPlayer & obj)
     {
     unsigned int countryToAttackFrom;
     unsigned int countryToAttack;
     cout << endl << obj.getPlayerName() << ", which country do you wish to attack from? ";
     obj.showList();
     cin >> countryToAttackFrom;
 
     if(obj.checkIfInList(countryToAttackFrom) == false)
          {
          cout << "You must attack from your own country.\n";
          return 0;
          } 
 
     if(countryPopulationArray[countryToAttackFrom] < 2)
          {
          cout << "You are unable to attack from that country, the army is too small.\n";
          return 0;
          }
 
     cout << "Now, which country do you wish to attack?  The only eligable countries to\n"
          << "attack are: ";
 
     }
int fortifyFunction(humanPlayer & obj)
     {
     }
void worldAffairsFunction()
     {
     cout << endl << "This is the current state of the game, player by player: \n";
     renderBoard();
     for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
          {
          cout << (*iter).getPlayerName() << " has " << (*iter).listSize() << " countries in their possesion."
               << "\nAnd those countries are as follows: ";
          (*iter).showList();
          }
     }
bool isCountryAttached(int countryToCheck, int listToCheck)               
     {
 
     }

Recommended Answers

All 10 Replies

Unexpected characters means you're probably accessing an array out of bounds. When running the program, print out the indexes whenever you access an array for printing. If the index isn't what you expected, that's the symptom of the bug and you can trace it back to where the index got the wrong value.

Is there any way I can make the name a constant? I'm not sure how because I can't initialize it with something.

>Is there any way I can make the name a constant?
Yes, qualify it with const in your class and use an initialization list in the constructor:

class humanPlayer {
  const std::string playerName;
public:
  humanPlayer ( const std::string& name )
    : playerName ( name )
  {}
};

I don't quite understand what you said to do there. Sorry

I can't say it any simpler. Make the name const and treat it as const. If you can't figure it out, you'll have to work around it not being const.

commented: Not needed, just don't comment if you can't help the poster in some way. +0

Yet another classic example of why globals should be best avoided. The culprit here is the iterator variable 'iter' which you chose to share among all your function calls resulting in side effects. The fact that the function mainTurn() along with all its sub functions like 'wordAffairs()' share the same variable causes the 'funny character printing syndrome'.

Create a local variable for each function call when global state is not explicitly required and you should be good to go.

Add a line list<humanPlayer>::iterator iter; at the start of mainTurn() function. Plus the logic for rotating turn looks fishy to me.

I can't say it any simpler. Make the name const and treat it as const. If you can't figure it out, you'll have to work around it not being const.

Just don't become a teacher okay? And I wasn't looking for simpler, I was loking for more expanded. You can explain anything too simple so no one understands. You ask me what a book is and I tell you it's a collection of words. That's probably the simplest I could explain it, but many people who don't know what a book is won't know what it is even after that. Thanks for your help though.

And oh man! Thank you S.O.S. this problem got me good. But where did it get the arrows from? Was the iterator getting incremented beyond the number of players?

> But where did it get the arrows from? Was the iterator getting
> incremented beyond the number of players?
Your iterator was in an invalid state and you try to access it. Simple.

> Just don't become a teacher OK?
You are being unnecessarily harsh here; she was trying to help you out. The negative reputation was so uncalled for. Such kind of attitude would make a contributing poster think twice before helping you out.

Hope you understand.

>Just don't become a teacher okay?
As you wish. From this point on, I'll refuse to help you ever again.

You are being unnecessarily harsh here; she was trying to help you out. The negative reputation was so uncalled for.

I do apologize, I took what she said as unnecessarily harsh. I actually got offendid by it, I really was trying to understand. But if she didn't mean anything by that, then I would like to officially apologize for what I said to her: I am sorry, you would make a fine teacher. But if she did mean it in a frustrated, offensive sense, then I don't think I did anything truly wrong(still sorry though), also, I agree, the rep thing was truly uncalled for and I'm going to find some good posts of hers (probably 3 or so of them, which shouldn't be hard: she does leave excellent posts) to leave good reputation on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.