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;
}

Recommended Answers

All 4 Replies

Lines 36, 37 and 44 and 45 -- I'm surprised this compiles. You're declaring an array of a variable size, but you are not using the "new" operator.

Line 61 -- where did the number 25 come from?
Line 63 -- You're assigning a character to equal a string?

I'm having a little trouble following what you're trying to do.

Lines 36, 27, 44 and 45: Would I have to use a pointer?
Line 61: I'm not quite sure.
Line 63: Whoops... I'll make letterGuess a char.

Here's my updated version (it doesn't compile if I remove the number from line 61):

#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 = new char[length1];
     
    int TempNumOne = randomWord.size();
    for (int a = 0; a <= TempNumOne; a++)
    {
    wordChars[a] = randomWord[a];
    }
    const int length2 = randomWord.length();
    string *display = new string[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], 100);
    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 = (wordChars[i]);
    if (display[i] == str) {
    correct += 1;
    }
    }
    if (correct == randomWord.length()) {
    winner = true;
    }
    return winner;
    }

The issue isn't whether it compiles (well, of course that's an issue), but what are you trying to accomplish with the command. You can change the syntax so that it compiles (i.e. putting a 100 in the line -- that begs the question. If 25 had no meaning, does 100?) Sure you can stick a number in there and make it compile, but do you know what the function does and what are you trying to accomplish with it?

Before anyone can help debug your code, you have to explain the overall methodology you are using and you have to understand it yourself. Then and only then can someone tell you whether your code is correct.

You seem to have some trouble switching back and forth between STL string objects and C style strings (null terminated char arrays). I'd use one or the other for this project.

You can use the [] opoerator on an STL string to acceass any givenn char therein. The STL library overloads the += operator for use with STL strings such that you can append one STL string to another or append one char to an STL string or append a C style string to the end of an STL string.

To my way of thinking you need the string to guess and the string representing underscores and visible char that have been guessed correctly so far and that's it in terms of strings to manipulate. If the two strings are the same, then the game is won. Using a loop to place an underscore in string of guesses for each char in the string to guess by appending an underscore to the string of guesses using the += operator each time through the loop should get the string of guesses in the default starting position of all underscores.

You may want a third string containing all the letters guessed so far, but that would be optional.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.