Loop changes it's mind when running.

Thread Solved
Reply

Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Loop changes it's mind when running.

 
0
  #1
Jun 12th, 2007
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?

  1. /****************************
  2.  * ISU program (text risk) *
  3.  * created by Matthew *
  4.  * in grade 12 programming. *
  5.  ****************************/
  6.  
  7. #include <iostream>
  8. #include <list>
  9. #include <iterator>
  10. #include <vector>
  11. #include <algorithm>
  12. using namespace std;
  13. class humanPlayer
  14. {
  15. public:
  16. humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum);
  17. ~humanPlayer(){};
  18.  
  19. void addToList(unsigned int countryToAdd) {countriesOwned.push_back(countryToAdd);}
  20. void showList();
  21. int getSpecificListEntry(unsigned int memberSpot);
  22. int listSize() {return countriesOwned.size();}
  23. void removeFromList(unsigned int countryToRemove) {countriesOwned.remove(countryToRemove);}
  24. bool checkIfInList(unsigned int countryToCheck);
  25.  
  26. string getPlayerName() {return playerName;}
  27. string getPlayerInitial() {return playerInitial;}
  28.  
  29. int getPlayerTurnNum() {return playerTurnNum;}
  30. void setPlayerTurnNum(unsigned int newPlayerTurnNum);
  31.  
  32. private:
  33. list<int> countriesOwned;
  34. string playerName;
  35. string playerInitial;
  36. unsigned int playerTurnNum;
  37. };
  38. humanPlayer::humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum)
  39. {
  40. playerName = thePlayersName;
  41. playerTurnNum = thePlayersTurnNum;
  42. playerInitial = thePlayersName[0];
  43. }
  44.  
  45. bool humanPlayer::checkIfInList(unsigned int countryToCheck)
  46. {
  47. if(find(countriesOwned.begin(), countriesOwned.end(), countryToCheck) != countriesOwned.end())
  48. {
  49. return true;
  50. }
  51. else
  52. {
  53. return false;
  54. }
  55. }
  56.  
  57. void humanPlayer::showList()
  58. {
  59. copy(countriesOwned.begin(),countriesOwned.end(),ostream_iterator<int>(cout,", "));
  60. cout << endl;
  61. }
  62.  
  63. void humanPlayer::setPlayerTurnNum(unsigned int newPlayerTurnNum)
  64. {
  65. playerTurnNum = newPlayerTurnNum;
  66. }
  67.  
  68. int humanPlayer::getSpecificListEntry(unsigned int memberSpot)
  69. {
  70. list<int>::iterator iter;
  71. iter = countriesOwned.begin();
  72. (*iter) += memberSpot;
  73. return (*iter);
  74. }
  75.  
  76.  
  77. int getName(), attackFunction(humanPlayer &), fortifyFunction(humanPlayer &);
  78. void countryDivider(), renderBoard(), mainTurn(), worldAffairsFunction();
  79. bool isCountryAttached(int countryToCheck, int listToCheck);
  80. unsigned int numOfPlayers;
  81. list<humanPlayer> jailHouse;
  82. list<humanPlayer>::iterator iter;
  83. vector<int> virginCountries;
  84. string playerInitialList[20] = " ";
  85. int countryPopulationArray[20] = {1};
  86. int countryAttachedXX[20][6] = {
  87. {1,3,-1,-1,-1,-1},
  88. {0,2,3,4,-1,-1},
  89. {1,4,5,13,-1,-1},
  90. {0,1,4,6,-1,-1},
  91. {1,2,3,5,6,7},
  92. {2,4,7,-1,-1,-1},
  93. {3,4,7,8,-1,-1},
  94. {4,5,6,8,-1,-1},
  95. {6,7,9,-1,-1,-1},
  96. {8,10,11,-1,-1,-1},
  97. {9,11,12,-1,-1,-1},
  98. {9,10,12,-1,-1,-1},
  99. {10,11,-1,-1,-1,-1},
  100. {2,14,15,-1,-1,-1},
  101. {13,15,16,19,-1,-1},
  102. {13,14,16,17,-1,-1},
  103. {14,15,17,18,19,-1},
  104. {15,16,18,-1,-1,-1},
  105. {16,17,19,-1,-1,-1},
  106. {14,16,18,-1,-1,-1}};
  107. int main()
  108. {
  109. cout << "Welcome to text risk, the rules are the same as the board game,\n"
  110. << "minus risk cards, and an altered fortification rule set. \n"
  111. << "First off ";
  112.  
  113. getName();
  114.  
  115. cout << endl << "The countries are now going to be randomly divided between the "
  116. << numOfPlayers << " of you:" << endl;
  117. countryDivider();
  118.  
  119. renderBoard();
  120. mainTurn();
  121.  
  122. cout << "\n"; system("PAUSE");
  123. }
  124.  
  125. void mainTurn()
  126. {
  127. int initialTurnChoice = 0;
  128. while(true)
  129. {
  130. for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
  131. {
  132. bool turn = true;
  133. while(turn == true)
  134. {
  135. cout << endl << "Do you, " << (*iter).getPlayerName() << ", wish to: " << endl
  136. << "(1) Attack someone." << endl
  137. << "(2) Fortify somewhere." << endl
  138. << "(3) Don't do anything to anyone." << endl
  139. << "(4) Display current world affairs." << endl;
  140. cin >> initialTurnChoice;
  141.  
  142. switch(initialTurnChoice)
  143. {
  144. case 1: attackFunction(*iter);
  145. break;
  146. case 2: fortifyFunction(*iter);
  147. break;
  148. case 3: turn = false;
  149. break;
  150. case 4: worldAffairsFunction();
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. }
  157.  
  158. int getName()
  159. {
  160. cout << "how many players will there be? ";
  161. cin >> numOfPlayers;
  162. cout << endl;
  163.  
  164. while(true)
  165. {
  166. if(numOfPlayers < 2)
  167. {
  168. cout << endl << "You cannot play risk with less than two players, \n"
  169. << "it's just not going to work. So how many people are\n"
  170. << "actually playing risk, total? ";
  171. cin >> numOfPlayers;
  172. }
  173. if(numOfPlayers > 8)
  174. {
  175. cout << endl << "Have you ever tried to play risk with more than 8 players?\n"
  176. << "Cut a couple people and then say how many people are going to\n"
  177. << "play risk, total: ";
  178. cin >> numOfPlayers;
  179. }
  180. if(numOfPlayers > 1)
  181. {
  182. if(numOfPlayers < 9)
  183. {
  184. break;
  185. }
  186. }
  187. }
  188.  
  189. for(unsigned int i=0 ; i<numOfPlayers ; i++)
  190. {
  191. char name[256];
  192. cin.ignore(255,'\n');
  193. cout << "What is player " << (i+1) << "'s name? ";
  194. cin.get(name,256);
  195. jailHouse.push_back(humanPlayer(name,(i+1)));
  196. }
  197. }
  198.  
  199. void countryDivider()
  200. {
  201. unsigned int counter = 0;
  202.  
  203. for(unsigned int i=0 ; i<20 ; i++)
  204. {
  205. virginCountries.push_back(i);
  206. }
  207.  
  208. random_shuffle(virginCountries.begin(), virginCountries.end());
  209.  
  210. while(counter != 20)
  211. {
  212. for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
  213. {
  214. (*iter).addToList(virginCountries.at(0));
  215. playerInitialList[virginCountries.at(0)] = (*iter).getPlayerInitial();
  216. virginCountries.erase(virginCountries.begin());
  217. counter += 1;
  218. if(counter == 20)
  219. {
  220. break;
  221. }
  222. }
  223. }
  224.  
  225. //test
  226. for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
  227. {
  228. cout << (*iter).getPlayerName() << ": ";
  229. (*iter).showList();
  230. }
  231. }
  232.  
  233. void renderBoard()
  234. {
  235. cout << endl
  236. << " __ ____" << endl
  237. << " N.A. | \\ / ." << endl
  238. << " \\__| \\ 2 | ____" << endl
  239. << " ____________ |" << playerInitialList[2] << " / __ Eu / | |" << endl
  240. << " / | 1 | \\ \\| /13\\ ___/ | |" << endl
  241. << "/ 0 |__" << playerInitialList[1] << "__|_/ |\\ ." << playerInitialList[13] << "_// 14 / |" << endl
  242. << "| " << playerInitialList[0] << " | 3 | 4|__|5\\ / " << playerInitialList[14] << " / |" << endl
  243. << " [url="http://www.daniweb.com/techtalkforums/"]\\/[/url] \\|__" << playerInitialList[3] << "_|_" << playerInitialList[4] << "__|_" << playerInitialList[5] << "_\\ __ |_/|_/| 19 /" << endl
  244. << " / | 6 | 7 / /15\\ __/ | " << playerInitialList[19] << " /" << endl
  245. << " |__" << playerInitialList[6] << "__|_" << playerInitialList[7] << "_/ ." << playerInitialList[15] << "_/ | 16 | /" << endl
  246. << " \\ 8 | / _" << playerInitialList[16] << " | |" << endl
  247. << " \\ " << playerInitialList[8] << " \\ /\\ / [url="http://www.daniweb.com/techtalkforums/"]\\/\\[/url] |" << endl
  248. << " \\ \\ /17|18/ ||" << endl
  249. << " _\\_ |" << playerInitialList[17] << "_/ ." << playerInitialList[18] << " \\|" << endl
  250. << " S.A._/ 9 \\__" << endl
  251. << " /___" << playerInitialList[9] << "____|" << endl
  252. << " | | /" << endl
  253. << " \\ 10|11/" << endl
  254. << " |" << playerInitialList[10] << " |" << playerInitialList[11] << "/" << endl
  255. << " |__|/" << endl
  256. << " |12/" << endl
  257. << " |" << playerInitialList[12] << "/" << endl
  258. << " |/" << endl;
  259. }
  260. int attackFunction(humanPlayer & obj)
  261. {
  262. unsigned int countryToAttackFrom;
  263. unsigned int countryToAttack;
  264. cout << endl << obj.getPlayerName() << ", which country do you wish to attack from? ";
  265. obj.showList();
  266. cin >> countryToAttackFrom;
  267.  
  268. if(obj.checkIfInList(countryToAttackFrom) == false)
  269. {
  270. cout << "You must attack from your own country.\n";
  271. return 0;
  272. }
  273.  
  274. if(countryPopulationArray[countryToAttackFrom] < 2)
  275. {
  276. cout << "You are unable to attack from that country, the army is too small.\n";
  277. return 0;
  278. }
  279.  
  280. cout << "Now, which country do you wish to attack? The only eligable countries to\n"
  281. << "attack are: ";
  282.  
  283. }
  284. int fortifyFunction(humanPlayer & obj)
  285. {
  286. }
  287. void worldAffairsFunction()
  288. {
  289. cout << endl << "This is the current state of the game, player by player: \n";
  290. renderBoard();
  291. for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
  292. {
  293. cout << (*iter).getPlayerName() << " has " << (*iter).listSize() << " countries in their possesion."
  294. << "\nAnd those countries are as follows: ";
  295. (*iter).showList();
  296. }
  297. }
  298. bool isCountryAttached(int countryToCheck, int listToCheck)
  299. {
  300.  
  301. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Loop changes it's mind when running.

 
0
  #2
Jun 12th, 2007
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: Loop changes it's mind when running.

 
0
  #3
Jun 12th, 2007
Is there any way I can make the name a constant? I'm not sure how because I can't initialize it with something.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Loop changes it's mind when running.

 
0
  #4
Jun 12th, 2007
>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:
  1. class humanPlayer {
  2. const std::string playerName;
  3. public:
  4. humanPlayer ( const std::string& name )
  5. : playerName ( name )
  6. {}
  7. };
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: Loop changes it's mind when running.

 
0
  #5
Jun 12th, 2007
I don't quite understand what you said to do there. Sorry
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Loop changes it's mind when running.

 
0
  #6
Jun 12th, 2007
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Loop changes it's mind when running.

 
0
  #7
Jun 12th, 2007
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.
Last edited by ~s.o.s~; Jun 12th, 2007 at 3:01 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: Loop changes it's mind when running.

 
0
  #8
Jun 12th, 2007
Originally Posted by Narue View Post
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?
Last edited by Matt Tacular; Jun 12th, 2007 at 5:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Loop changes it's mind when running.

 
0
  #9
Jun 12th, 2007
> 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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Loop changes it's mind when running.

 
0
  #10
Jun 13th, 2007
>Just don't become a teacher okay?
As you wish. From this point on, I'll refuse to help you ever again.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC