Any advice on the following program?

I've just made a simple hangman game and I'd like to know if there's any where I can improve on this.
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
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<fstream.h>
#include<stdlib.h>
#define CHANCES 8
int is_entered(char ch,char *chk)
{
	int check=0;
	for(int k=0;chk[k]!='\0';k++)
	{
		if(ch==chk[k])
		{
			check=1;
			break;
		}
	}
	return check;
}
void getstring(char* str)
{
	ifstream ifil("myfil.txt");
	int traverse;
	//char str[30];
	srand((unsigned)(time(0)));
	traverse=(rand()%20)+1;
	for(int i=0;i<traverse;i++)
	ifil.getline(str,20,'\n');
	ifil.close();
}
void str_assign(char *str,int x)
{
	for(int i=0;i<x;i++)
	{
		str[i]='_';
		str[i+1]='\0';
	}
}
void main()
{
	int hang=0,cor,ttl=0;
	char str[30],ch,str2[30];
	getstring(str);
	clrscr();
	int fil_count=strlen(str);
	clrscr();
	cout<<"Guess the word:\n";
	str_assign(str2,fil_count);
	for(int g=0;g<strlen(str);g++)
	cout<<'_';
	cor=0;
	while(fil_count!=0 && hang<CHANCES)
	{
		cout<<"\nEnter a character:\n";
		cin>>ch;
		ch=tolower(ch);
		if(is_entered(ch,str2))
			{
				cout<<"Character already entered.\n";
				cor--;
				continue;
			}
		cor=0;
		for(int i=0;str[i]!='\0';i++)
		{
		  if(ch==str[i])
			{
				cout<<str[i];
				cor++;
				str2[i]=str[i];
			}
			else
			cout<<str2[i];
		}
		ttl+=cor;
		if(cor!=0)
		{
			cout<<"\nCorrect guess!\n"<<endl;
			cout<<"The number of letters found are:"<<ttl;
			fil_count-=cor;
		}
		else
		{
			cout<<"Wrong guess.\n"<<endl;
			hang++;
			int chance=CHANCES-hang;
			cout<<"Chances left until you are 

hung:\n"<<chance<<endl;
		}
	}
	if(hang==CHANCES)
	{
		cout<<"\nYou lost the game.";
		cout<<"The word was "<<str;
	}
	else
	cout<<"\nYou guessed the word correctly. Congratulations!";
	getch();
}

PS:I've used the turbo c++ compiler for this.
It's possible you could make some optimizations, but your program is a decent size for what you've done (especially considering your spacing is nice and open).

Were there any problems with your code or did you just want to know if you could optimize it more?
A slight thing: #define is not really as good as const int.
Other than that, purty good.

Good Program.
Only thing I can think of is that main() should be as short as possible so you could take alot of the code inside the main() and create functions. Also add comments, its good practice.
main() should return int.

My advice is to try re-writing the program to use STL strings and containers rather than
char* and arrays.
@jpeg Just wanted to know if there was any room for improvement in my program and if I was doing anything that could be done in an easier way.

@QWERTYman I've never heard that before, but thanks anyway. I'll change it. I would love it if you could tell me the reason behind behind that though.

@umz Yeah I've been thinking of doing that. Actually, I'll go do that right now.

@jsmith I've seen that floating around, using int main() and returning an int, but is there any difference between int main() with a return value and void main() and no return value?
About that stl thing, I have no idea what it is, sorry.

Thanks for the suggestions everybody.
is there any difference between int main() with a return value and void main() and no return value?

Yes. One is standard-compliant and the other isn't. Your choice.
STL = Standard Template Library.

It provides you containers such as set, map, vector, list, etc. as well as a general string class.

I implemented your program using the STL string class to hold the words and an STL set class to hold the characters already guessed.

Refer to

http://www.cplusplus.com/reference/stl/

for information on the STL.
Topic archived. No new replies allowed.