I have been working on a game of hangman and have finished up with nearly all of the heavy coding. However, I am having an issue with testing for consecutive input. I would like to close the program if the user has entered something other than "yes" or "no" three times consecutively when prompted if they would like to play hangman. I have only managed to come up with a way that closes the program if the user has entered three total error inputs. Below is my code. Any help is much appreciated!
#include <iostream>
#include <string>
#include "MyFuncts.h"
#include "randword.h"
using namespace std;
void drawHangman(int incorrectCount);
int askToPlay();
int response = 0;
int incorrectCount = 0;
void gameSequence();
int main()
{
string reply = " ";
int consecutiveErrors = 0;
getWords("hangman.dat");
do
{
askToPlay();
if (response == PLAY)
{
gameSequence();
}
else if (response == STOP)
{
cout << "Goodbye\n";
break;
}
else
{
consecutiveErrors++;
cout << "ERROR\n";
}
} while (getNextWord() != "" && consecutiveErrors < 3);
system("pause");
}
void drawHangman(int incorrectCount)
{
if (incorrectCount == 0)
cout << " -------|\n"
" | |\n"
" |\n"
" |\n"
" |\n"
" |\n"
" -----\n\n";
else if (incorrectCount == 1)
cout << " -------|\n"
" | |\n"
" O |\n"
" |\n"
" |\n"
" |\n"
" -----\n\n";
else if (incorrectCount == 2)
cout << " -------|\n"
" | |\n"
" O |\n"
" | |\n"
" |\n"
" |\n"
" -----\n\n";
else if (incorrectCount == 3)
cout << " -------|\n"
" | |\n"
" O |\n"
"-| |\n"
" |\n"
" |\n"
" -----\n\n";
else if (incorrectCount == 4)
cout << " -------|\n"
" | |\n"
" O |\n"
"-|- |\n"
" |\n"
" |\n"
" -----\n\n";
else if (incorrectCount == 5)
cout << " -------|\n"
" | |\n"
" O |\n"
"-|- |\n"
"/ |\n"
" |\n"
" -----\n\n";
else
cout << " -------|\n"
" | |\n"
" O |\n"
"-|- |\n"
"/ \\ |\n"
" |\n"
" -----\n\n";
}
int askToPlay()
{
string reply = " ";
cout << "\nDo you want to play hangman? (y or n): ";
cin >> reply;
response = promptYN(reply);
return response;
}
void gameSequence()
{
string guessWord = " ";
char guessLetter = ' ';
cout << "Let's PLAY\n\n";
guessWord = strToUpper(getNextWord());
cout << "Word to Guess: " << guessWord << endl << endl;
while (incorrectCount < 6)
{
drawHangman(incorrectCount);
cout << "Enter a letter to guess: ";
cin >> guessLetter;
guessLetter = toupper(guessLetter);
cout << "You entered: " << guessLetter << endl << endl;
if (guessWord.find(guessLetter) != string::npos)
cout << guessLetter << " is in the word to guess.\n\n";
else
{
cout << guessLetter << " is NOT in the word to guess.\n\n";
incorrectCount++;
if (incorrectCount == 6)
{
drawHangman(incorrectCount);
cout << "Sorry you lose - the word was: " << guessWord << endl << endl;
}
}
}
incorrectCount = 0;
}
答案 0 :(得分:0)
Just reset consecutiveErrors
to 0
whenever response
equals PLAY
.
The if
statements in your while
loop would resemble the following:
if (response == PLAY) {
consecutiveErrors = 0; // add this line
gameSequence();
} else if (response == STOP) {
cout << "Goodbye\n";
break;
} else {
consecutiveErrors++;
cout << "ERROR\n";
}
答案 1 :(得分:0)
In your main loop you increment the consecutiveErrors
counter on every error, but you are not clearing it upon an acceptable response. Simply set consecutiveErrors
to 0 when ever you get an acceptable response.