Question

I'm working on a project that requires me to make a game of Hangman in c++. I have most of it working, but I'm stuck at printing out the part of the word guessed correctly each time after the user enters a guess. I've created a class to represent a game of hangman, and in this class are the methods that determine what to do for a guess. If a guess is found in any location in the randomly chosen word from a dictionary, I save that char to the same location in a vector called currentWord. currentWord is initialized in the constructor to be contain "_" for the length of the randomly chosen word(that way it is the same size as the word and I can just update it as the user types a guess). For example, if the word is "semicolonialism", and the user's first guess is 'i', I want to replace the '_' in the currentWord vector with the letter 'i'.

string tempWord = word;
    for (int i = 0; i < tempWord.size(); i++) {


        u_long location = tempWord.find(guess);
        currentWord->at(location) = tempWord[location];
        tempWord[location] = '_';
    }

What I've tried to do is store the member variable "word" in a temporary variable called tempWord. Then I iterate from 0 to the length of tempword. I use tempWord.find(guess) to find the location in tempWord that is a match for the guess, store that into a variable called location, and then update the currentWord vector at that location to equal the tempWord at that location. Since this would only work for the first time the matching char is found, I then change tempWord[location] to '_', that way the next time through, location will be different. But by doing this I some times get the out of range error. If I comment out

tempWord[location] = '_';

then I don't see this error, but only the first occurrence is replaced. Even though I get this out of bounds error, I can see in the debugger that each occurrence is properly replaced in the currentWord vector. This leaves me very confused, so any help would be greatly appreciated! Thanks

EDIT

Thanks to rapptz suggestion to check if location is equal to std::string::npos, I finally have it working. Here's the updated code segment with that check in place:

string tempWord = word;
    for (int i = 0; i < tempWord.size(); i++) {


        u_long location = tempWord.find(guess);
        if (location != std::string::npos) {
            currentWord->at(location) = tempWord[location];
            tempWord[location] = '_';
        }

    }

I really liked Tristan's suggestion too, and will do that tomorrow most likely. Once I do, I'll post the updated code as well in case someone else might find it useful. Thanks again!

Was it helpful?

Solution

Was going to post this as a comment but it's easier in a bigger text box! You can avoid both the tempWord copy and the for loop like this:

std::string::size_type location = 0, start_pos = 0; // int would be fine, tbh

while ( (location = word.find(guess, start_pos)) != std::string::npos) {
    currentWord.at(location) = word[location];
    start_pos = location;
}

OTHER TIPS

My guess is that tempword.find(guess) starts from 1 to the lenght of word, not 0. Please share that function too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top