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.
// 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;
}
The output I get when compiling is
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 ==========