Help - Hang Man Problem

Please Have A Look At My Code

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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292

#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdlib.h> // rand() Function
#include <time.h>   // Initialize rand()
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
using namespace std;

class hang
{
public:								 
	char len[101];					 // Asterix Word
	int i, n, rem, lngth, chk, diff; // All Used In Different Operations
	char ch;						 // User's Character
	char w[101], hint[51], z;		 // w Is The Random Word, z Is For Converting ch To Upper Character
	bool over, corr;			     // corr to check for Correct Ans
	int plmove;                      // Alt. Turns
	int movech()
	{
		if(plmove == 1)
		{
			plmove = 2;
			return plmove;
		}
		else
		{
			plmove = 1;
			return plmove;
		}
	}
	void get_word(char name[])
	{
		cout << name << ", Enter Word: ";
		gets_s(w);
		lngth = strlen(w);
		for(i = 0; i < (strlen(w)); i++)
		{
			w[i] = (toupper(w[i]));
		}
	}
	void get_hint()
	{
		cout << "Enter Hint For Word: ";
		gets_s(hint);
		for(i = 0; i < (strlen(hint)); i++)
		{
			hint[i] = toupper(hint[i]);
		}
	}
	void assign()
	{
		len[lngth] = ' ';
		len[lngth + 1] = ' ';
		for (i = 0; i < lngth; i++)
		{
			if(diff == 1)
			{
				switch (w[i])
				{
				case 'A':
					len[i] = 'A';
					n++;
					break;
				case 'E':
					len[i] = 'E';
					n++;
					break;
				case 'I':
					len[i] = 'I';
					n++;
					break;
				case 'O':
					len[i] = 'O';
					n++;
					break;
				case 'U':
					len[i] = 'U';
					n++;
					break;
				case ' ':
					len[i] = ' ';
					n++;
					break;
				default:
					len[i] = '*';
				}
			}
			else if(diff == 2)
			{
				switch (w[i])
				{
				case 'A':
					len[i] = 'A';
					n++;
					break;
				case 'O':
					len[i] = 'O';
					n++;
					break;
				case ' ':
					len[i] = ' ';
					n++;
					break;
				default:
					len[i] = '*';
				}
			}
			else if(diff == 3)
			{
				len[i] = '*';
				if(w[i] == ' ')
				{
					len[i] = ' ';
					n++;
				}
			}
		}
	}
	char entry(char name[])
	{
		cout << "\n\n" << name <<  " Has " << rem << " Chance(s) Remaining" << endl;
		cout << "Enter The Character: ";
		cin >> ch;
		z = (toupper(ch));
		return z;
	}
	void check()
	{
		corr = false;
		for(i = 0; i < lngth; i++)
		{
			if(z == w[i])
			{
				corr = true;
				len[i] = w[i];
				n++;
			}
		}
		if(!corr)
		{
			rem--;
			len[lngth + chk] = z;
			chk++;
		}
	}
	void comp(char name[], char name1[])
	{
		if(n == lngth)
		{
			system("cls");
			for (i = 0; i < lngth; i++)
			{
				cout << len[i];
			}
			over = true;
			cout << "\n\n" << name << " Wins";
		}
		else if(rem == 0)
		{
			system("cls");
			cout << name1 << " Wins";
			cout << "\n\nThe Word Was ";
			for (i = 0; i < lngth; i++)
			{
				cout << w[i];
			}
			over = true;
		}
	}
	void reset()
	{
		for(i = 0; i < 101; i++)
		{
			w[i] = '\0';
		}
		for(i = 0; i < 101; i++)
		{
			len[i] = '\0';
		}
		for(i = 0; i < 51; i++)
		{
			hint[i] = '\0';
		}
		over = false;
		rem = 7;
		n = 0;
		chk = 2;
	}
};

void main()
{
	int i;
	char c = 'y';
	char name1[21], name2[21];
	hang x;
	x.lngth = 0;
	system("cls");
	cout << "Made By Yash Gupta";
	cout << "\n\nWelcome To Hang-Man" << endl;
	cout << "You Have 7 Chances To Guess The Word";
	cout << "\nIncorrect Guesses Take Away 1 Chance And Appear On The Right Of The Word";
	cout << "\n\nPress ENTER";
	_getch();
	system("cls");
	cout << "Player I, Name : ";
	gets_s(name1);
	name1[0] = toupper(name1[0]);
	cout << "Player II, Name: ";
	gets_s(name2);
	name2[0] = toupper(name2[0]);
	x.plmove = 1;
	while(c == 'y')
	{
		system("cls");
		x.reset();
		if(x.plmove == 1)
		{
			x.get_word(name1);
		}
		else if(x.plmove == 2)
		{
			x.get_word(name2);
		}
		x.get_hint();
		system("cls");
		cout << "The Length Of The Word Is: " << x.lngth;
		cout << "\nHint: " << x.hint;
		cout << "\n\nChose Difficulty";
		cout << "\n\n1. Easy --------- All Vowels Are Done\n2. Intermediate - 'A' And 'O' Are Done\n3. Hard --------- Complete On You Own\n4. Random\n" << endl;
		cin >> x.diff;
		if(x.diff == 4)
		{
			srand(unsigned(time(0)));
			x.diff = rand() % 3 + 1;
			if(x.diff == 1)
			{
				system("cls");
				cout << "Easy - All Vowels Are Done";
				cout << "\nPress ENTER";
				_getch();
			}
			else if(x.diff == 2)
			{
				system("cls");
				cout << "Intermediate - 'A' And 'O' Are Done";
				cout << "\nPress ENTER";
				_getch();
			}
			else if(x.diff == 3)
			{
				system("cls");
				cout << "Hard - Complete On Your Own";
				cout << "\nPress ENTER";
				_getch();
			}
		}
		x.assign();
		do
		{
			system("cls");
			for(i = 0; i < (x.lngth + x.chk); i++)
			{
				cout << x.len[i];
			}
			if(x.plmove == 1)
			{
				x.entry(name2);
			}
			else if(x.plmove == 2)
			{
				x.entry(name1);
			}
			cout << endl;
			x.check();
			if(x.plmove == 1)
			{
				x.comp(name2, name1);
			}
			else if(x.plmove == 2)
			{
				x.comp(name1, name2);
			}
		} while(!x.over);
		cout << "\n\nPlay Again y/n: ";
		cin >> c;
		x.movech();
	}
}

the code compiles perfectly in visual studio 2012

The First Time Around It Works Perfectly, But When the loop Executes The program Again, It Skips Just Taking The Word From The User And Goes To Take The Hint. As A Result, The Program Fails As There Is No Word.

I am just a beginner so please dont post solutions involving pointers, constructors,etc. Another Thing, Is This in any way "GOOD PROGRAMMING". I know that we should not use the gets_s function but i have tried cin.get() and cin.getline() and i get the same results always.
Thanks In Advance
Last edited on
the problem is that >> (like cin >> c;) leaves the 'end of line' ('\n') in the stream.
if gets_s() is called later it takes that end of line and returns an empty string.

Use ignore() after >> with '\n' to solve that problem.

http://www.cplusplus.com/reference/istream/basic_istream/ignore/
@coder777

Why does cout.flush() also work? I have to use it after using cout without calling endl, or else it has the tendency to take whatever is in the buffer and use it for input.

I know it works, because I see it in the results, but I have no clue why.
Last edited on
@coder777

Thank You So Much Man, It Works Now
Can I Ask You To Please Explain It Out As I Want To Learn More About The Use Of This Function (like i said i'm a beginner so i have implemented it but cant understand what the function does)

thanks again.
@IWishIKnew

Why does cout.flush() also work? I have to use it after using cout without calling endl, or else it has the tendency to take whatever is in the buffer and use it for input.
What do you mean? cin and cout are completely different objects. They're not supposed to interfere.

endl calls flush() internally:

http://www.cplusplus.com/reference/ostream/endl/?kw=endl
Inserts a new-line character and flushes the stream.



@Yash8976

Did you read the link? I think it's a good explanation? Where is the problem?
@coder177

Like i said i understood the implementation in my program, what i couldn't understood is the USE of it.
Anyhow I've got it now after more usage of it
thanks man
@coder: That's why I'm asking, lol. It works... I am on windows 8 though, and some of the STL functions just don't work, or they work differently. For example: isalpha() will return true for characters that aren't even letters... After I wrote my own bool function my program worked fine.

Although the cout and cin objects have nothing to do with each other, I use cout.fush() whenever I don't use endl and it shows the same result as your solution.
Last edited on
Topic archived. No new replies allowed.