Beginners Hangman quick

Use IF, loops, switch, and string statements to create a basic hangman program.
You will have to:
1. Create a string variable for the secret word
2. Create a string variable for the answer
3. Create a char variable for the letter the user will guess
4. Ask the user to enter a letter
5. Place this in a loop so the user can enter another letter
6. Use Replace command to replace correct letter guessed with letters of secret word.
7. Stop the program and “hang” the user after 7 incorrect guesses or win
when the user guesses the correct word
I really do not know how to do this at all. Please help me quick.
Last edited on
never mind
Last edited on
create a basic hangman program

Basic? This forum is about C++
Basic, as in simple.
As in, they only need to print "hang" or "win" as the outcome.

No ASCII art, or other graphical depiction one normally associates with the game.

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
// basic hangman program
#include <iostream>
#include <string>
using namespace std;

int main()
{
    unsigned maxguess(0);
// 1. Create a string variable for the secret word
    string hidden("concealed");
// 2. Create a string variable for the answer
    string answer("*********");
// 3. Create a char variable for the letter the user will guess
    char guess;
// 4. Ask the user to enter a letter
/*  * * * * * I'll ask later within the loop * * * * *
**  cout << "Type a letter and hit Return or Enter at will... ";
**  cin >> guess;
*/
// 5. Place this in a loop so the user can enter another letter
    do
    {
        cout << "Uncovered: >>" << answer << "<< Your guess pls: "; /* alas same propmt also for run-in cycle */
        cin >> guess;
// 6. Use Replace command to replace correct letter guessed with letters of secret word.
/* do not know replace() */
        for(int i = 0; i < hidden.length(); i++)
        {
            if (guess == hidden[i])
                answer[i] = guess;
        }
// 7. Stop the program and “hang” the user after 7 incorrect guesses or win
    } while (++maxguess < 7);
    cout << "Game over: >>" << answer << "<<";
}

Uncovered: >>*********<< Your guess pls: a
Uncovered: >>*****a***<< Your guess pls: e
Uncovered: >>****ea*e*<< Your guess pls: o
Uncovered: >>*o**ea*e*<< Your guess pls: i
Uncovered: >>*o**ea*e*<< Your guess pls: n
Uncovered: >>*on*ea*e*<< Your guess pls: c
Uncovered: >>concea*e*<< Your guess pls: l
Game over: >>conceale*<< 
Exit code: 0 (normal program termination)

This is the beginners coner, so I've left out to display another prompt at run-in cycle. I used only the tutorials of this site and hidden.length() I could not find the reference any more. I hope this is a sufficiently basic.
Topic archived. No new replies allowed.