Help me to understand these sections of code

So, i am going through the book 'Beginning C++ Through Game Programming'. I have reached the Hangman game and am trying to understand a few sections of code.

Here is the full example

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include<cctype>
#include <random>

using namespace std;

int main()

{

const int MAX_WRONG = 8;
vector<string> words;
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];
int wrong = 0;
string so_far(THE_WORD.length(), '_');
string used;

while ((wrong < MAX_WRONG) && (so_far != THE_WORD))
{
cout << "You have " << (MAX_WRONG - wrong) << " guesses left " << endl << endl;
cout << "You've used the following letters " << used << endl;
cout << "So far, the word is " << so_far << endl << endl;

char guess;
cout << "Enter your guess " << endl;
cin >> guess;

while (used.find(guess) != string::npos)
{
cout << "You've already guessed " << guess << endl << endl;
cout << "Enter your guess " << endl;
cin >> guess;
}

used += guess;

if (THE_WORD.find(guess) != string::npos)
{
cout << "That's right! " << guess << " is in the word" << endl << endl;

for (int i = 0; i < THE_WORD.length(); ++i)
{
if (THE_WORD[i] == guess)
{
so_far[i] = guess;
}
}
}
else
{
cout << "Sorry, " << guess << " isn't in the word" << endl << endl;
++wrong;
}
}

if (wrong == MAX_WRONG)
{
cout << "You've been hanged" << endl << endl;
}
else
{
cout << "You guessed it" << endl;
}

cout << "The word was " << THE_WORD << endl;

system("pause");

return 0;
}

For the most part i feel like i understand it but would just like to check whether my understanding is sound or flawed.

These two sections of code in particular are my main issue

while (used.find(guess) != string::npos)

Does this mean, we are searching for 'used' using 'find' in the 'guess' and we do so until the end of the string, if we don't reach the end of the string then we have a match and we request a new character.

if (THE_WORD.find(guess) != string::npos)

Does this mean, we are searching for any of the letters in 'THE_WORD' using 'find' in 'guess' and we do so until the end of the string, if we don't reach the end of the string then we have a match and move forward.
while (used.find(guess) != std::string::npos)

string::find(string str); return the position of the first character of the matching "used" substring with "guess", and return std::string::npos (i.e -1) if there is no match.

if (THE_WORD.find(guess) != std::string::npos)

Same as above, you're searching for substring, not chars.

Without second argument, find search from the beginning of the string.
Last edited on
Zainyorkshireman,



PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Well you are about half right, but I believe backwards. You are searching the string "used" containing all the letters that are correct and have been used using "guess" as what to match to.

If a match is found you enter the while loop and arre asked to guess again until you enter a letter that is new.

The same applies for the second example.

You may find these helpful:

http://www.cplusplus.com/reference/string/string/find/

http://www.cplusplus.com/reference/string/string/npos/

Hope that helps,

Andy
Hi

Thank you for all of the above, that makes things a lot clearer

So basically we are finding the guess in used and finding the guess in the word

Regarding the != std::string::npos), does this mean until we reach the end of the 'used' string and 'the_word' string, or at least keep searching until we reach a match
npos basically means "not a position", which in this context means not found.

For the while loop, it means keep looping while a match is found, a match being found if they used that word already. At first loop the used would be empty, so the nothing would be found. Every time they try again, if the re-use a word, it is found, and the loop repeats.

In the other clause, the "THE_WORD" string, if not found, they failed the guess. Fashioned as != npos, that's "not - not found", which means found ;)

This mean until you get no match.
Topic archived. No new replies allowed.