I need some help with hangman.

I need to be able to find the location of ALL instances of a single character in a string. This is my code so far:

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
  #include <iostream>
#include <cstdlib>

using namespace std;

string input, currentWord;
int numberOfBadGuesses = 0;

void clearConsole(){
    system("CLS");
}

void updateCurrentWord(string toBeSearched, string whatToFind){
    currentWord.replace(toBeSearched.find(whatToFind), 1, whatToFind);
}

void prompt(string letter){
    try{
    updateCurrentWord(input, letter);
    }catch(out_of_range){
    }
    clearConsole();
    cout << currentWord << "\n";
    cout << "You have " << 6 - numberOfBadGuesses << " guesses remaining.";
}

void hangman(string input){
    string letterInput;
    clearConsole();
    for(int i = 0; i < input.length(); i++){
        currentWord = currentWord + "_";
    }
    cout << currentWord << "\n";
    cout << "Enter a letter.";
    cin >> letterInput;
    prompt(letterInput);

}

int main()
{
    cout << "Welcome to hangman! Start by typing in a word." << endl;
    cin >> input;
    hangman(input);
}


The function I need help with is updateCurrentWord(). It only changes the first instance of the letter. Thanks for your help!
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
#include <iostream>
#include <string>
#include <cctype>

// update guessed_word_so_far, return true if at least one character was updated
bool update( const std::string& word, std::string& guessed_word_so_far, char ch ) {

    bool updated = false ;

    for( std::size_t i = 0 ; i < word.size() ; ++i ) {

        if( word[i] == ch ) {

            guessed_word_so_far[i] = ch ;
            updated = true ;
        }
    }

    return updated ;
}

// return true if this character was already guessed
bool already_guessed( const std::string& guesses_made_so_far, char ch ) {

    return guesses_made_so_far.find(ch) != std::string::npos ;
}

// return the next character guessed
char get_guess( const std::string& guessed_word_so_far, std::string& guesses_made_so_far, int n_guesses_left ) {

    std::cout << "\nword: " << guessed_word_so_far << '\n'
              << "\nYou have " << n_guesses_left << " wrong guesses remaining.\nguess? ";

    char ch ;
    std::cin >> ch ;
    ch = std::toupper(ch) ;

    if( !std::isalpha(ch) ) std::cout << "please enter a letter\n" ;
    else if( already_guessed( guesses_made_so_far, ch ) ) std::cout << "already guessed\n" ;
    else {

        guesses_made_so_far += ch ;
        return ch ;
    }

    return get_guess( guessed_word_so_far, guesses_made_so_far, n_guesses_left ) ; // try again
}

// return true if the word was guessed correctly
bool hangman( std::string word ) {

    for( char& c : word ) c = std::toupper(c) ; // make all upper case
    std::string guessed_word_so_far( word.size(), '_' ) ;
    std::string guesses_made_so_far ;
    int n_guesses_left = 6 ;

    while( n_guesses_left > 0 ) {

        const char ch = get_guess( guessed_word_so_far, guesses_made_so_far, n_guesses_left ) ;
        if( !update( word, guessed_word_so_far, ch ) ) --n_guesses_left ;

        if( guessed_word_so_far == word ) return true ;
    }

    return false ;
}

int main() {

    std::cout << "Welcome to hangman!\n" ; // Start by typing in a word: " ;

    // TO DO: generate a ward at random (use a list of words and pick one)
    std::string word = "SLEEPLESSNESS" ;
    // std::cin >> word;

    if( hangman(word) ) std::cout << "\nwell done! you guessed it.\n" ;
    else std::cout << "\nbetter luck next time\n" ;

    std::cout << "\nthe word was: " << word << '\n' ;
}
Thanks for the reply, but that wasn't really what I was looking for. I was just looking for a way to make my updateCurrentWord() function work. This is for a project so I don't want to just copy and paste code.
If JLBorges' code doesn't supply you with what you need to solve your problem, then can you be more specific about what it is that you do need?

They've shown you a technique for identifying every character in the answer that matches the user's guess, and updating that character in the string containing the current guesses. How is that not what you wanted?
@RandomCppN00b,

I don't think that @JLBorges was expecting you to cut and paste code. He has deliberately highlighted the parts that you need to read and understand. Within that are the implications that:

- letter is a char, not a string

- you can simply loop through your characters with a for loop and change any that need changing;

- you shouldn't be using global variables like this; pass them as reference arguments

- it's convenient to return a bool to state whether anything was changed or not. Isn't that necessary for Hangman?


I think that there is also an implication that you need to restructure your code first before simply trying to "make my updateCurrentWord() function work".



As an unrelated aside, your exception-handling meant that I couldn't compile your code.
Last edited on
Topic archived. No new replies allowed.