Question
Create a Program in C++ We need commentaries in the program setter and getter we need the Output of the program and the code compiling and use a file.
You will design a program that plays hangman. The rules are explained here. The program must at least include one class and c
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

const string words[10] = {"cat","pen","pop","can","pan","rat","mat","bat","pin","bun"};

public :

Hangman();

void guessWord();

int indx;

};

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=8;

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<<"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:

C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\HangmanClass.exe (Guess) Enter a letter in word ***>m No of gueses Remaining :6 Gu

==================================Thank You

Add a comment
Know the answer?
Add Answer to:
Create a Program in C++ We need commentaries in the program setter and getter we need...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Hangman Game in C++ using Classes. The program doesn't need to have the hangman...

    Create a Hangman Game in C++ using Classes. The program doesn't need to have the hangman drawing, it just needs the following: - Ten 3-letter words - Keep track of everytime the player misses a letter - Simple and commented code

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • Hello I am confused about this C++ program. I need to create a payroll C++ program...

    Hello I am confused about this C++ program. I need to create a payroll C++ program following these steps. Source file structure Your program will consist of three source files: main.cpp the main program Payroll.hppclass declaration for the Payroll class. Make sure you have an include guard. Payroll.cpp Payroll's member functions Class definition Create a Payroll class definition. The class has private members which originate from user input: number of hours worked hourly rate float float - and private members...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • Hi, I need to write a program for linux (putty) and please read carefully (I've asked...

    Hi, I need to write a program for linux (putty) and please read carefully (I've asked the same question 3 times because the experts who answered my questions wrote their own carmain1.cpp file. Please leave it where it is and only create car.cpp and car.h files.) Here's carmain1.cpp /* carmain1.cpp * Please don't rewrite this file * I will be using the exact * same file below when grading * */ #include <iostream> #include "car.h" using namespace std; int main()...

  • DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to...

    DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...

  • I need help doing all these questions (1-6) ASAP. Keep in mind we are only on...

    I need help doing all these questions (1-6) ASAP. Keep in mind we are only on chapter 5 of "Building Java Programs" so too advanced code cannot be used. Please use the appropriate code coved in the chapter and the chapters before. Thank you.? Preview File Edit View Go Tools Window Help O 2.15 GB 00% Sun Nov 27 4:01:43 PM Q e E Ch.5 Problem Set.pdf e 2 of 2) a Search Ch.5 Problem Set.pdf l. te an interactive...

  • C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header ...

    C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header files given. Then make a main program using Book and Warehouse to read data from book.dat and have functions to list and find book by isbn Objectives: Class Association and operator overloading This project is a continuation from Project 1. The program should accept the same input data file and support the same list and find operations. You will change the implementation of...

  • Once again we will create a program that tells us how many months we need to...

    Once again we will create a program that tells us how many months we need to save to reach a goal. This time however, using recursive functions. The guidelines are the following:  In main, ask the user for the initial amount, the goal and the “monthly amount” (the constant amount of money that will be put aside every month).  Once you have these values you should call your function. Your function should not return anything and it should...

  • Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by...

    Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by implementing Person and Student classes. Once you are sure you can serialize and deserialize and ArrayList of Students to and from a file, move on to building the GUI application. Person: The Person class should implement serializable interface. It contains the following: Person's first name (String) Person's last name (String) Person's id number Person's date of birth (Date) public String toString(): This method method...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT