This is an assignment for class,design a hangman program,I convert the letters of the random word to charchters,when the user inputs there letter how do I compare that with the secret word, and display it,any suggestions????

for(int i = 0; i<word_to_guess.size();i++)
                  {
                      word_to_guess[i] = 'X';
                     
                  }
                   cout<<word_to_guess<<endl<<endl;
                  
                   
          while (lives>0 && wordguessed!=word_to_guess)
          {
               cout<<"You have "<<lives<<" guesses left"<<endl<<endl;
               cout<<"Please enter your guess"<<endl<<endl;
               cin>>guess;                         
               lives = lives -1;
               lettersguessed = lettersguessed + guess;
          
               solution = word_to_guess.find(guess)
                           
               
               
                                               
               cout<<"Your letters "<<lettersguessed<<endl<<endl;

Recommended Answers

All 5 Replies

There's not really enough here for us to tell how you are currently doing things. I think you might want to do something with parallel arrays. At minimum, have an array with the secret word and an array to track the revealed letters. Or is that what you are already doing?

Sorry thats not all the code,i have a string array with 10 words,the program randomly picks one of these words,it then converts the letters in the word to 'X',the user is asked to input a letter. I would like to know how to find that letter in the secret word

So it places the correct number of 'X's in word_to_guess[] as placeholders, then you need to figure out how to convert the 'X's to the proper letters?

Algorithm (pseudocode):

accept a character input;
begin a for loop that runs from i=0 to i=secretWordLength-1;
  compare the input letter to each character in the [B]original string[/B];
  if the character matches:
    set the character word_to_guess[i] to the correct character;
end for loop

Thanks,I had something like that a while ago but i must have made a mistake in my code,thanks again

use a loop to compare each letter in the word to guess to the character acting as the current guess.

If the guessed char is in the word to guess then replace the X by the appropriate letter in the appropriate spot of the display array, just like on Wheel of Fortune. .

If the guessed letter isn't in the word to guess, then add a body part to the hangman.

bool notFound = true;
for each letter in the word to guess
  if current letter in word to guess is the same as the character guess
     change the X in the display word to the character guess
     change the value of notFound to false

if notFound
  add a body part to the hangman

Edit: I'm typing too slow today

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.