Hangman Code?

HI! :) I'm new to programming and I have a hangman assignment to do to learn the concept of functions. We aren't allowed to have cin or cout statements in the setupUnsolved or updateUnsolved functions.


I am having trouble getting the prevGuesses string to come out properly (this is supposed to print all the letters the user has guessed). Also, my updateUnsolved function doesn't "stay updated". So for phrase "hello" and first guess 'h' it would be h----, and second guess 'e', it would be -e---.


I am looking for suggestions on what I should do to fix the updateUnsolved function as well as printing all the guessed characters (the prevGuesses string)


On another question, how would I set up the "incorrect" guesses if the user guesses incorrectly so it deducts a try? (There are 7 guesses) I'm not looking for a direct answer, just more of the best loop to use or where to place this :)


Thank you!

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
using namespace std;


string setupUnsolved(string phrase)
{
    for (int pos = 0; pos <= phrase.size()-1; pos++)
    {
        if (phrase.at(pos) >= 'a' && phrase.at(pos) <= 'z')
        {
            phrase.at(pos) = '-';
        }
    }
    return phrase;
}

string updateUnsolved(string phrase, string unsolved, char guess)
{
    for (int pos = 0; pos <= phrase.size()-1; pos++)
    {
        if (phrase.at(pos) == guess)
        {
            unsolved.at(pos) = phrase.at(pos);
        }
    }
    
    return unsolved;
}

char getGuess(string & prevGuesses)
{
    char guess;
    string userGuess;
    
    cin >> guess;
    
    if (!(guess >= 'a' && guess <= 'z'))
    {
        cout << "Invalid guess! Please re-enter a guess: ";
        cin >> guess;
        cout << endl;
    }
    if (guess >= 'a' && guess <= 'z')
    {
        return guess;
    }
}




int main ()
{
    string phrase;
    string unsolved;
    string userGuess;
    char guess;
    string solved;
    string prevGuesses;

    
    cout << "Enter phrase: ";
    getline(cin, phrase);
    cout << endl;
    
    clearScreen();
    
    unsolved = setupUnsolved(phrase); // phrase replaced by dashed line
    
    cout << "Phrase: " << unsolved << endl;
    cout << endl;
    
    for (int i = 5; i >= 0; i--) // For loop just for testing purposes
    {
        cout << "Enter a guess: "; //taking a guess
        guess = getGuess(userGuess);
        cout << endl;
        
        prevGuesses = userGuess + guess; //needs fixing
        
        // All the guessed characters (needs fixing)
        cout << "Guessed so far: " << prevGuesses << endl;
        
        cout << "Phrase: " << updateUnsolved(phrase, unsolved, guess);
    
        cout << endl;
    }

    
    return 0;
}
Last edited on
std::string supports the += operator.
Ah! Thanks!!


What do you suggest about the updateUnsolved function?
Pass the unsolved string by reference. Right now you are creating a copy, updating and returning the copy, but never actually changing the original. If you pass by reference, you can change the original, so it keeps updating.
Thanks :) !

Where do you suggest I insert the "wrong guesses" part of the game? I currently have the following but it's not going as planned.. I tried printing wrongGuesses, but it comes out to 25 even after I initialized it to 0 so it's not taking in the number of wrong guesses correctly

** I replaced my for loop (from the top) to a while loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
while (unsolved != phrase)
    {
        cout << "Enter a guess: "; //taking a guess
        guess = getGuess(userGuess);
        cout << endl;
        
        for (int pos = 0; pos <= phrase.size()-1; pos++) // if the guess is not in the word
        {
            if (phrase.at(pos) != guess)
            {
                wrongGuesses++;
            }
        }
Last edited on
in your update function, if the loop doesn't find a match, return a null string. then check for the null string. if null string, increment the guess count.
My instructor had just told me we are not allowed to pass by reference so I would have to "update" my function in the main function.. how would I do this?
Registered users can post here. Sign in or register to post.