Hangman String Comparison Problems

I'm having a problem where if I try to compare a user's guess (string "userEnteredWord" which was then assigned to a corresponding array location in string "alphabet"). I cannot get the letters to display correctly, or to compare effectively. Does anyone know how I can fix this? I want to get it it display the letter where it should be, and a "_" if the letter hasn't been guessed yet.
My problem lies in voidCurrentWord::compareGuessToWord() which in in declarations.cpp.

This is my 'outline.h' header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//prevent multiple inclusions of header
#ifndef TIME_H
#define TIME_H
//Time Class definition
class CurrentWord
{
      public:
      string alphabet[30]; //will house guesses
      int alphabetCounter; //counter to cycle alphabet string
      string userEnteredWord; //user's guesses
      string newWord; //sets new word

            CurrentWord(); //constructor
             void outputGuesses(); //output user's guesses
             void inputGuesses(); //User enter letterguess
             void setNewWord(); //set new word to guess
            void toLowerCase(string &a); //change to lower case
            void compareGuessToWord(); // compare the user's guess to the actual word that is to be guessed
      
      private:
          string buffer; // used to open word file
           FILE * pFile; // used to open word file

}; // end class

#endif


This is a partial of my "declarations.cpp" with the currentword class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

void CurrentWord::outputGuesses() //displays all guesses made. guesses stored in an array
{
     for(int a = 0; a < alphabetCounter; a++)
     cout << alphabet[a] << ",";
     
     cout << endl;
 };
 void CurrentWord::inputGuesses() //input guess. stores to array
 {
      cout << "Enter Letter Guess: ";
      cin >> userEnteredWord;
      toLowerCase(userEnteredWord);
      alphabet[alphabetCounter] = userEnteredWord;
      alphabetCounter = alphabetCounter + 1;
      
  };
 void CurrentWord::compareGuessToWord() //THIS function is where my problem lies
 {
      bool sorted = true; 
     
      cout << "Correctly guessed letters so far: ";
      for(int b = 0; b < newWord.length() && sorted ;b++) //runs for all spaces in newWord. newWord is the word to be guessed.
      {
             while(sorted)
              {
              sorted = false;
              
      for(int a = 0; a < newWord.length(); a++)
      { 
              string temp;
              temp = newWord.at(a); //allows my to compare array values
             
              if(alphabet[b] == temp)
                            { 
                            cout << alphabet[b];
                             
                             }
              if(alphabet[b] != temp)
              {
                             cout << " _ ";
                           
                             }
              } //for
              }//while
              }//far
              cout << endl;
  };
Last edited on
Topic archived. No new replies allowed.