look the code

Hi. Can someone look this code and say mistakes ?

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
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#include "pp.h"
using namespace std;
///////////////////////////////////////////////////////
class zaidimas {
 public:
  void LoadFile();
  void kartuves(int State);
  void RunGame();

 private:
  typedef char String[MAX_WORD_SIZE];
  String Words[MAX_WORDS - 1];
  char C;
  int Count;

  char Word;
  int Size;
  int State;
  int Subscript;
  char Guess[MAX_WORD_SIZE];
  char Copy[MAX_WORD_SIZE];
  char Letter;
  int Correct;
};
///////////////////////////////////////////////////////
void zaidimas::LoadFile()
{
  char C;
  int Count =0;
  ifstream Datfile;
  Datfile.open("words.dat");

  while((C=Datfile.peek()) !=EOF)
   {
     Datfile>>Words[Count++];

     if(Count > MAX_WORDS - 1)
     {
       cout<<endl<<"Too many words"
       <<MAX_WORDS<<" words."<<endl;

       Count = MAX_WORDS;
       break;
     }
   }

   Count--;
   Datfile.close();
////////////////////////////////////////////////////////
void zaidimas::kartuves(int State)
{
  if(State==6)
  {
		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;
	}


}
///////////////////////////////////////////////////////
void zaidimas::RunGame()
{
  int State=1;
  int Subscript=0;
  int Correct=0;

     // Seed and create a random number
    srand((unsigned)time(NULL));	// use time as a seed
    Word = rand() % Count;

	// kopijuoja zodi
	strcpy(Copy,Words[Word]);

	Size = strlen(Words[Word]); // gaunamas zodio ilgis

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

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

	// Cilklas vyksta, kol zaidejas pakariamas
	while(State!=6)
	{
		kartuves(State);	//piesia kartuves
		cout<<Guess<<endl;	// uzraso spejama zodi

		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++)
		{

			//jei spejimas geras, apie tai informuojamas zaidejas ir spejimas uzrasomas
			if(Copy[Subscript] == Letter)
			{
				Guess[Subscript] = Letter;
				Correct = 1;
				cout<<endl<<"Good Guess!";

				//Jei spejimas = spejamam zodziui, zaidejas laimi ir programa baigia sawo darba.
				if(strcmp(Words[Word],Guess) == 0)
				{
					cout<<endl<<"Yea, You survived and won!";
					return;
				}
			}
		}

		//Jei raides neatspeja.
		if(Correct == 0)
		{
			cout<<endl<<"Sorry, bad guess!";
			State++;
		}

		Correct = 0;	// reset Correct

	}

	kartuves(State);	//Zaidimo pabaigoje nupiesiamos kartuves.
	//Zaidejas pralose, todel jam parodomas spejamas zodis.
	cout<<"The word was : "<<Words[Word]<<endl<<endl;	

}
//////////////////////////////////////////////////////
int main()
{
     SetConsoleTitle("HANKGMAN");
     zaidimas z1, z2;
     cout<<"Hi.LET'S START...!"<<endl;
     z1.LoadFile();
     z2.RunGame();






 system("PAUSE");
}



and pp.h:
1
2
3
4
5
6
#define MAX_WORD_SIZE 15	
#define MAX_WORDS 255



Line 139: strcpy(Copy,Words[Word]);
You should use strncpy(). strcpy doesn't do any checks prior to copy and is subject to buffer overflows :)

Line 205: in English it's "HANGMAN". Not sure if thats what you meant.

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


This concerns me. You should be using the string type.

e.g
1
2
3
4
5
6
7
8
9
#include <string>

using namespace std;

int main() {
 string sHello = "Hello";
 cin >> sHello;
 // etc
}
...and I would suggest changing your includes
1
2
3
#include <iostream.h>
#include <fstream.h>
#include <string.h> 

to
1
2
3
#include <iostream>
#include <fstream>
#include <string> 

Last edited on
thanks...
Topic archived. No new replies allowed.