I cant understand a part of this HANGMAN code. Strings are used.

So i along with my friend decided to make hangman.

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>
using namespace std;

int main()
{
	cout << "*******************************************************************************" << endl;
	cout << "*\t" << "\t" << "\t" << "\t" << "\t" << "\t" << "\t" << "\t" << "\t      *" << endl;

	cout << "*\t" << "\t" << "\t" << "\t" << " HANGMAN" << "                                      *" << endl;

	cout << "*\t" << "\t" << "\t" << "\t" << "\t" << "\t" << "\t" << "\t" << "\t      *" << endl;
	cout << "*******************************************************************************" << endl;

	cout << "NOTE" << endl;
	cout << "The alphabets yout type have to be lowercase" << endl << endl;
	cout << "Enter the word for other player to guess" << endl;

	string word;
	rewrite:
	getline(cin, word);
	cout << endl;
	for (int i = 0;i < word.length();i++)
	{
		if (word.at(i) >= 65 && word.at(i) <= 90)
		{
			cout << "Your input contained an upper-case letter" << endl;
			cout << "Please type again without an upper-case letter" << endl;
			goto rewrite;
		}
	}

	string copy = word;

	string Underscore;

	for(int i=0; i!=word.length(); i++)
	{
	    if(word.at(i) == ' ')
		{
			Underscore += " ";
	    }
		else
		{
			Underscore += "_";
	    }
	}
  
	for(int i=0; i!=50; ++i)
	{
		cout << endl;
	}
	
	string guess;
	int wrong=0;

	while(1)
	{
		if(wrong == 6)
		{
			cout << "You Lose! The word was: " << word << endl;
			break;
		}

		cout << Underscore << endl;
		cout << "There are " << word.length() << " letters with spaces" << endl;
		cout << "You have " << 6 - wrong << " more tries left" << endl;
		
		if(Underscore == word)
		{
			cout << "You win!" << endl;
			break;
    	}
		
		cout << "Guess a letter or a word" << endl;
		getline(cin, guess);
    
    	if(guess.length() > 1)
		{
			if(guess == word)
			{
				cout << "WOW!!! You got it on the first try. You win!" << endl << endl;
				break;
			}
			else
			{
				cout << "Wrong word " << endl;
				wrong++;
			}
		}
		else if(copy.find(guess) != -1)
		{
			while(copy.find(guess) != -1)
			{
				Underscore.replace(copy.find(guess), 1, guess);
				copy.replace(copy.find(guess), 1, "_");
        	}
		}
		else
		{
			cout << "That's wrong" << endl;
			wrong++;
		}
    
		cout << endl;
	}
	system("pause");
	return 0;
}

My friend wrote the code from line 91 to line 98. Can someone explain me this part?
I cant understand whats happening. Whats the meaning of != -1 in line 91 and 93.
In fact it should be written as

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

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



EDIT: Fixed syntax error
Last edited on
How do you write std::string::npos without the colon? I dont know the meaning of :: yet. We haven't studied :: yet and we're not supposed to use this syntax.
C++ does allow declaring a hierarchy of names. This allows organizing names like objects in a directory. There exists a top level name called "std". Names like "string", "cin" and "cout" are subordinated to "std". Those doublecolon ("::") symbol acts as separator between two naming components, f.e. "std::string". "npos" is a constant declared as subordinated symbol to "std::string" so "std::string::npos" is its full name. "std::string::npos" may usually (but there may exists implementations defining it in some other way) be defined as '-1'.

The statement in line 3 of your code does instruct the compiler to lookup for unknown names below the top level namespace "std".

In general it is good practice to not use this using namespace std; statement to avoid unwanted insertion of names. Instead use fully qualified names like "std::can", ...
Topic archived. No new replies allowed.