Hangman game error

the program works perfects but i cant figured out how to make it remember the correct letter the user enter.
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream> // main function
#include <fstream> // read file
#include <time.h> // record time 
#include <string> // for text 

using namespace std;
#define MAX_WORD_SIZE 15	
#define MAX_WORDS 255	

void drawing(int position);
void guessword(string word);
void DrawGallows(int State);
void RunGame();

typedef char String[MAX_WORD_SIZE];
String Words[MAX_WORDS - 1];	

void main()
{
	char Continue = 'Y';	// end game if = to 'N'
	cout<<"Welcome to Hangman . . . Don't lose your head and Good Luck!"<<endl;

	
	// Continue the game as longas the player wants
	while(Continue == 'Y')
	{
		RunGame();	// Run the game
		cout<<endl<<"Do you want to play again (Yes or No)? ";
		cin>>Continue;
		Continue = toupper(Continue);
	}
	// say good bye
	cout<<endl<<"Thanks for playing and Please come again."<<endl;
}
     

void RunGame() 
{ 
 int Word =0;   // This will hold the subscript of our word 
 int Size;   // This will hold the length of our word 
 int State=1;  // This holds the game state 
 int Subscript=0; // This will hold subscripts 
 char Guess[MAX_WORD_SIZE]; // this will hold their current word 
 char Copy[MAX_WORD_SIZE]; // This will hold a copy of the word 
 char Letter;  //This will be their letter guess 
 int Correct=0;  // This is a True/False value deciding if they got a good answer 
 int count = 0;
     
	 ifstream infile;
     infile.open("file.dat");
  
	 while((infile.peek()) != EOF) 
 { 
  // Get the next word and then increment Count 
 infile>>Words[count++];
	 }

	 srand(time(NULL));     //initializes the random number generator
     while (count < 1)
     {
   
      Word = rand()%10 +1;    // rand returns a random integer from 0 to maxInt
      count++;
     } 
     
	for (count=0;count<Word;count++)
  
	 strcpy(Copy,Words[Word]); 
	

     Size = strlen(Words[Word]);
	 cout<<Word<<endl;

 // Create a null terminated string to represent the word as the 
 // player knows it. 
 for(; Subscript < Size; Subscript++) 
 { 
  Guess[Subscript] = '-'; 
 }

 // insert the null character 
 Guess[Subscript] = '\0';

 // Go till the player is hung 
 while(State!=6) 
 { 
  DrawGallows(State); //Draw the gallows 
  cout<<Guess<<endl; // Draw Guess

  cout<<"Guess a letter :"; 
  cin>>Letter;

  // We will use only lower case letters 
  Letter = tolower(Letter); 
  
  // Loop through the word 
  for(Subscript = 0; Subscript < Size; Subscript++) 
  {

   //if the guess is good tell the user and update Guess 
   if(Copy[Subscript] == Letter) 
   { 
    Guess[Subscript] = Letter; 
    Correct = 1; 
    cout<<endl<<"Good Guess!";

    // If guess equals the word they won so exit 
    if(strcmp(Words[Word],Guess) == 0) 
    { 
     cout<<endl<<"Yea, You survived and won!"; 
     return; 
    } 
   } 
  }

  // If they didn't get aletter correct chide the user 
  if(Correct == 0) 
  { 
   cout<<endl<<"Sorry, bad guess!"; 
   State++; 
  }

  Correct = 0; // reset Correct

 }

 DrawGallows(State); //Draw the gallows at end of game 
 //They lost if they are here so tell them the answer. 
 cout<<"The word was : "<<Words[Word]<<endl<<endl;

}

// This will Draw the gallows according to the state 
void DrawGallows(int State) 
{ 
 if(State==6) 
 { 
  // The \\ will translate as '\' because it is a special char 
  cout<<endl<<endl 
   <<"   +----+     "<<endl 
   <<"   |    |     "<<endl 
   <<"   |    O     "<<endl 
   <<"   |   /|\\   "<<endl 
   <<"   |   / \\   "<<endl 
   <<"   |Your Dead "<<endl 
   <<"  ============"<<endl<<endl; 
 } 
 else if(State==5) 
 { 
  cout<<endl<<endl 
   <<"   +----+  "<<endl 
   <<"   |    |  "<<endl 
   <<"   |    O  "<<endl 
   <<"   |   /|\\ "<<endl 
   <<"   |     \\ "<<endl 
   <<"   |       "<<endl 
   <<"  ============"<<endl<<endl; 
 } 
 else if(State==4) 
 { 
  cout<<endl<<endl 
   <<"   +----+  "<<endl 
   <<"   |    |  "<<endl 
   <<"   |    O  "<<endl 
   <<"   |   /|\\ "<<endl 
   <<"   |       "<<endl 
   <<"   |       "<<endl 
   <<"  ============="<<endl<<endl; 
 } 
 else if(State==3) 
 { 
  cout<<endl<<endl 
   <<"   +----+  "<<endl 
   <<"   |    |  "<<endl 
   <<"   |    O  "<<endl 
   <<"   |   /|  "<<endl 
   <<"   |       "<<endl 
   <<"   |       "<<endl 
   <<"  ============="<<endl<<endl; 
 } 
 else if(State==2) 
 { 
  cout<<endl<<endl 
   <<"   +----+  "<<endl 
   <<"   |    |  "<<endl 
   <<"   |    O  "<<endl 
   <<"   |    |  "<<endl 
   <<"   |       "<<endl 
   <<"   |       "<<endl 
   <<"  ============="<<endl<<endl; 
 } 
 else if(State==1) 
 { 
  cout<<endl<<endl 
   <<"   +----+  "<<endl 
   <<"   |    |  "<<endl 
   <<"   |       "<<endl 
   <<"   |       "<<endl 
   <<"   |       "<<endl 
   <<"   |       "<<endl 
   <<"  ============="<<endl<<endl; 
 }

}
Last edited on
make it remember the correct letter the user enter

What do you mean by that? The letter the user entered is stored in Letter (downshifted).

Line 4: You include the <string> header, but don't use it.

Line 15: Why are you declaring your own String type? This is ugly.

Line 16: Best practice is to avoid use of globals.

Line 18: void main () is incorrect. It should always be int main().

Line 58: You should call srand() only once in your game. You're calling it each time runGame is called.

Line 59: What is the point of this loop? Assuming you read at least one word from the file, this loop will never be executed.

Line 66: What is the point of this loop?

Line 76: subscript is initialized to 0 at 42, but it poor practice not to initialize it here.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
ty soo much for pointing out my super big mistakes, now my code works perfectly. ty and love ya lol
Topic archived. No new replies allowed.