Jump to content
  • Advertisement
Sign in to follow this  
agm_ultimatex

comparing each character in a string. c++

This topic is 3738 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

Hello, Im working on a text-based hangman game for practice, and im stuck at the part where I compare the letter taken from the user to the word. Heres my current code, in which it does not compile:
//checks if letter is in the word
if(theWord.find(letter) != std::string::npos)
{
	if(lettersUsed.find(letter) == std::string::npos)
	{
		std::cout << "You got a letter in the word.\n";
		for(unsigned int i = 0; i < theWord.length(); i++)
		{
			if(theWord.at(i) == letter)
			{
				displayWord += letter;
			}
			else
			{
				displayWord += "-";
			}
		}
	}
}


Share this post


Link to post
Share on other sites
Advertisement
Hí,


// init displayWord
for(unsigned int i=0; i < theWord.length(); ++i)
{
diplayWord += '-';
}

// ...

//checks if letter is in the word
if(theWord.find(letter) != theWord.end())
{
if(lettersUsed.find(letter) == lettersUsed.end())
{
for(unsigned int i=0; i < theWord.length(); ++i)
{
if(theWord == letter)
{
diplayWord = letter;
}
}
}
}




Hope to get you going.

Share this post


Link to post
Share on other sites
Quote:
Original post by dragongame
if(theWord.find(letter) != theWord.end())
{
if(lettersUsed.find(letter) == lettersUsed.end())

Yeah don't do that, find has an integral return type not an iterator type, if the search fails then it returns the value of std::string::npos.

Share this post


Link to post
Share on other sites
hmm okay. The error im getting is at the line: if(theWord.at(i) == letter)
none the less, ill try to find another way to do this on the whole.

Any recommendations on functions to google?

Share this post


Link to post
Share on other sites
Quote:
Original post by Portmanteau
AFAIK, the STL string class does not have an at method, just an [] operator.


visual studio brings up the at in the list when you type string1. However using [] also gives me the error: Error 2 error C2784: 'bool std::operator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const char'

Share this post


Link to post
Share on other sites
Well I am guessing letter is an std::string because you use it in find above, in that case you cannot compare a char to an std::string, try letter[0].

Share this post


Link to post
Share on other sites
Whether this compiles or not depends on the types of things. I don't get where the vector comes into play as mentioned in the error message.

But logically, I think it won't work anyway: it will only show you the character you guessed last or -, not what you have guessed already or -.

Share this post


Link to post
Share on other sites
Quote:
Original post by agm_ultimatex
Quote:
Original post by Portmanteau
AFAIK, the STL string class does not have an at method, just an [] operator.


visual studio brings up the at in the list when you type string1. However using [] also gives me the error: Error 2 error C2784: 'bool std::operator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const char'


Show the part of the code where you declare the variables you're using here.

Share this post


Link to post
Share on other sites
Quote:
Original post by agm_ultimatex
Quote:
Original post by Portmanteau
AFAIK, the STL string class does not have an at method, just an [] operator.


visual studio brings up the at in the list when you type string

The STL string and the SC++L string are actually quite different in a number of ways.

Quote:
using [] also gives me the error: Error 2 error C2784: 'bool std::operator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const char'

You can't compare a std::string::reference (returned by std::string::at and std::string::operator[]) to a std::string using the (std::operator==) equality operator.
Either compare with letter[0] or make letter into a char.

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!