Outputting Issue

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 insead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#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;
}
Last edited on
Topic archived. No new replies allowed.