someone explain this code for me please!

hi, so i'm currently reading Beginning c++ through game programming, at the end of chap.4, he writes a HangMan game, now there's something really annoying me and i really appreciate it if someone could explain it to me. the underlined code is what i don't understand, wtf is that 'used' thingy, he did string used = ""; wtf does it even mean!? there's nothing inside of it, empty! and he did used.find(guess) != string::npos, really confusing me, please someone explain, i would really really appreciate it, npos means a letter that can't exist right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

int main()
{
    // set-up
    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 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[i] == guess)
				{
                    soFar[i] = 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;
}
string used = ""
A string is an array of characters with many useful functions. This is just creating an empty string.

1
2
3
4
5
6
7
while (used.find(guess) != string::npos)
        {
            cout << "\nYou've already guessed " << guess << endl;
            cout << "Enter your guess: ";
            cin >> guess;
            guess = toupper(guess);
        }


find is a function for strings (the type of variable used is). It returns the position in the string where the character you're searching is. If it returns string::npos, then the character isn't contained in the string.If the guess is already in used, then that means that letter has already been guessed before.

used += guess;
This just adds the guess to the string.

1
2
3
4
5
6
7
8
9
10
11
12
13
        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[i] == guess)
				{
                    soFar[i] = guess;
				}
			}
        }


If find doesn't return string::npos, then the guess is somewhere in the word. The for loop just searches for that characters position in THE_WORD and then puts it in that same position in soFar.

Tell me if I wasn't clear enough on any of this. Hope it helped.
std::string::npos:
http://www.cplusplus.com/reference/string/string/npos/

...As a return value, it is usually used to indicate no matches.


string used = "";
This is used to keep track of the letters that have already been guessed. The std::string.find method returns the position the substring is located in the string otherwise it returns npos

lines 42 to 48 will loop until you enter a guess that is not already in the used string which is why right after line 48 the next line of code appends the character to the string

Remember that initially this used string has to be empty because you have not made any guesses
Last edited on
Thank you so much both of you, now i understand :-)
Topic archived. No new replies allowed.