C++ Hangman Program using Loops.

Let me preface this by saying this is NOT a current homework assignment; but it is an assignment from 3 years ago before I dropped out of school. I am self teaching and am revisiting an assignment I recently rediscovered. I am NOT asking for the entire program, I'm simply looking for help building the skeleton for the initial start of the game.
MORE INFO:
Player 1 will enter word(of any length / i have been using "Testing") for Player 2 to guess. Player 2 will have 5 letter guesses, and 5 word guesses. If Player 2 enters "Testing" it should be able to ignore the case between upper/lower (WITHOUT using toupper / tolower)
IF: Player 2 enters more than 1 letter for a guess: "aa" make them guess again until they only guess 1 letter "a".

My problem is setting this all up. I feel I'm mixing things up and having a hard time keeping everything in order, and missing certain functions. My code will be posted below. I can elaborate further if necessary.




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
 #include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    string word, wordguess, lower, dashes;
    string letterguess;
    int i = 0;
    bool GameOver = false, Validletterguess = true, Validwordguess = true;    


    cout << "Player 1, enter a word for Player 2 to guess: " << endl;
    getline(cin, word);
    cout << endl;
    cout << "Player 2, you have 5 letter guesses, and 5 word guesses." << endl;
    cout << "Guess your first letter: " << endl; 

    while (GameOver == false) // Start of Game. Setup for Round 1 letter guess and word guess.
    {      
        while (letterguess.length() != 1)
        {
            cout << endl << "Type a single letter and press <enter>: ";
            cin >> letterguess;               

            for (int i = 0; i < letterguess.length(); i++) 
            {
                if (letterguess.at(i) >= 'A' && letterguess.at(i) <= 'Z') 
                {
                    lower += letterguess.at(i) + 32; 

                }
                else
                {
                    lower += letterguess.at(i); 
                }  

            }                     
                       

        }
        if (letterguess.at(i) == word.at(i) && Validletterguess == true)
        {
            cout << "You guessed your first letter correctly! Enter your first word guess." << endl;
            cin >> wordguess;

        }    
       
        

        
        
    }
I also would like to add the dashes, but I do not know where to include them
Before you start coding, you should first design the program. It looks like you are attempting to design as coding - instead of before coding.

Only once you have a design do you start coding from the design. Compile and test frequently so that your program always compiles and written code works. Once you have fully coded the design then you should have a working program.

In programming sometimes the hardest part is the design. But this part isn't really coding related - its logic, required data, structures, flow, algorithms etc. If 2 people who know nothing about hangman follow your program design, would they be able to play using pen/paper? If yes, then you are probably ready to code. if not, then some further design work is needed.

Have fun!
@seeplus I have everything written out. I just need help setting up the first part.

what i know:
Player one enters word of any length. [y]
Player 2 gets 5 letter guesses, and 5 word guesses [y]
Player 2 must guess 1 letter, if more than 1 character is entered loop until its 1 letter [y]
allow case sensitiviy [y]
show dashes [havent figured that out yet]

I just don't know how to properly put it all in order, or where to put cout statements or anything like that. If you wouldn't mind, could you show me how it should look, so I can compare mine and where I need to put everything to have it in order

current code:

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    string word, wordguess, lower, dashes;
    string letterguess;    
    int numguess;
    bool GameOver = false, Validletterguess = true, Validwordguess = true;


    cout << "Player 1, enter a word for Player 2 to guess: " << endl;
    getline(cin, word);
    cout << endl;
    cout << "Player 2, you have 5 letter guesses, and 5 word guesses." << endl;
    cout << "Guess your first letter: " << endl;

    while (GameOver == false) // Start of Game. Setup for Round 1 letter guess and word guess.
    {      
        while (letterguess.length() < 1) // 1 letter per guess. If user enter "a" good input. If user enter "aa" or nothing, bad input, loop until good input. 
        {
            cout << endl << "Type a single letter and press <enter>: ";
            cin >> letterguess;   // enter first letter guess        

        }
        for (int i = 0; i < letterguess.length(); i++) //ignore case of letter guess? 
        {
            if (letterguess.at(i) = word.at(i))
            {
                cout << "You guessed your first letter correct. " << endl;
                cout << "Make your first word guess: " << endl;
            }

        }

        cin >> wordguess;
    }

        
        
}

I have everything written out.


I very much doubt that - as lines 32/33 refer to first letter/first word. If the design was right, the guess number would be irrelevant to that part of the code.

Have you come across 'top down' design and 'step-wise refinement'. If not, look them up. They're a process for the development of design.

For example, a first attempt at playing could be something like:

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
set word_to_guess to the word to be guessed
set word_guessed to all _ for the number of chars in word_to_guess
initialise max_letter_guess, max_word_guess to required max values
initialise letter guess and word_guess to 0

while word_not_guessed and attempts_remain {
    obtain guess
    if guess is single char
        if guess in word_to_guess {
            set all chars in word_guessed to guess where guess equals a char in word_to_guess
            if guessed equals word_to_guess
                game over
        } else
            increment no_char_guesses
    else
        if guess equals word_to_guess {
            set word_guessed to actual word
            game over
        } else
             increment no_word_guesses
    display word_guessed
}

if word_guessed equals word_to_guess
    display You win
else {
    display you loose. The word was
    display word-to_guess
}

Topic archived. No new replies allowed.