Help with hangman game not begging for code!!

Heres the problem... Program almost works.. if i cut out the playAgain function the code runs..however say I enter the word "cat" as the word to guess. First guess enter 'c' it shows up and displays properly. second guess 'a' same result third guess 't' now program only displays 'c' and 't' entered and will not give a win untill 'a' is entered a second time. I hope this is clear enough and someone can point out what I am misunderstanding. Thank YOU!

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;

bool playing = true;
string word = "";
char knownLetters[] ={0};
char guess;
int playerLife = 7;
string secretWord;

void checkWin();
void checkLose();
void printWord();
void hangMan();
void playAgain();

 
 
int main ()
{
    cout <<"Please enter a word"<<endl; //prompts user for word
    cin >>secretWord;
  
    word = secretWord;                  //assigns secretWord to word
   
    while(playing)
    {
  
  
  cout <<"Guess a letter:\n";         //asks user to enter a letter
  cin >>guess;
  
  bool correct = 0;
  for(int i = 0; i < word.length(); i++)  //starts for loop cycles through each character
  {                                       //
          if(guess == word[i])            //while
          {
                   knownLetters[i] = guess;
                   correct = 1;
                   }
          
              
          } 
          if (correct == 0)
          --playerLife;
          correct = 0;
          cout <<playerLife<<endl;
          
  hangMan();
  printWord();
  checkWin();
  checkLose();
  playAgain();
  
  
    }
 
    
    system("PAUSE");
    return 0;

}


void hangMan()
 {
      if (playerLife == 7)
      {
      cout <<"__\n";
      cout <<"  |\n";
      cout <<"  |\n";
      cout <<"  |\n";
      cout <<" _|\n"<<endl;
           }
           else
           if (playerLife == 6)
      {
      cout <<"____  \n";
      cout <<" o  | \n";
      cout <<"    | \n";
      cout <<"    | \n";
      cout <<" ___| \n"<<endl;
           }
           else
           if (playerLife == 5)
      {
      cout <<"____  \n";
      cout <<" o  | \n";
      cout <<" { | \n";
      cout <<"    | \n";
      cout <<" ___| \n"<<endl;
           }
           else
           if (playerLife == 4)
      {
      cout <<"____  \n";
      cout <<" o  | \n";
      cout <<" {} | \n";
      cout <<"    | \n";
      cout <<" ___| \n"<<endl;
           }
           else
           if (playerLife == 3)
      {
      cout <<"____  \n";
      cout <<" o  | \n";
      cout <<"'{} | \n";
      cout <<"    | \n";
      cout <<" ___| \n"<<endl;
           }
           else
           if (playerLife == 2)
      {
      cout <<"____  \n";
      cout <<" o  | \n";
      cout <<"'{}'| \n";
      cout <<"    | \n";
      cout <<" ___| \n"<<endl;
           }
           else
           if (playerLife == 1)
      {
      cout <<"____  \n";
      cout <<" o  | \n";
      cout <<"'{}'| \n";
      cout <<" I  | \n";
      cout <<" ___| \n"<<endl;
           }
           else
           if (playerLife == 0)
      {
      cout <<"____  \n";
      cout <<" o  | \n";
      cout <<"'{}'| \n";
      cout <<" II | \n";
      cout <<" ___| \n"<<endl;
           }
      
      
      
      }

 void printWord()
{
       
       for(int i = 0; i < word.length(); i++)
       {
          if(knownLetters[i] == word[i])
          cout <<word[i];
          else
          cout <<"*";
       }        
  }

void checkWin()
{
     if(knownLetters == word)
     cout <<"Congratulations you WIN!!!"<<endl;
     }
     
void checkLose()
{
     if(playerLife == 0)
     cout <<"You LOST!!"<<endl;
     }
     
void playAgain()
{
     char ans;
     
     cout <<"Would you like to play again? y or n"<<endl;
     cin >>ans;
     
     if (ans == y)
     {
     playing = true;
     }
     else 
     if (ans == n)
     {
     playing = false;
     }
}
1. This does not compile
if (ans == y) is invalid. Use if (tolower(ans) == 'y')

2. You need to use cin.ignore() after your cin >> to remove any newlines.
http://www.cplusplus.com/forum/articles/6046/
This compiles and you can see what im talking about

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;

bool playing = true;
string word = "";
char knownLetters[] ={0};
char guess;
int playerLife = 7;
string secretWord;

void checkWin();
void checkLose();
void printWord();
void hangMan();


 
 
int main ()
{
    cout <<"Please enter a word"<<endl; //prompts user for word
    cin >>secretWord;
  
    word = secretWord;                  //assigns secretWord to word
   
    while(playing)
    {
  
  
  cout <<"Guess a letter:\n";         //asks user to enter a letter
  cin >>guess;
  
  bool correct = 0;
  for(int i = 0; i < word.length(); i++)  //starts for loop cycles through each character
  {                                       //
          if(guess == word[i])            //while
          {
                   knownLetters[i] = guess;
                   correct = 1;
                   }
          
              
          } 
          if (correct == 0)
          --playerLife;
          correct = 0;
          cout <<playerLife<<endl;
          
  hangMan();
  printWord();
  checkWin();
  checkLose();

  
  
    }
 
    
    system("PAUSE");
    return 0;

}


void hangMan()
 {
      if (playerLife == 7)
      {
      cout <<"__\n";
      cout <<"  |\n";
      cout <<"  |\n";
      cout <<"  |\n";
      cout <<" _|\n"<<endl;
           }
           else
           if (playerLife == 6)
      {
      cout <<"____  \n";
      cout <<" o  | \n";
      cout <<"    | \n";
      cout <<"    | \n";
      cout <<" ___| \n"<<endl;
           }
           else
           if (playerLife == 5)
      {
      cout <<"____  \n";
      cout <<" o  | \n";
      cout <<" { | \n";
      cout <<"    | \n";
      cout <<" ___| \n"<<endl;
           }
           else
           if (playerLife == 4)
      {
      cout <<"____  \n";
      cout <<" o  | \n";
      cout <<" {} | \n";
      cout <<"    | \n";
      cout <<" ___| \n"<<endl;
           }
           else
           if (playerLife == 3)
      {
      cout <<"____  \n";
      cout <<" o  | \n";
      cout <<"'{} | \n";
      cout <<"    | \n";
      cout <<" ___| \n"<<endl;
           }
           else
           if (playerLife == 2)
      {
      cout <<"____  \n";
      cout <<" o  | \n";
      cout <<"'{}'| \n";
      cout <<"    | \n";
      cout <<" ___| \n"<<endl;
           }
           else
           if (playerLife == 1)
      {
      cout <<"____  \n";
      cout <<" o  | \n";
      cout <<"'{}'| \n";
      cout <<" I  | \n";
      cout <<" ___| \n"<<endl;
           }
           else
           if (playerLife == 0)
      {
      cout <<"____  \n";
      cout <<" o  | \n";
      cout <<"'{}'| \n";
      cout <<" II | \n";
      cout <<" ___| \n"<<endl;
           }
      
      
      
      }

 void printWord()
{
       
       for(int i = 0; i < word.length(); i++)
       {
          if(knownLetters[i] == word[i])
          cout <<word[i];
          else
          cout <<"*";
       }        
  }

void checkWin()
{
     if(knownLetters == word)
     cout <<"Congratulations you WIN!!!"<<endl;
     }
     
void checkLose()
{
     if(playerLife == 0)
     cout <<"You LOST!!"<<endl;
     }
     





Last edited on
You're main problem is that you have not allocated memory for the knownLetters array so it's over-writing bad memory spaces and causing your bug. You also need to work on code formatting to make your code more readable.

Consider using this instead:
1
2
3
4
5
string knownLetters = "";

// Then in main 
word = secretWord;                  //assigns secretWord to word
knownLetters.assign(word.size(), '\0');


It should compile and work with those changes :)
Last edited on
Well, you know that there are 26 letters in the English language, and assuming that this is being run in English... the array would only have to be 26 letters long. So there's no need for having an unallocated array, unless you're allowing the user to enter symbols and such in place of letters. Also, why are you #includ(ing) cmath? You never use any of its specific functions anywhere in the code, so I fail to see why you need it.
Last edited on
That worked! so if im understanding line 5 it is assigning memory to known letters by the size of the word?
@Ispil The number of letters in the alphabet isn't relevant here. He is using the knownLetters array to check which letters have been placed in the correct location when compared to word. This could in theory be an unlimited length array. The problem here is that the OP simply forgot to allocate some memory.

Oh, when placed in the correct location? I thought it was the number of letters guessed in the first place, in which case you're completely right.
Topic archived. No new replies allowed.