Hello,
I wrote a hangman game but it won't output what the user entered. At the beginning the display array is filled with underscores and as the user enters letters it is supposed to get updated with their input, but it isn't updating. Can someone please tell me what I'm doing wrong? The problem seems to be around line 60.
Thanks in advance,
Benjamin. H
Note: This program doesn't seem to work in Visual Studio, so use Dev-C++ or Xcode to compile it instead.
#include <iostream>
#include <cstring>
#include <cctype>
#include <string>
#include <time.h>
using namespace std;
void ClearConsole();
void DisplayGame(string randomWord, string display[], int guessesLeft, bool winner);
bool CheckWinner(string randomWord, string display[], char wordChars[], bool winner, int guessesLeft, string lose, int correct, bool loser, string easterEgg);
int main()
{
srand ( time(NULL) );
const int LIMIT = 10;
string letterGuess;
int guessesLeft = 12;
string lose = "You lose! ";
string win = "You win! ";
string words[LIMIT];
int correct = 0;
bool loser = false;
string easterEgg = "hello";
words[0] = "hello";
words[1] = "boat";
words[3] = "mouse";
words[4] = "computer";
words[5] = "coffee";
words[6] = "pebkac";
words[7] = "oop";
words[8] = "programming";
words[9] = "sequel";
//Derive a random word from the array
int r = rand() % LIMIT;
string randomWord = words[r];
const int length1 = randomWord.length() + 1;
char wordChars[length1];
int TempNumOne = randomWord.size();
for (int a = 0; a <= TempNumOne; a++)
{
wordChars[a] = randomWord[a];
}
const int length2 = randomWord.length();
string display[length2];
for (int i = 0; i < randomWord.length(); i++) {
display[i] = "_";
}
//Play the game
bool winner = false;
loser = false;
while (winner == false) {
if (guessesLeft > 0) {
DisplayGame(randomWord, display, guessesLeft, winner);
}
cin >> letterGuess;
guessesLeft -= 1;
string wordChar;
for (int i = 0; i < randomWord.length(); i++) {
wordChar.assign(wordChars[i], 25);
if (wordChar == letterGuess) {
display[i] = letterGuess;
guessesLeft += 1;
ClearConsole();
}
}
ClearConsole();
if (guessesLeft < 1) {
loser = true;
}
if (loser == true) {
cout << lose;
cout << "The word was: " << randomWord;
}
winner = CheckWinner(randomWord, display, wordChars, winner, guessesLeft, lose, correct, loser, easterEgg);
if (winner == true) {
cout << win;
cin >> easterEgg;
if (easterEgg == "1304"){
cout << " _____ ____";
cout << " / / | o |";
cout << "| |/ ___/";
cout << "|_________/ ";
cout << "|_|_| |_|_| ";
}
}
}
return 0;
}
void ClearConsole() {
//Clear the console
system("cls");
}
void DisplayGame(string randomWord, string display[], int guessesLeft, bool winner) {
//Display the game
for (int i = 0; i < randomWord.length(); i++) {
cout << display[i] << " ";
}
cout << " Guesses left: " << guessesLeft;
cout << " ";
}
bool CheckWinner(string randomWord, string display[], char wordChars[], bool winner, int guessesLeft, string lose, int correct, bool loser, string easterEgg) {
string str;
//Check if the player has won or lost
winner = false;
for (int i = 0; i < randomWord.length(); i++) {
str = (1, wordChars[i]);
if (display[i] == str) {
correct += 1;
}
}
if (correct == randomWord.length()) {
winner = true;
}
return winner;
}