For the code posted below, a hangman game, how would I make it so after the conditions in the loop are evaluated, it goes back and asks for another letter input without an infinite loop?
// 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(choice, hint);
bodyCount=6;
triesLeft=bodyCount;
cout << "Enter a letter to guess or enter '!' for a hint." << endl;
cout << "Input letter guess: ";
cin >> guess;
while(triesLeft != 0)
{
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 lettersUsed;
cout << "*************************" << endl;
cout << "* Hangman *" << endl;
cout << "* v.1 *" << 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;
return;
}
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;
return;
}
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;
return;
}
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;
return;
}
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;
return;
}
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;
return;
}