Jump to content
  • Advertisement
Sign in to follow this  


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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can anyone tell me in layman’s terms what std::npos means and does I am reading Beginning C++ for gamers and at the end of chapter 4. Hangman is the example game. I understand all the code except what str::npos actually does see line 47 I have googled and msdn it without fully understanding
 // Hangman
// The classic game of hangman

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

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;

int main()
	// setup
	const int MAX_WRONG = 8;		// maximum number of incorrect guesses allowed

	vector<string> words;		// collection of possible words to guess

	random_shuffle(words.begin(), words.end());
	const string THE_WORD = words[0];
	int wrong = 0;
	string soFar(THE_WORD.size(), '-');
	string used = "";
	cout << "Welcome to Hangman. Good luck!\n";

	// main loop
	while ((wrong < MAX_WRONG) && (soFar != THE_WORD)) {

		cout << "\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.";
		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 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";

	// shut down
	if (wrong == MAX_WRONG)
		cout << "\nYou've been hanged!";
		cout << "\nYou guessed it!";

	cout << "\nThe word was " << THE_WORD << endl;

	return 0;


Share this post

Link to post
Share on other sites
It represents a position that was not found.

IE: In terms of that code, string::find == string::npos means that whatever you were searching for was not found.

Share this post

Link to post
Share on other sites
string::npos is basically the value that a string returns whenever what you are searching for is not found.

Share this post

Link to post
Share on other sites
WOW thanks for the quick replies. Just so I get this straight the line

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

means if 'guess' is found in 'used' and is NOT 'not found' then the statement is 'true'


if 'guess' is NOT found in 'users' and is 'not found' the statement is 'false'

thanks again for all the help.

Share this post

Link to post
Share on other sites
find will return the index of the guess. If it does not exist then how can it signal this to you? The only ways are to either throw an exception or return a unique value that signifys an error. The string class defines the unique value as std::string::npos. npos is typically the maximum value of an unsigned type, for example it might be static_cast<unsigned int>(-1). It is this high to prevent you passing in a very large string that contains the found letter at an index that is the same as the npos unique failure value.

Share this post

Link to post
Share on other sites
I think I understand now.

With the find only returning a value it does not make the statement true or false that is why it is compared to std::npos so it can return a bool value to the while statement.

Thanks again for all the help.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!