Hangman Help

Hi. Can you please help me with this problem? If you can do it as soon as possible. Thanks. :)

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
Please also output a simple graphic, using dashes and slashes while the user is being “hung”.
Funnily enough, @Offstar1029 seems to have been asking the same.

Post the code you have written so far - items 1 to 4 don't look too taxing.
@lastchance
#include <iostream>
#include <string>

using namespace std;
using::std::string;

int main()
{

string secretword = "happy";
string answer = " ";
char letter;
I'm basically really stupid and I'm a begginer, i don't knoe that much, that's why i asked for help. :) @lastchance
Make sure that your code will compile at every stage - so add the final brace } that closes main; also remove the line using::std::string.

So now do item 4 in your list:
- ask the user (hint, use cout)
- enter the letter (hint, use cin)

Then THINK about item 5:
- read about loops
- consider what you will do IN WORDS first; e.g play a game of hangman
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
#include <iostream>
#include <string>
it main()
{
    std::string secretWord = "pie", answer = "   ";
    char guess;
    bool guessCorrect = false, suceed = false;
    for (int tries = 0; tries != 7; ++tries)
    {
        int index = -1;
        std::cout << "Guess a letter: ";
        std::cin >> guess;
        if (secretWord == answer)
        {
            succeed = true;
            break;
        }
        for (auto c : secretWord)
        {
            ++index;
            if (guess == c)
            {
                guessCorrect = true;
                answer[index] = secretWord[index];
                break;
            }
        }
    }
    if (succeed)
    {
        std::cout << "You guess the word! - " << secretWord << std::endl;
    }
    else
    {
        std::cout << "The word was " << secretWord << "!" << std::endl;
        // hanging graphics . . .
    }
    return 0;
}
Last edited on
Topic archived. No new replies allowed.