// 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(); void head(); void headBody(); void headBodyLeftArm(); void headBodyRightArm(); void headBodyLeftLeg(); void dead(); int main() { string guess; string lettersUsed; int triesLeft; int bodyCount; int position; getString(); 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 - 1; } else { cout << "That letter is in the word!" << endl; triesLeft = bodyCount; } } } getchar(); return 0; } void getString() { 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; }
1>------ Build started: Project: Hangman, Configuration: Debug Win32 ------ 1>Compiling... 1>Hangman.cpp 1>c:\documents and settings\black_hatter\my documents\visual studio 2008\projects\hangman\hangman\hangman.cpp(53) : error C2065: 'hint' : undeclared identifier 1>c:\documents and settings\black_hatter\my documents\visual studio 2008\projects\hangman\hangman\hangman.cpp(57) : error C2065: 'choice' : undeclared identifier 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 1> type is ''unknown-type'' 1>Build log was saved at "file://c:\Documents and Settings\black_hatter\My Documents\Visual Studio 2008\Projects\Hangman\Hangman\Debug\BuildLog.htm" 1>Hangman - 3 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
GetString(string &choice, string &hint); 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.
1>------ Build started: Project: Hangman, Configuration: Debug Win32 ------ 1>Compiling... 1>Hangman.cpp 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 1> c:\program files\microsoft visual studio 9.0\vc\include\xstring(2210) : see declaration of 'std::string' 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 1> c:\program files\microsoft visual studio 9.0\vc\include\xstring(2210) : see declaration of 'std::string' 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' 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' 1>Build log was saved at "file://c:\Documents and Settings\black_hatter\My Documents\Visual Studio 2008\Projects\Hangman\Hangman\Debug\BuildLog.htm" 1>Hangman - 4 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
// 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; }
// 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; }
| DaniWeb Message | |
| Cancel Changes | |