How to compare a certain character in a string WITHOUT the .at() function?

I need to compare a string's letters to another char (a guess in my hangman program). The string temp; comparison bombs out and stops working in the if statement after a user enters two wrong guesses. I believe it is a problem using the .at() string function. Is there another way to pull out a character at a location in my string and make a comparison using that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for(int b = 0; b < Word.newWord.length()  ;b++) //runs for all spaces in newWord. newWord is the word to be guessed.
      {         
      for(int a = 0; a < Word.newWord.length(); a++) //same as above
      {
              string temp;
              temp = Word.newWord[a];// Word.newWord.at(a) doesn't work either - causes same problem
              
                            if(Word.alphabet[b] == temp) //if user guess and word match
                            {
                           output[a]= Word.newWord[a] ; //Word.newWord.at(a) doesn't work either - causes same problem
              
                           //str2 = str2+ Word.newWord.at(a);
                             }                       
              } //for end
              }//for end 
Last edited on
Shouldn't b < Word.newWord.length() be b < Word.alphabet.length()?
Nope, That is what allows the guess to be cycled through all spaces in the word for any combination. The cycle amount is dependent on the word length.
You access Word.alphabet[b] so this will only work if Word.newWord.length() <= Word.alphabet.length()
Last edited on
You can call the internal c-string using

const char* string::c_str()

and then dereference it the way you want.
at() is checked access. If that causes a problem, so will unchecked access -- it will just be harder to track the problem down.

We don't know types for your code snippet, so it's difficult to understand what you're trying to accomplish and what should work.

I'm assuming word.newWord is a std::string representing the word to be guessed, and word.alphabet is a string representing ?

output, which I would assume is a std::string turns out to be an array of std::strings which is not what you want. One std::string will serve. The type of Word is not apparent. You need to supply at least the type definitions for meaningful advice concerning your existing code. (of the form class XXX { ... };

(More code found here: http://www.cplusplus.com/forum/general/70486/, but still lacking type definitions.)



Last edited on
The .length() suggests that Word.newWord is a string. If this is the case, I would use

1
2
char temp;
temp = Word.newWord[a];


rather than

1
2
string temp;
temp = Word.newWord[a];


Or it it something else?
My apologies for the lack of information; the declarations are as follows:

1
2
3
      string alphabet[30]; //will house guesses
      string userEnteredWord;//user's guesses
      string newWord; //sets new word 
Last edited on
Topic archived. No new replies allowed.