Sign in to follow this  

std::npos

This topic is 4669 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
	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[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;

} 

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
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.

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

This topic is 4669 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this