How am I getting these errors?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 7
Reputation: krichard89 is an unknown quantity at this point 
Solved Threads: 0
krichard89 krichard89 is offline Offline
Newbie Poster

How am I getting these errors?

 
0
  #1
Mar 18th, 2008
I am implementing code to program a hangman game in C++ for my CSP class. So far, I am working on the random string function as well as calling the body parts as void functions. I am attempting to compile what I have as of now to see how it runs, but I run into a problem. I declared some variables in a function, so how do I use them and their values in the main portion of the program? The code and errors are posted here. I am trying to call the values of choice and hint as determined in the getString function in the main portion of the program.

  1. // Kevin Richard
  2. // Hangman.cpp
  3. // Basic Hangman console game
  4.  
  5. /*
  6. Changelog:
  7. -added char guess, and string lettersUsed.
  8. -implemented help call.
  9. -added void subfunctions for the body parts of the hanged man.
  10. */
  11.  
  12.  
  13. #include "stdafx.h"
  14. #include "iostream"
  15. #include "string"
  16. #include "cmath"
  17. #include "ctime"
  18. #include "cstdlib"
  19. #include "conio.h"
  20.  
  21. using namespace std;
  22.  
  23. void getString();
  24. void head();
  25. void headBody();
  26. void headBodyLeftArm();
  27. void headBodyRightArm();
  28. void headBodyLeftLeg();
  29. void dead();
  30.  
  31. int main()
  32. {
  33. string guess;
  34. string lettersUsed;
  35. int triesLeft;
  36. int bodyCount;
  37. int position;
  38.  
  39. getString();
  40.  
  41.  
  42. bodyCount=6;
  43. triesLeft=bodyCount;
  44.  
  45. cout << "Welcome to Hangman! Guess the letters in the word to win." << endl;
  46.  
  47. while(triesLeft != 0)
  48. {
  49. cout << "Enter a letter to guess or enter '!' for a hint." << endl;
  50.  
  51. if (guess == "!") //May need its "" or == changed.
  52. {
  53. cout << hint << endl;
  54. }
  55. else
  56. {
  57. position = choice.find(guess);
  58. if(position = false)
  59. {
  60. cout << "Incorrect guess!" << endl;
  61. lettersUsed = lettersUsed + guess;
  62. triesLeft = bodyCount - 1;
  63. }
  64. else
  65. {
  66. cout << "That letter is in the word!" << endl;
  67. triesLeft = bodyCount;
  68. }
  69. }
  70. }
  71.  
  72. getchar();
  73.  
  74. return 0;
  75. }
  76. void getString()
  77. {
  78. string wordOne = "bases";
  79. string wordTwo = "catcher";
  80. string wordThree = "pitcher";
  81. string wordFour = "outfield";
  82. string wordFive = "infield";
  83. string wordSix = "bleachers";
  84. string wordSeven = "stadium";
  85. string wordEight = "peanuts";
  86. string wordNine = "batboy";
  87. string wordTen = "crowd";
  88. string choice;
  89. string lettersUsed;
  90. string hint;
  91.  
  92. cout << "*************************" << endl;
  93. cout << "* Hangman *" << endl;
  94. cout << "* v.1 *" << endl;
  95. cout << "*************************" << endl;
  96. cout << " " << endl;
  97.  
  98. /*
  99. Random number generator code found at: http://www.daniweb.com/forums/thread1769.html
  100. Posted by: Bob
  101. Daniweb IT Discussion Community Forums
  102. Thread: C++ Random Numbers
  103. */
  104.  
  105. srand((unsigned)time(0));
  106. int random_integer;
  107. int lowest=1, highest=10;
  108. int range=(highest-lowest)+1;
  109. for(int index=0; index<2; index++)
  110. {
  111. random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
  112. }
  113. /*
  114. End of C++ Random Number code as found on http://www.daniweb.com/forums/thread1769.html
  115. */
  116.  
  117. if(random_integer == 1)
  118. {
  119. choice = wordOne;
  120. hint = "A sucessful batter will run the _____.";
  121. }
  122. else if(random_integer == 2)
  123. {
  124. choice = wordTwo;
  125. hint = "If the batter missed, the _____ gets the ball.";
  126. }
  127. else if(random_integer == 3)
  128. {
  129. choice = wordThree;
  130. hint = "Throws the ball at the batter.";
  131. }
  132. else if(random_integer == 4)
  133. {
  134. choice = wordFour;
  135. hint = "Further away than the outfield.";
  136. }
  137. else if(random_integer == 5)
  138. {
  139. choice = wordFive;
  140. hint = "Closer than the outfield.";
  141. }
  142. else if(random_integer == 6)
  143. {
  144. choice = wordSix;
  145. hint = "The worst seats in the house.";
  146. }
  147. else if(random_integer == 7)
  148. {
  149. choice = wordSeven;
  150. hint = "Shea or Madison Square Garden are examples of one of these.";
  151. }
  152. else if(random_integer == 8)
  153. {
  154. choice = wordEight;
  155. hint = "Buy me some _____ and Cracker Jacks...";
  156. }
  157. else if(random_integer == 9)
  158. {
  159. choice = wordNine;
  160. hint = "Runs after the bats when the batter throws them.";
  161. }
  162. else if(random_integer == 10)
  163. {
  164. choice = wordTen;
  165. hint = "For the world series, the stadium always has a big _____.";
  166. }
  167. else if(random_integer > 10)
  168. {
  169. choice = wordFive;
  170. hint = "Closer than the outfield.";
  171. }
  172.  
  173. }
  174.  
  175. void head()
  176. {
  177. cout << " * * " << endl;
  178. cout << " * * " << endl;
  179. cout << " * x x * " << endl;
  180. cout << " * * " << endl;
  181. cout << " * * " << endl;
  182. cout << " * * " << endl;
  183. }
  184.  
  185. void headBody()
  186. {
  187. cout << " * * " << endl;
  188. cout << " * * " << endl;
  189. cout << " * x x * " << endl;
  190. cout << " * * " << endl;
  191. cout << " * * " << endl;
  192. cout << " * * " << endl;
  193. cout << " * * " << endl;
  194. cout << " * * " << endl;
  195. cout << " * * " << endl;
  196. cout << " * * " << endl;
  197. cout << " * * " << endl;
  198. cout << " * * " << endl;
  199. cout << " * * " << endl;
  200. cout << " * * " << endl;
  201. cout << " * * " << endl;
  202. cout << " * * " << endl;
  203. }
  204.  
  205. void headBodyLeftArm()
  206. {
  207. cout << " * * " << endl;
  208. cout << " * * " << endl;
  209. cout << " * x x * " << endl;
  210. cout << " * * " << endl;
  211. cout << "* * * " << endl;
  212. cout << " * * * " << endl;
  213. cout << " * * * " << endl;
  214. cout << " * * * " << endl;
  215. cout << " * * " << endl;
  216. cout << " * * " << endl;
  217. cout << " * * " << endl;
  218. cout << " * * " << endl;
  219. cout << " * * " << endl;
  220. cout << " * * " << endl;
  221. cout << " * * " << endl;
  222. cout << " * * " << endl;
  223. }
  224.  
  225. void headBodyRightArm()
  226. {
  227. cout << " * * " << endl;
  228. cout << " * * " << endl;
  229. cout << " * x x * " << endl;
  230. cout << " * * " << endl;
  231. cout << "* * * *" << endl;
  232. cout << " * * * * " << endl;
  233. cout << " * * * * " << endl;
  234. cout << " * * * * " << endl;
  235. cout << " * * " << endl;
  236. cout << " * * " << endl;
  237. cout << " * * " << endl;
  238. cout << " * * " << endl;
  239. cout << " * * " << endl;
  240. cout << " * * " << endl;
  241. cout << " * * " << endl;
  242. cout << " * * " << endl;
  243. }
  244.  
  245. void headBodyLeftLeg()
  246. {
  247. cout << " * * " << endl;
  248. cout << " * * " << endl;
  249. cout << " * x x * " << endl;
  250. cout << " * * " << endl;
  251. cout << "* * * *" << endl;
  252. cout << " * * * * " << endl;
  253. cout << " * * * * " << endl;
  254. cout << " * * * * " << endl;
  255. cout << " * * " << endl;
  256. cout << " * * " << endl;
  257. cout << " * * " << endl;
  258. cout << " * * " << endl;
  259. cout << " * * " << endl;
  260. cout << " * * " << endl;
  261. cout << " * * " << endl;
  262. cout << " * " << endl;
  263. cout << " * " << endl;
  264. cout << " * " << endl;
  265. cout << " * " << endl;
  266. cout << " * " << endl;
  267. cout << " * " << endl;
  268. cout << "* " << endl;
  269. }
  270.  
  271. void dead()
  272. {
  273. cout << " * * " << endl;
  274. cout << " * * " << endl;
  275. cout << " * x x * " << endl;
  276. cout << " * * " << endl;
  277. cout << "* * * *" << endl;
  278. cout << " * * * * " << endl;
  279. cout << " * * * * " << endl;
  280. cout << " * * * * " << endl;
  281. cout << " * * " << endl;
  282. cout << " * * " << endl;
  283. cout << " * * " << endl;
  284. cout << " * * " << endl;
  285. cout << " * * " << endl;
  286. cout << " * * " << endl;
  287. cout << " * * " << endl;
  288. cout << " * * " << endl;
  289. cout << " * * " << endl;
  290. cout << " * * " << endl;
  291. cout << " * * " << endl;
  292. cout << " * * " << endl;
  293. cout << " * * " << endl;
  294. cout << "* *" << endl;
  295. }

The output I get when compiling is

  1. 1>------ Build started: Project: Hangman, Configuration: Debug Win32 ------
  2. 1>Compiling...
  3. 1>Hangman.cpp
  4. 1>c:\documents and settings\black_hatter\my documents\visual studio 2008\projects\hangman\hangman\hangman.cpp(53) : error C2065: 'hint' : undeclared identifier
  5. 1>c:\documents and settings\black_hatter\my documents\visual studio 2008\projects\hangman\hangman\hangman.cpp(57) : error C2065: 'choice' : undeclared identifier
  6. 1>c:\documents and settings\black_hatter\my documents\visual studio 2008\projects\hangman\hangman\hangman.cpp(57) : error C2228: left of '.find' must have class/struct/union
  7. 1> type is ''unknown-type''
  8. 1>Build log was saved at "file://c:\Documents and Settings\black_hatter\My Documents\Visual Studio 2008\Projects\Hangman\Hangman\Debug\BuildLog.htm"
  9. 1>Hangman - 3 error(s), 0 warning(s)
  10. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited by krichard89; Mar 18th, 2008 at 4:56 pm. Reason: messed up tags for compiler output
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 34
Reputation: cbattagler is an unknown quantity at this point 
Solved Threads: 4
cbattagler cbattagler is offline Offline
Light Poster

Re: How am I getting these errors?

 
0
  #2
Mar 18th, 2008
You will want to declare those two variables in your main function. Then call your GetString() function with two arguments by reference. As in saying change the declaration of GetString to
GetString(string &choice, string &hint);
This will send the pointers to the function allowing them to manipulate the values directly and returning them to you without any problems what so ever.

Cameron

Edit: Also I would check out when you say how many times they have tried. From what I can tell, they will just be able to keep going because BodyCount never truly decreases try BodyCount-- instead.
Last edited by cbattagler; Mar 18th, 2008 at 5:59 pm. Reason: Saw another problem
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 101
Reputation: PirateTUX is an unknown quantity at this point 
Solved Threads: 3
PirateTUX's Avatar
PirateTUX PirateTUX is offline Offline
Junior Poster

Re: How am I getting these errors?

 
1
  #3
Mar 18th, 2008
That's a very good solution. If your teacher has not gone over pass-by-value vs. pass-by-reference, or global vs. local variables, now would be a good time to bring that up.

CAMERON, FOR THE WIN!!!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: krichard89 is an unknown quantity at this point 
Solved Threads: 0
krichard89 krichard89 is offline Offline
Newbie Poster

Re: How am I getting these errors?

 
0
  #4
Mar 18th, 2008
Originally Posted by cbattagler View Post
You will want to declare those two variables in your main function. Then call your GetString() function with two arguments by reference. As in saying change the declaration of GetString to
GetString(string &choice, string &hint);
This will send the pointers to the function allowing them to manipulate the values directly and returning them to you without any problems what so ever.

Cameron

Edit: Also I would check out when you say how many times they have tried. From what I can tell, they will just be able to keep going because BodyCount never truly decreases try BodyCount-- instead.
Now using that code, when I compile, I get these errors:
  1. 1>------ Build started: Project: Hangman, Configuration: Debug Win32 ------
  2. 1>Compiling...
  3. 1>Hangman.cpp
  4. 1>c:\documents and settings\black_hatter\my documents\visual studio 2008\projects\hangman\hangman\hangman.cpp(41) : error C2275: 'std::string' : illegal use of this type as an expression
  5. 1> c:\program files\microsoft visual studio 9.0\vc\include\xstring(2210) : see declaration of 'std::string'
  6. 1>c:\documents and settings\black_hatter\my documents\visual studio 2008\projects\hangman\hangman\hangman.cpp(41) : error C2275: 'std::string' : illegal use of this type as an expression
  7. 1> c:\program files\microsoft visual studio 9.0\vc\include\xstring(2210) : see declaration of 'std::string'
  8. 1>c:\documents and settings\black_hatter\my documents\visual studio 2008\projects\hangman\hangman\hangman.cpp(90) : error C2082: redefinition of formal parameter 'choice'
  9. 1>c:\documents and settings\black_hatter\my documents\visual studio 2008\projects\hangman\hangman\hangman.cpp(92) : error C2082: redefinition of formal parameter 'hint'
  10. 1>Build log was saved at "file://c:\Documents and Settings\black_hatter\My Documents\Visual Studio 2008\Projects\Hangman\Hangman\Debug\BuildLog.htm"
  11. 1>Hangman - 4 error(s), 0 warning(s)
  12. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 34
Reputation: cbattagler is an unknown quantity at this point 
Solved Threads: 4
cbattagler cbattagler is offline Offline
Light Poster

Re: How am I getting these errors?

 
0
  #5
Mar 18th, 2008
Paste your new code so I can compare where/what went on, thank you.

Cameron
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: krichard89 is an unknown quantity at this point 
Solved Threads: 0
krichard89 krichard89 is offline Offline
Newbie Poster

Re: How am I getting these errors?

 
0
  #6
Mar 18th, 2008
  1. // Kevin Richard
  2. // Hangman.cpp
  3. // Basic Hangman console game
  4.  
  5. /*
  6. Changelog:
  7. -added char guess, and string lettersUsed.
  8. -implemented help call.
  9. -added void subfunctions for the body parts of the hanged man.
  10. */
  11.  
  12.  
  13. #include "stdafx.h"
  14. #include "iostream"
  15. #include "string"
  16. #include "cmath"
  17. #include "ctime"
  18. #include "cstdlib"
  19. #include "conio.h"
  20.  
  21. using namespace std;
  22.  
  23. void getString(string, string);
  24. void head();
  25. void headBody();
  26. void headBodyLeftArm();
  27. void headBodyRightArm();
  28. void headBodyLeftLeg();
  29. void dead();
  30.  
  31. int main()
  32. {
  33. string guess;
  34. string lettersUsed;
  35. string choice;
  36. string hint;
  37. int triesLeft;
  38. int bodyCount;
  39. int position;
  40.  
  41. getString(string &choice, string &hint);
  42.  
  43.  
  44. bodyCount=6;
  45. triesLeft=bodyCount;
  46.  
  47. cout << "Welcome to Hangman! Guess the letters in the word to win." << endl;
  48.  
  49. while(triesLeft != 0)
  50. {
  51. cout << "Enter a letter to guess or enter '!' for a hint." << endl;
  52.  
  53. if (guess == "!") //May need its "" or == changed.
  54. {
  55. cout << hint << endl;
  56. }
  57. else
  58. {
  59. position = choice.find(guess);
  60. if(position = false)
  61. {
  62. cout << "Incorrect guess!" << endl;
  63. lettersUsed = lettersUsed + guess;
  64. triesLeft = bodyCount--;
  65. }
  66. else
  67. {
  68. cout << "That letter is in the word!" << endl;
  69. triesLeft = bodyCount;
  70. }
  71. }
  72. }
  73.  
  74. getchar();
  75.  
  76. return 0;
  77. }
  78. void getString(string choice, string hint)
  79. {
  80. string wordOne = "bases";
  81. string wordTwo = "catcher";
  82. string wordThree = "pitcher";
  83. string wordFour = "outfield";
  84. string wordFive = "infield";
  85. string wordSix = "bleachers";
  86. string wordSeven = "stadium";
  87. string wordEight = "peanuts";
  88. string wordNine = "batboy";
  89. string wordTen = "crowd";
  90. string choice;
  91. string lettersUsed;
  92. string hint;
  93.  
  94. cout << "*************************" << endl;
  95. cout << "* Hangman *" << endl;
  96. cout << "* v.1 *" << endl;
  97. cout << "*************************" << endl;
  98. cout << " " << endl;
  99.  
  100. /*
  101. Random number generator code found at: http://www.daniweb.com/forums/thread1769.html
  102. Posted by: Bob
  103. Daniweb IT Discussion Community Forums
  104. Thread: C++ Random Numbers
  105. */
  106.  
  107. srand((unsigned)time(0));
  108. int random_integer;
  109. int lowest=1, highest=10;
  110. int range=(highest-lowest)+1;
  111. for(int index=0; index<2; index++)
  112. {
  113. random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
  114. }
  115. /*
  116. End of C++ Random Number code as found on http://www.daniweb.com/forums/thread1769.html
  117. */
  118.  
  119. if(random_integer == 1)
  120. {
  121. choice = wordOne;
  122. hint = "A sucessful batter will run the _____.";
  123. }
  124. else if(random_integer == 2)
  125. {
  126. choice = wordTwo;
  127. hint = "If the batter missed, the _____ gets the ball.";
  128. }
  129. else if(random_integer == 3)
  130. {
  131. choice = wordThree;
  132. hint = "Throws the ball at the batter.";
  133. }
  134. else if(random_integer == 4)
  135. {
  136. choice = wordFour;
  137. hint = "Further away than the outfield.";
  138. }
  139. else if(random_integer == 5)
  140. {
  141. choice = wordFive;
  142. hint = "Closer than the outfield.";
  143. }
  144. else if(random_integer == 6)
  145. {
  146. choice = wordSix;
  147. hint = "The worst seats in the house.";
  148. }
  149. else if(random_integer == 7)
  150. {
  151. choice = wordSeven;
  152. hint = "Shea or Madison Square Garden are examples of one of these.";
  153. }
  154. else if(random_integer == 8)
  155. {
  156. choice = wordEight;
  157. hint = "Buy me some _____ and Cracker Jacks...";
  158. }
  159. else if(random_integer == 9)
  160. {
  161. choice = wordNine;
  162. hint = "Runs after the bats when the batter throws them.";
  163. }
  164. else if(random_integer == 10)
  165. {
  166. choice = wordTen;
  167. hint = "For the world series, the stadium always has a big _____.";
  168. }
  169. else if(random_integer > 10)
  170. {
  171. choice = wordFive;
  172. hint = "Closer than the outfield.";
  173. }
  174.  
  175. }
  176.  
  177. void head()
  178. {
  179. cout << " * * " << endl;
  180. cout << " * * " << endl;
  181. cout << " * x x * " << endl;
  182. cout << " * * " << endl;
  183. cout << " * * " << endl;
  184. cout << " * * " << endl;
  185. }
  186.  
  187. void headBody()
  188. {
  189. cout << " * * " << endl;
  190. cout << " * * " << endl;
  191. cout << " * x x * " << endl;
  192. cout << " * * " << endl;
  193. cout << " * * " << endl;
  194. cout << " * * " << endl;
  195. cout << " * * " << endl;
  196. cout << " * * " << endl;
  197. cout << " * * " << endl;
  198. cout << " * * " << endl;
  199. cout << " * * " << endl;
  200. cout << " * * " << endl;
  201. cout << " * * " << endl;
  202. cout << " * * " << endl;
  203. cout << " * * " << endl;
  204. cout << " * * " << endl;
  205. }
  206.  
  207. void headBodyLeftArm()
  208. {
  209. cout << " * * " << endl;
  210. cout << " * * " << endl;
  211. cout << " * x x * " << endl;
  212. cout << " * * " << endl;
  213. cout << "* * * " << endl;
  214. cout << " * * * " << endl;
  215. cout << " * * * " << endl;
  216. cout << " * * * " << endl;
  217. cout << " * * " << endl;
  218. cout << " * * " << endl;
  219. cout << " * * " << endl;
  220. cout << " * * " << endl;
  221. cout << " * * " << endl;
  222. cout << " * * " << endl;
  223. cout << " * * " << endl;
  224. cout << " * * " << endl;
  225. }
  226.  
  227. void headBodyRightArm()
  228. {
  229. cout << " * * " << endl;
  230. cout << " * * " << endl;
  231. cout << " * x x * " << endl;
  232. cout << " * * " << endl;
  233. cout << "* * * *" << endl;
  234. cout << " * * * * " << endl;
  235. cout << " * * * * " << endl;
  236. cout << " * * * * " << endl;
  237. cout << " * * " << endl;
  238. cout << " * * " << endl;
  239. cout << " * * " << endl;
  240. cout << " * * " << endl;
  241. cout << " * * " << endl;
  242. cout << " * * " << endl;
  243. cout << " * * " << endl;
  244. cout << " * * " << endl;
  245. }
  246.  
  247. void headBodyLeftLeg()
  248. {
  249. cout << " * * " << endl;
  250. cout << " * * " << endl;
  251. cout << " * x x * " << endl;
  252. cout << " * * " << endl;
  253. cout << "* * * *" << endl;
  254. cout << " * * * * " << endl;
  255. cout << " * * * * " << endl;
  256. cout << " * * * * " << endl;
  257. cout << " * * " << endl;
  258. cout << " * * " << endl;
  259. cout << " * * " << endl;
  260. cout << " * * " << endl;
  261. cout << " * * " << endl;
  262. cout << " * * " << endl;
  263. cout << " * * " << endl;
  264. cout << " * " << endl;
  265. cout << " * " << endl;
  266. cout << " * " << endl;
  267. cout << " * " << endl;
  268. cout << " * " << endl;
  269. cout << " * " << endl;
  270. cout << "* " << endl;
  271. }
  272.  
  273. void dead()
  274. {
  275. cout << " * * " << endl;
  276. cout << " * * " << endl;
  277. cout << " * x x * " << endl;
  278. cout << " * * " << endl;
  279. cout << "* * * *" << endl;
  280. cout << " * * * * " << endl;
  281. cout << " * * * * " << endl;
  282. cout << " * * * * " << endl;
  283. cout << " * * " << endl;
  284. cout << " * * " << endl;
  285. cout << " * * " << endl;
  286. cout << " * * " << endl;
  287. cout << " * * " << endl;
  288. cout << " * * " << endl;
  289. cout << " * * " << endl;
  290. cout << " * * " << endl;
  291. cout << " * * " << endl;
  292. cout << " * * " << endl;
  293. cout << " * * " << endl;
  294. cout << " * * " << endl;
  295. cout << " * * " << endl;
  296. cout << "* *" << endl;
  297. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: How am I getting these errors?

 
0
  #7
Mar 18th, 2008
please post it with (code = cplusplus) (/code) wrapped around the code please. it will help with identifying the line numbers that the errors are occuring at. (replace the round brackets with these [ ] )
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: krichard89 is an unknown quantity at this point 
Solved Threads: 0
krichard89 krichard89 is offline Offline
Newbie Poster

Re: How am I getting these errors?

 
0
  #8
Mar 18th, 2008
[code = cplusplus]
// Kevin Richard
// Hangman.cpp
// Basic Hangman console game

/*
Changelog:
-added char guess, and string lettersUsed.
-implemented help call.
-added void subfunctions for the body parts of the hanged man.
*/


#include "stdafx.h"
#include "iostream"
#include "string"
#include "cmath"
#include "ctime"
#include "cstdlib"
#include "conio.h"

using namespace std;

void getString(string, string);
void head();
void headBody();
void headBodyLeftArm();
void headBodyRightArm();
void headBodyLeftLeg();
void dead();

int main()
{
string guess;
string lettersUsed;
string choice;
string hint;
int triesLeft;
int bodyCount;
int position;

getString(string &choice, string &hint);


bodyCount=6;
triesLeft=bodyCount;

cout << "Welcome to Hangman! Guess the letters in the word to win." << endl;

while(triesLeft != 0)
{
cout << "Enter a letter to guess or enter '!' for a hint." << endl;

if (guess == "!") //May need its "" or == changed.
{
cout << hint << endl;
}
else
{
position = choice.find(guess);
if(position = false)
{
cout << "Incorrect guess!" << endl;
lettersUsed = lettersUsed + guess;
triesLeft = bodyCount--;
}
else
{
cout << "That letter is in the word!" << endl;
triesLeft = bodyCount;
}
}
}

getchar();

return 0;
}
void getString(string choice, string hint)
{
string wordOne = "bases";
string wordTwo = "catcher";
string wordThree = "pitcher";
string wordFour = "outfield";
string wordFive = "infield";
string wordSix = "bleachers";
string wordSeven = "stadium";
string wordEight = "peanuts";
string wordNine = "batboy";
string wordTen = "crowd";
string choice;
string lettersUsed;
string hint;

cout << "*************************" << endl;
cout << "* Hangman *" << endl;
cout << "* v.1 *" << endl;
cout << "*************************" << endl;
cout << " " << endl;

/*
Random number generator code found at: http://www.daniweb.com/forums/thread1769.html
Posted by: Bob
Daniweb IT Discussion Community Forums
Thread: C++ Random Numbers
*/

srand((unsigned)time(0));
int random_integer;
int lowest=1, highest=10;
int range=(highest-lowest)+1;
for(int index=0; index<2; index++)
{
random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
}
/*
End of C++ Random Number code as found on http://www.daniweb.com/forums/thread1769.html
*/

if(random_integer == 1)
{
choice = wordOne;
hint = "A sucessful batter will run the _____.";
}
else if(random_integer == 2)
{
choice = wordTwo;
hint = "If the batter missed, the _____ gets the ball.";
}
else if(random_integer == 3)
{
choice = wordThree;
hint = "Throws the ball at the batter.";
}
else if(random_integer == 4)
{
choice = wordFour;
hint = "Further away than the outfield.";
}
else if(random_integer == 5)
{
choice = wordFive;
hint = "Closer than the outfield.";
}
else if(random_integer == 6)
{
choice = wordSix;
hint = "The worst seats in the house.";
}
else if(random_integer == 7)
{
choice = wordSeven;
hint = "Shea or Madison Square Garden are examples of one of these.";
}
else if(random_integer == 8)
{
choice = wordEight;
hint = "Buy me some _____ and Cracker Jacks...";
}
else if(random_integer == 9)
{
choice = wordNine;
hint = "Runs after the bats when the batter throws them.";
}
else if(random_integer == 10)
{
choice = wordTen;
hint = "For the world series, the stadium always has a big _____.";
}
else if(random_integer > 10)
{
choice = wordFive;
hint = "Closer than the outfield.";
}

}

void head()
{
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * x x * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
}

void headBody()
{
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * x x * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
}

void headBodyLeftArm()
{
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * x x * " << endl;
cout << " * * " << endl;
cout << "* * * " << endl;
cout << " * * * " << endl;
cout << " * * * " << endl;
cout << " * * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
}

void headBodyRightArm()
{
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * x x * " << endl;
cout << " * * " << endl;
cout << "* * * *" << endl;
cout << " * * * * " << endl;
cout << " * * * * " << endl;
cout << " * * * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
}

void headBodyLeftLeg()
{
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * x x * " << endl;
cout << " * * " << endl;
cout << "* * * *" << endl;
cout << " * * * * " << endl;
cout << " * * * * " << endl;
cout << " * * * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * " << endl;
cout << " * " << endl;
cout << " * " << endl;
cout << " * " << endl;
cout << " * " << endl;
cout << " * " << endl;
cout << "* " << endl;
}

void dead()
{
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * x x * " << endl;
cout << " * * " << endl;
cout << "* * * *" << endl;
cout << " * * * * " << endl;
cout << " * * * * " << endl;
cout << " * * * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << "* *" << endl;
}
[/code]
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: How am I getting these errors?

 
0
  #9
Mar 18th, 2008
hmmm maybe try making that code=c++ instead...that didnt seem to wrap properly
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: krichard89 is an unknown quantity at this point 
Solved Threads: 0
krichard89 krichard89 is offline Offline
Newbie Poster

Re: How am I getting these errors?

 
0
  #10
Mar 18th, 2008
  1. // Kevin Richard
  2. // Hangman.cpp
  3. // Basic Hangman console game
  4.  
  5. /*
  6. Changelog:
  7. -added char guess, and string lettersUsed.
  8. -implemented help call.
  9. -added void subfunctions for the body parts of the hanged man.
  10. */
  11.  
  12.  
  13. #include "stdafx.h"
  14. #include "iostream"
  15. #include "string"
  16. #include "cmath"
  17. #include "ctime"
  18. #include "cstdlib"
  19. #include "conio.h"
  20.  
  21. using namespace std;
  22.  
  23. void getString(string, string);
  24. void head();
  25. void headBody();
  26. void headBodyLeftArm();
  27. void headBodyRightArm();
  28. void headBodyLeftLeg();
  29. void dead();
  30.  
  31. int main()
  32. {
  33. string guess;
  34. string lettersUsed;
  35. string choice;
  36. string hint;
  37. int triesLeft;
  38. int bodyCount;
  39. int position;
  40.  
  41. getString(string &choice, string &hint);
  42.  
  43.  
  44. bodyCount=6;
  45. triesLeft=bodyCount;
  46.  
  47. cout << "Welcome to Hangman! Guess the letters in the word to win." << endl;
  48.  
  49. while(triesLeft != 0)
  50. {
  51. cout << "Enter a letter to guess or enter '!' for a hint." << endl;
  52.  
  53. if (guess == "!") //May need its "" or == changed.
  54. {
  55. cout << hint << endl;
  56. }
  57. else
  58. {
  59. position = choice.find(guess);
  60. if(position = false)
  61. {
  62. cout << "Incorrect guess!" << endl;
  63. lettersUsed = lettersUsed + guess;
  64. triesLeft = bodyCount--;
  65. }
  66. else
  67. {
  68. cout << "That letter is in the word!" << endl;
  69. triesLeft = bodyCount;
  70. }
  71. }
  72. }
  73.  
  74. getchar();
  75.  
  76. return 0;
  77. }
  78. void getString(string choice, string hint)
  79. {
  80. string wordOne = "bases";
  81. string wordTwo = "catcher";
  82. string wordThree = "pitcher";
  83. string wordFour = "outfield";
  84. string wordFive = "infield";
  85. string wordSix = "bleachers";
  86. string wordSeven = "stadium";
  87. string wordEight = "peanuts";
  88. string wordNine = "batboy";
  89. string wordTen = "crowd";
  90. string choice;
  91. string lettersUsed;
  92. string hint;
  93.  
  94. cout << "*************************" << endl;
  95. cout << "* Hangman *" << endl;
  96. cout << "* v.1 *" << endl;
  97. cout << "*************************" << endl;
  98. cout << " " << endl;
  99.  
  100. /*
  101. Random number generator code found at: http://www.daniweb.com/forums/thread1769.html
  102. Posted by: Bob
  103. Daniweb IT Discussion Community Forums
  104. Thread: C++ Random Numbers
  105. */
  106.  
  107. srand((unsigned)time(0));
  108. int random_integer;
  109. int lowest=1, highest=10;
  110. int range=(highest-lowest)+1;
  111. for(int index=0; index<2; index++)
  112. {
  113. random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
  114. }
  115. /*
  116. End of C++ Random Number code as found on http://www.daniweb.com/forums/thread1769.html
  117. */
  118.  
  119. if(random_integer == 1)
  120. {
  121. choice = wordOne;
  122. hint = "A sucessful batter will run the _____.";
  123. }
  124. else if(random_integer == 2)
  125. {
  126. choice = wordTwo;
  127. hint = "If the batter missed, the _____ gets the ball.";
  128. }
  129. else if(random_integer == 3)
  130. {
  131. choice = wordThree;
  132. hint = "Throws the ball at the batter.";
  133. }
  134. else if(random_integer == 4)
  135. {
  136. choice = wordFour;
  137. hint = "Further away than the outfield.";
  138. }
  139. else if(random_integer == 5)
  140. {
  141. choice = wordFive;
  142. hint = "Closer than the outfield.";
  143. }
  144. else if(random_integer == 6)
  145. {
  146. choice = wordSix;
  147. hint = "The worst seats in the house.";
  148. }
  149. else if(random_integer == 7)
  150. {
  151. choice = wordSeven;
  152. hint = "Shea or Madison Square Garden are examples of one of these.";
  153. }
  154. else if(random_integer == 8)
  155. {
  156. choice = wordEight;
  157. hint = "Buy me some _____ and Cracker Jacks...";
  158. }
  159. else if(random_integer == 9)
  160. {
  161. choice = wordNine;
  162. hint = "Runs after the bats when the batter throws them.";
  163. }
  164. else if(random_integer == 10)
  165. {
  166. choice = wordTen;
  167. hint = "For the world series, the stadium always has a big _____.";
  168. }
  169. else if(random_integer > 10)
  170. {
  171. choice = wordFive;
  172. hint = "Closer than the outfield.";
  173. }
  174.  
  175. }
  176.  
  177. void head()
  178. {
  179. cout << " * * " << endl;
  180. cout << " * * " << endl;
  181. cout << " * x x * " << endl;
  182. cout << " * * " << endl;
  183. cout << " * * " << endl;
  184. cout << " * * " << endl;
  185. }
  186.  
  187. void headBody()
  188. {
  189. cout << " * * " << endl;
  190. cout << " * * " << endl;
  191. cout << " * x x * " << endl;
  192. cout << " * * " << endl;
  193. cout << " * * " << endl;
  194. cout << " * * " << endl;
  195. cout << " * * " << endl;
  196. cout << " * * " << endl;
  197. cout << " * * " << endl;
  198. cout << " * * " << endl;
  199. cout << " * * " << endl;
  200. cout << " * * " << endl;
  201. cout << " * * " << endl;
  202. cout << " * * " << endl;
  203. cout << " * * " << endl;
  204. cout << " * * " << endl;
  205. }
  206.  
  207. void headBodyLeftArm()
  208. {
  209. cout << " * * " << endl;
  210. cout << " * * " << endl;
  211. cout << " * x x * " << endl;
  212. cout << " * * " << endl;
  213. cout << "* * * " << endl;
  214. cout << " * * * " << endl;
  215. cout << " * * * " << endl;
  216. cout << " * * * " << endl;
  217. cout << " * * " << endl;
  218. cout << " * * " << endl;
  219. cout << " * * " << endl;
  220. cout << " * * " << endl;
  221. cout << " * * " << endl;
  222. cout << " * * " << endl;
  223. cout << " * * " << endl;
  224. cout << " * * " << endl;
  225. }
  226.  
  227. void headBodyRightArm()
  228. {
  229. cout << " * * " << endl;
  230. cout << " * * " << endl;
  231. cout << " * x x * " << endl;
  232. cout << " * * " << endl;
  233. cout << "* * * *" << endl;
  234. cout << " * * * * " << endl;
  235. cout << " * * * * " << endl;
  236. cout << " * * * * " << endl;
  237. cout << " * * " << endl;
  238. cout << " * * " << endl;
  239. cout << " * * " << endl;
  240. cout << " * * " << endl;
  241. cout << " * * " << endl;
  242. cout << " * * " << endl;
  243. cout << " * * " << endl;
  244. cout << " * * " << endl;
  245. }
  246.  
  247. void headBodyLeftLeg()
  248. {
  249. cout << " * * " << endl;
  250. cout << " * * " << endl;
  251. cout << " * x x * " << endl;
  252. cout << " * * " << endl;
  253. cout << "* * * *" << endl;
  254. cout << " * * * * " << endl;
  255. cout << " * * * * " << endl;
  256. cout << " * * * * " << endl;
  257. cout << " * * " << endl;
  258. cout << " * * " << endl;
  259. cout << " * * " << endl;
  260. cout << " * * " << endl;
  261. cout << " * * " << endl;
  262. cout << " * * " << endl;
  263. cout << " * * " << endl;
  264. cout << " * " << endl;
  265. cout << " * " << endl;
  266. cout << " * " << endl;
  267. cout << " * " << endl;
  268. cout << " * " << endl;
  269. cout << " * " << endl;
  270. cout << "* " << endl;
  271. }
  272.  
  273. void dead()
  274. {
  275. cout << " * * " << endl;
  276. cout << " * * " << endl;
  277. cout << " * x x * " << endl;
  278. cout << " * * " << endl;
  279. cout << "* * * *" << endl;
  280. cout << " * * * * " << endl;
  281. cout << " * * * * " << endl;
  282. cout << " * * * * " << endl;
  283. cout << " * * " << endl;
  284. cout << " * * " << endl;
  285. cout << " * * " << endl;
  286. cout << " * * " << endl;
  287. cout << " * * " << endl;
  288. cout << " * * " << endl;
  289. cout << " * * " << endl;
  290. cout << " * * " << endl;
  291. cout << " * * " << endl;
  292. cout << " * * " << endl;
  293. cout << " * * " << endl;
  294. cout << " * * " << endl;
  295. cout << " * * " << endl;
  296. cout << "* *" << endl;
  297. }
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC