C++ Mini Project Hang Man
Mini project Hang Man is a mini project in C++. It is done using functions and control statements in C++. It is a simple project made using the console application of C++. This means, no graphics component is added. The main target user of this project is the C++ beginners who want to make the project in C++ and especially those who are interested in learning the Class in C++ language.
The complete source code for the project is given below:
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
|
/**********************************************************************************
Program Name : Hang Man Game
Language Used : C++
***********************************************************************************/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#include <cstring>
usingnamespacestd;
inlinevoidtype_of_word(charf);
intmain()
{ charc,h,ch,ch1,ch2;
charword[25];
charword2[25];
intl,i,ng,n,k,x;
do{
do{
c='\0';
cout<<"\n\t\t *********** HANGMAN IN C++ ***********\n\n";
cout<<"(E) Enter a word\n\n(C) Computer chooses word\n\n(A)"
cout<<"Add new word to list\n\n(Q) Quit\n\n\nEnter your choice (E - C - Q): ";
cin>>ch2;
}while(ch2!='C'&&ch2!='c'&&ch2!='E'&&ch2!='e'&&
ch2!='Q'&&ch2!='q'&&ch2!='A'&&ch2!='a');
if(ch2=='Q'||ch2=='q') exit(0);
if(ch2=='C'||ch2=='c')
{
ifstreamfin("hangword.txt");
if(!fin){
cout<<"File missing, aborting.\n\nYou are missing a file of name"
cout<<"**hangword.txt**\n\nLocate it, then place it next to the"
"program file.\n\n";system("pause");return0;}
for(i=0;!fin.eof();i++) fin.getline(word,25);
fin.close();
do{
x=rand();
}while(x>i||x<0);
ifstreamfinn("hangword.txt");
for(i=0;!finn.eof();i++)
{finn>>c;finn.getline(word,25);if(x==i)break;}
finn.close();
}
if(ch2=='A'||ch2=='a')
{
ofstreamfout("hangword.txt",ios::app);
if(!fout){//clrscr();
cout<<"File missing, aborting.\n\nYou are missing a file of name"
" **hangword.txt**\n\nLocate it, then place it next to the program"
" file.\n\n";system("pause");return0;}
cin.get();
cout<<"Choose the topic of your word\n\n(M) Movie\n\n(A) Animal\n\n(P)"
" Sport\n\n(S) Song\n\nEnter your choice (A-P-S-M) : ";
cin>>h;
cin.get();
//clrscr();
cout<<"\n\nThe word should not exceed 25 letters\n\nEnter the word : ";
cin.getline(word,25);
fout<<h<<word<<endl;
fout.close();
}
if(ch2=='E'||ch2=='e')
{// clrscr();
cin.get();
cout<<"\t\t\t Type the word : ";
cin.getline(word,25);
}
if(ch2=='E'||ch2=='e'||ch2=='C'||ch2=='c')
{
l=strlen(word);
charchoosen[25]="\0";
n=0;k=0;
for(i=0;i<=24;i++)
{
if(word[i]=='\0'){word2[i]='\0';break;}
if(word[i]==' ') {word2[i]=' '; n++;}
if(word[i]!=' ') word2[i]='-';
}
ng=l+2-n; //only 2 guesses extra
do{
there: type_of_word(c);
if(k!=0) cout<<"\n\n\t\t\tChoosen letters : "<<choosen<<"\n";
cout<<"\n\n\n\t\t\t "<<word2<<"\n\n\nYou have "<<ng
<<" guesses left, choose a letter : ";
cin>>ch;cin.get();
for(i=0;i<25;i++)if(choosen[i]==ch){//clrscr();
cout<<"\a\t\t !!You have choosen "<<ch<<" already!!\n";gotothere;}
ng--;choosen[k]=ch;choosen[k+1]=',';k+=2;
for(i=0;i<=24;i++)
if(word[i]==ch||word[i]==ch+32||word[i]==ch-32)word2[i]=ch;
if(!strcmpi(word2,word)){cout<<"\n\t\t\t "<<strupr(word)
<<"\n\n\t\t\tCongratulations :-()\n";break;}
}while(ng>0||!strcmpi(word2,word));
if(strcmpi(word2,word)) cout<<"\nSorry, maybe next time.\n\nThe word was : "
<<strupr(word)<<endl;
}
cout<<"\nWould you like to play again??? (Y - N) : ";
cin>>ch1; cin.get();
}while(ch1=='y'||ch1=='Y');
system("PAUSE");
return0;
}
inlinevoidtype_of_word(charf)
{ if(f=='m')cout<<"\t\t\t\tMOVIE";
if(f=='a')cout<<"\t\t\t\tANIMAL";
if(f=='p')cout<<"\t\t\t\tSPORT";
if(f=='s')cout<<"\t\t\t\tSONG";
}
|

Related Article
destination source:https://www.programming-techniques.com/?p=2402