//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 += "-";
}
}
}
}
comparing each character in a string. c++
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:
Hí,
Hope to get you going.
// init displayWordfor(unsigned int i=0; i < theWord.length(); ++i){ diplayWord += '-';}// ...//checks if letter is in the wordif(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.
Quote:Original post by dragongameif(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.
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?
none the less, ill try to find another way to do this on the whole.
Any recommendations on functions to google?
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'
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].
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 -.
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 -.
Quote:Original post by agm_ultimatexQuote: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.
Quote:Original post by agm_ultimatexQuote: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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement