Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Hangman
{
private :
//Declare and string array
static const string words[10];
public :
Hangman();
void guessWord();
int indx;
};
const string Hangman::words[10] =
{"cat","pen","pop","can","pan","rat","mat","bat","pin","bun"};
Hangman::Hangman()
{
}
//This function will ask the user to guess a word
void Hangman::guessWord()
{
int seedVal = 0;
// t is a 'time_t' type variable
time_t t;
seedVal = (unsigned)time(&t);
srand(seedVal);
//Generating a random number between 0-25
this->indx= rand() % (10);
string word=words[indx];
char ch;
const int NO_OF_MISSES=7;
int miss = 0,flag;
int len = word.length();
char arr[len];
for (int i = 0; i < len; i++)
{
arr[i] = '*';
}
while (len != 0)
{
flag=0;
cout << "(Guess) Enter a letter in word ";
for (int i = 0; i < word.length(); i++)
{
cout << arr[i];
}
cout << ">";
cin >> ch;
for (int i = 0; i < word.length(); i++)
{
if (word[i] == ch)
{
arr[i] = ch;
len--;
flag=1;
}
}
if(flag==0)
{
miss++;
cout<<"No of gueses Remaining
:"<<NO_OF_MISSES-miss<<endl;
if(miss==NO_OF_MISSES)
{
cout<<"You Lost the game."<<endl;
cout<<"The Word is :"<<word<<endl;
break;
}
}
if (len == 0)
{
cout<<"\nThe word is
:"<<word<<endl;
cout<<"You won the game."<<endl;
}
}
}
int main()
{
//Declaring variables
string word;
char ch;
Hangman h;
/* This while loop continues to execute
* until the user enters a 'y' or 'Y'
*/
while (true)
{
//calling a function
h.guessWord();
//Prompting the user to run again
cout << "\nDo you Want to continue(Y/N):";
cin >> ch;
if (ch == 'y' || ch == 'Y')
{
continue;
}
else
{
break;
}
}
return 0;
}
================================================
Output:
=====================Could you plz rate me well.Thank
You
Get Answers For Free
Most questions answered within 1 hours.