std::npos

Started by
4 comments, last by SmugBoy 19 years, 1 month ago
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
	words.push_back("GUESS");
	words.push_back("HANGMAN");
	words.push_back("DIFFICULT");

	srand(time(0));
	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";
			++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;

} 
Advertisement
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.
string::npos is basically the value that a string returns whenever what you are searching for is not found.
"What are you trying to tell me? That I can write an O(N^2) recursive solution for a 2-dimensional knapsack?" "No, programmer. I'm trying to tell you that when you're ready, you won't have to." -Adapted from "The Matrix"
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'

else

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

thanks again for all the help.
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.
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.

This topic is closed to new replies.

Advertisement