Alright, I'm down to the nitty gritty absolute end of a fully functional hangman game. There is just one part where I am absolutely stuck. When you get a wrong guess, the game successfully draws the gallows. I have no issues with wrong guesses. It's the right guesses that has me stumped. Here is what I have for this:

guess(); //call member function of letter chosen
ltrpos = gameWord.find(gameGuess);
cout << "You have chosen the letter " << gameGuess << '.' << endl; 
if (ltrpos < gameWord.length()) //checks letter existance in chosen random word (correct choice)
{ 
for (i = 0; i < gameWord.length(); i++)
{
if (gameGuess == gameWord[i])
{
cout << gameGuess;
}
else
cout << '_'; 
}
cout << endl;
cout << "Wow, nice guess!!! Guess another letter." << endl;

When you guess the correct letter it DOES display ALL the instances of that character in the correct places. The problem I'm having is, when it loops around to guess again, how do I keep the correct guess that was just made and add to that guess with the next correct guess while putting the NEW letters in their correct instances as well.
Example of how it is now:
Loop 1: n_n__n__
Loop 2: _i______
Loop 3: ___t____
The word here should be nintendo and it should ALSO do this:
Loop 1: n_n__n__
Loop 2: nin__n__
Loop 3: nint_n__

Suggestions?????

Recommended Answers

All 10 Replies

You don't seem to be stuffing the letter! To save it!
Or some indication you have that letter within the word that was already tested!

if (gameGuess == gameWord[i])   // matching letter found?
{
   myWord[i] = gameGuess;  // <---- Stuff new letter
   cout << gameGuess;
}
else if (myWord[i] )
   cout << myWord[i];
else
  cout << '_'; 
}

Another approach could be
1.Make another userGuess string.
2. First , fill all the characters in userGuess string
with underscores( _ ).
3.Make changes in the userGuess string.

guess(); //call member function of letter chosen
ltrpos = gameWord.find(gameGuess);
cout << "You have chosen the letter " << gameGuess << '.' << endl; 
if (ltrpos < gameWord.length()) //checks letter existance in chosen random word (correct choice)
{ 
for (i = 0; i < gameWord.length(); i++)
{
    if (gameGuess == gameWord[i])
    {
        //Instead  of cout << gameGuess . I hope gameGuess is a 
       // a single character.
        userGuess[i] = gameGuess;    
    }

} //After the for loop display the changed string altogether
cout  << userGuess; 
//This will retain changes

4. Search for any underscores remaining in the userGuess string.
If there are , the word is stilll incomplete. Loop again.

Edit:
@wildgoose: I think gameWord is the original string.

Yes, simultaneous post. It's late here, and I noticed that after the post and editted then saw your post!

Cromarte88, you need either to store the characters you picked correctly, or store a flag for each correctly chosen character position so that letters previously chosen will be displayed!

Another approach could be
1.Make another userGuess string.
2. First , fill all the characters in userGuess string
with underscores( _ ).
3.Make changes in the userGuess string.

guess(); //call member function of letter chosen
ltrpos = gameWord.find(gameGuess);
cout << "You have chosen the letter " << gameGuess << '.' << endl; 
if (ltrpos < gameWord.length()) //checks letter existance in chosen random word (correct choice)
{ 
for (i = 0; i < gameWord.length(); i++)
{
    if (gameGuess == gameWord[i])
    {
        //Instead  of cout << gameGuess . I hope gameGuess is a 
       // a single character.
        userGuess[i] = gameGuess;    
    }

} //After the for loop display the changed string altogether
cout  << userGuess; 
//This will retain changes

4. Search for any underscores remaining in the userGuess string.
If there are , the word is stilll incomplete. Loop again.

Edit:
@wildgoose: I think gameWord is the original string.

I can see how this will print out the word the way I have it now, but I'm not fully understanding how this will add new correctly guessed words to the string. I'll give it a whirl though. Thanks for your thoughts, I greatly appreciate it.

Method of using a double character buffer!

<-- gameWord[]
<-- gameGuess[]

As each letter guessed, the letter is copied over to same index

Only display gameGuess, not gameWord gets displayed.

Another approach could be
1.Make another userGuess string.
2. First , fill all the characters in userGuess string
with underscores( _ ).

I'm not clear on how this helps me if the word is a different length every time the game is played. That seems like it would be useful if all words were the same length.

So, use a big buffer for both. Get string length of actual hangman string, fill guess string with '_' and set the ASCIIz terminator at the element corresponding to the string length.

So now they are the same length!

I said there were several ways of doing this. If you don't fully understand strings then go back and review!

if the word is a different length every time the game is played. That seems like it would be useful if all words were the same length.

Then..

So, use a big buffer for both....

I wouldn't agree.
A snippet which might help you if you can see the link.:twisted:

#include<iostream>
#include<string>

int main()
{
    std::string word = "Hangman";
    std::string userGuess(word.length() , '+'); //<<
    std::cout <<"The original string is" << word;
    std::cout <<"\nThe contents of the userGuess string is" << userGuess;
    userGuess.erase(); //<<
    std::cout <<"\nAfter erasing" <<userGuess;

    word = "daniweb";
    std::cout <<"The original string is " << word;
    //And then again
    userGuess.resize(word.length(), '_'); //<<
    std::cout <<"The original string is" << word;
    std::cout <<"\nThe contents of the userGuess string is" << userGuess;
    return 0;
}

Like I posted. There are several ways of doing this! In my case, it was based upon a fixed length buffer that gets reused. In Zalezog's case, they're promoting the use of a dynamic buffer. You're having trouble with simple ASCII buffers so I felt the fixed length buffer was a good starting place. The method of using variable length buffer's such as using the string class is the more advanced but better solution. You are using C++ in your code!

Then..

I wouldn't agree.
A snippet which might help you if you can see the link.:twisted:

#include<iostream>
#include<string>

int main()
{
    std::string word = "Hangman";
    std::string userGuess(word.length() , '+'); //<<
    std::cout <<"The original string is" << word;
    std::cout <<"\nThe contents of the userGuess string is" << userGuess;
    userGuess.erase(); //<<
    std::cout <<"\nAfter erasing" <<userGuess;

    word = "daniweb";
    std::cout <<"The original string is " << word;
    //And then again
    userGuess.resize(word.length(), '_'); //<<
    std::cout <<"The original string is" << word;
    std::cout <<"\nThe contents of the userGuess string is" << userGuess;
    return 0;
}

Awesome...this explained a LOT to me. Thanks for the snippet. Not something I can directly use in the program but like I said, it does explain a lot to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.