| | |
How am I getting these errors?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
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.
The output I get when compiling is
C++ Syntax (Toggle Plain Text)
// 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
C++ Syntax (Toggle Plain Text)
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 ==========
Last edited by krichard89; Mar 18th, 2008 at 4:56 pm. Reason: messed up tags for compiler output
•
•
Join Date: Jan 2008
Posts: 34
Reputation:
Solved Threads: 4
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
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.
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
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!!!
CAMERON, FOR THE WIN!!!
WolfWorkz Studios - Server Development
WolfWorkz.com
Pirates Registration
My Personal Blog
SideBySideGeek.com - My Newest Project
WolfWorkz.com
Pirates Registration
My Personal Blog
SideBySideGeek.com - My Newest Project
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
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 ==========
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
// 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; }
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
[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]
// 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]
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
c++ Syntax (Toggle Plain Text)
// 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; }
![]() |
Similar Threads
- Simple Programming Errors (Computer Science)
- confused with errors in c++ (C++)
- Windows XP and Corel Suite 8 (WP8) errors (Windows NT / 2000 / XP)
- Loader.EXE and IEDLL.EXE errors (Web Browsers)
- Errors during scandisk (Windows 95 / 98 / Me)
- Upgrading Xp Home to Pro - ERRORS! (Windows NT / 2000 / XP)
- strange annoying errors (Windows NT / 2000 / XP)
- w2k server errors (Windows NT / 2000 / XP)
- Two Problems- "Page Cannot Be Displayed" Errors, Z (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: how to code ATM for banks
- Next Thread: Search textBox for words
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





