I need assistance with my incomplete hangman program.

I have to make a hangman program in my college class with starting code that was given to us. I have modified it a bit already but need to know how to replace underscores with the letters the user has guessed correctly. I am a beginner with c++ so it would help if the code that is given to me is not too complicated. I know stuff like arrays and all loops and statements etc but not many functions.
If you need the program script i am willing to send it to you. I am only looking for example code.

All help much appreciated :)
Last edited on
Post the code you have written so we can take a look at it. Don't PM it, just post it here
**And please use code tags**
I NEED HELP WIV ANOTHER THING INSTEAD! I need to make it so if the user guesses wrong it comes up with the attempted guesses. In the code you will see an array called attempts made and i have filled every element with a blank space. I have just not used the this array yet. It may be useful to u.
Here is the code.

#include <iostream>
#include <array>
#include <time.h>


using namespace std;


int main()
{
srand(time(0));


array <string, 20>listOfWords =
{
"phenomenal",
"insane",
"haunting",
"immaculate",
"duck",
"read",
"onomatopoeia",
"eloquent",
"ridiculous",
"jurassic",
"veni vidi vici",
"who you gonna call",
"ghostbusters",
"tremendous",
"variable",
"the game",
"blue",
"cuckoo",
"danny devito",
"oscar"
};


int randomWordNumber = rand() % 20;
string guessWord = listOfWords[randomWordNumber];


array <char, 20>guesses;
guesses.fill('_');


for (int count = 0; count < guessWord.length(); count++)
{
if (guessWord[count] == ' ')
{
guesses[count] = ' ';
}
}


for (int count = guessWord.length(); count < 20; count++)
{
guesses[count] = ' ';
}


bool running = true;
int lives = 5;


array <char, 20>lettersGuessed;
lettersGuessed.fill(' ');
char guess;





array <char, 20>attempts;
attempts.fill(' ');


/* USER INTERFACE GOES HERE. COMPLETE THIS SECTION */












int word = randomWordNumber; // TEMPORARY CODE DELETE AFTER TESTING!!!!
cout << word << endl;
cout << "Welcome to hangman!\nThe aim of the game is to guess letters in a word.\nEach underscore represents a letter!" << endl;
for (int x = 0; x <= guessWord.length(); x++)
{
cout << guesses[x] << " ";
}
cout << endl << "You have 5 lives to start off with!" << endl;



while (running == true)
{


cout << endl << endl;
cout << "Please enter your guess : ";
cin >> guess;
bool test = false;
for (int i = 0; i < guessWord.length(); i++)
{

if (guessWord[i] == guess)
{

guesses[i] = guess;

for (int x = 0; x <= guessWord.length(); x++)
{
cout << guesses[x] << " ";
}
cout << endl << endl << "Correct!" <<endl << endl;
cout<< "You have " << lives << " lives remaining!\n" << endl;
test = true;
}
}


static int counter = 0;


if (test == false)
{
lives--;
lettersGuessed[counter] = guess;
counter++;
cout << "\nWrong!" << " You have " << lives << " lives remaining!" << endl;
cout << "You have guessed the letters " << << " .";


}
else
{
lettersGuessed[counter] = guess;
counter++;

}

if (lives < 1)
{
cout << "YOU LOSE! The word was ";
for (int x = 0; x <= guessWord.length(); x++)
// I made the program print out what the word was.
{
cout << guessWord[x];
}
cout << endl;
cin.get();
running = false;
}


bool winFlag = true;


for (int count = 0; count < guessWord.length(); count++)
{
if (guesses[count] != guessWord[count])
{
winFlag = false;
}


}


if (winFlag == true)
{
cout << endl << endl << endl << "CONGRATULATIONS YOU WIN! The word was "; // I changed what was printed to make it more user friendly.

for (int x = 0; x <= guessWord.length(); x++)
// I made the program print out what the word was
{
cout << guessWord[x];
}
cout << endl;


cin.get();
running = false;
}
}
cin.get();


system("cls");



return 1;
}
It would also be nice if the code only shows up the underscores once with correct printed when there is more than one of the letter guessed. As at the moment say if there was 2 of the letter guessed in the word it would print out the underscores and the word correct 2 times.
Topic archived. No new replies allowed.