# Hangman program question (C++)

This topic is 2462 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So, I'm already back with my question of the hour.

I've just finished looking over the hangman program from my book, and while the majority of it makes sense, there are two parts I don't seem to get.

// Hangman // The classic game of hangman #include <iostream> #include <string> #include <vector> #include <algorithm> #include <ctime> #include <cctype> using namespace std; int main() { //setup const int MAX_WRONG = 8; //maximum number of incorrect guesses allowed vector<string> words; //collection of possible words to guess words.push_back("GUESS"); words.push_back("HANGMAN"); words.push_back("DIFFICULT"); srand(static_cast<unsigned int>(time(0))); random_shuffle(words.begin(), words.end()); const string THE_WORD = words[0]; //word to guess int wrong = 0; //number of incorrect guesses string soFar(THE_WORD.size(), '-'); //word guessed so far string used = ""; //letters already guessed cout << "Welcome to Hangman. Good luck!\n"; //main loop while ((wrong < MAX_WRONG) && (soFar != THE_WORD)) { cout << "\n\nYou have " << (MAX_WRONG - wrong); cout << " incorrect guesses left.\n"; cout << "\nYou've used the following letters:\n" << used << endl; cout << "\nSo far, the word is:\n" << soFar << endl; char guess; cout << "\n\nEnter your guess: "; cin >> guess; guess = toupper(guess); //make uppercase since secret word is in uppercase while (used.find(guess) != string::npos) { cout << "\nYou've already guessed " << guess << endl; cout << "Enter your guess: "; cin >> guess; guess = toupper(guess); } used += guess; if (THE_WORD.find(guess) != string::npos) { cout << "That's right! " << guess << " is in the word.\n"; //update soFar to include newly guessed letter for (unsigned int i = 0; i < THE_WORD.length(); ++i) { if (THE_WORD == guess) { soFar = guess; } } } else { cout << "Sorry, " << guess << " isn't in the word.\n"; ++wrong; } } //shut down if (wrong == MAX_WRONG) { cout << "\nYou've been hanged!"; } else { cout << "\nYou guessed it!"; } cout << "\nThe word was " << THE_WORD << endl; return 0; }

How does the program replace the secret word's characters as they are guessed, and how does it search for those characters in the word? I think I can make out where it does it, but the process isn't making sense to me. I can see it compare guesses to the word, see that it tells how they're there or not and update MAX_WRONG, but the replacement/update/completion I can't make out.

I appreciate the help and sorry for posting a new thread seemingly on the hour.

Edit: Actually think I may be starting to make some progress on it. May be able to solo this one after all.

I was originally misunderstanding , but what I am thinking now is:
 for (unsigned int i = 0; i < THE_WORD.length(); ++i) { if (THE_WORD == guess) { soFar = guess; } 
is incremented to check position by position through the length of THE_WORD for the value of guess, if its there, it updates the same position in soFar, which simply mimics the length of THE_WORD with '-' instead of the position's real value. The value for that position is updated with the value of guess in all positions it applies to. Woo, I think I got it. Sorry again for posting so much. Edited by ebontide

##### Share on other sites
It looks like you hit the nail on the head with what the for loop is doing. Nice job and keep up the good work.

##### Share on other sites
I appreciate it. These forums seem have been a great help.

##### Share on other sites
Hidden
here is where the words come from

 vector<string> words; //collection of possible words to guess words.push_back("GUESS"); words.push_back("HANGMAN"); words.push_back("DIFFICULT"); 

if you want more, just push back more (preferbly from a dictionary)