Question

I have this program using class that plays the game hangman the only thing that I...

I have this program using class that plays the game hangman the only thing that I need to make is a file or a function that will store the 10 words ( hey, ace, cow, fix, fly, fun, ice, max, new, sam) when the user starts the game the words most be chosen randomly not in any order

C++ code:

// Header of the class
#ifndef Hangman_H
#define Hangman_H
#include <string>
#include <iostream>
#include <string.h>
using namespace std;
class Hangman
{
private:
   char letter;
   string word;
public:
   Hangman();
   Hangman(char);
   ~Hangman();
   void setLetter(char);
   char getLetter();
   void setWord();
   string getWord();
   void compare();

};
#endif

// Definition of the class

#include "Hangman.h"
Hangman::Hangman()
{
   letter = '\0';
   word = "";
}
Hangman::Hangman(char l)
{
   letter = l;
}
Hangman::~Hangman()
{
   delete &letter;
   delete &word;
}
void Hangman::setLetter(char l)
{
   letter = l;
}
char Hangman::getLetter()
{
   return letter;
}
void Hangman::setWord()
{
   word = "hey";
}
string Hangman::getWord()
{
   return word;
}
void Hangman::compare()
{
   string w;
   int i = 8;
   char w1[4], l, y[4] = { '_','_','_' };
   setWord();
   w = getWord();
   strcpy_s(w1, w.c_str());
   do
   {
       cout << "Enter a letter: ";
       cin >> l;
       setLetter(l);
       if (letter == w1[0])
       {
           y[0] = letter;
           cout << y << endl;
       }
       else if (letter == w1[1])
       {
           y[1] = letter;
           cout << y << endl;
       }
       else if (letter == w1[2])
       {
           y[2] = letter;
           cout << y << endl;
       }
       else
       {
           i--;
           cout << "Letter does not match!\nChances left: " << i << endl;
       }
       if (y[0] == w1[0] && y[1] == w1[1] && y[2] == w1[2])
           break;
   } while (i > 0);
   if (i > 0)
       cout << "Congrats on the win!";
   else
       cout << "You lost";
}

// Main

#include "Hangman.h"
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
   int x;
   Hangman hM;
   do
   {
       cout << "Welcome to Hangman!\n";
       cout << "The game is handled from within the class, so have fun!\n";
       hM.compare();
       cout << "Would you like to play again?(Enter 1 for yes and 0 for no): ";
       cin >> x;
   } while (x != 0);

   system("pause");
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Hangman.h

#ifndef Hangman_H

#define Hangman_H

#include <string>

#include <iostream>

#include <string.h>

#include <cstdlib>

using namespace std;

class Hangman

{

private:

   char letter;

   string word;

   static string words[] ; // create a static string array to store the words

public:

   Hangman();

   Hangman(char);

   ~Hangman();

   void setLetter(char);

   char getLetter();

   void setWord();

   string getWord();

   void compare();

};

#endif

//end of Hangman.h

//Hangman.cpp

#include "Hangman.h"

// initialize the array of words

string Hangman::words[] = {"hey","ace","cow","fix","fly","fun","ice","max","new","sam"};

Hangman::Hangman()

{

   letter = '\0';

   word = "";

}

Hangman::Hangman(char l)

{

   letter = l;

}

Hangman::~Hangman()

{

// no need to delete since they are allocated statically and not dynamically

// delete &letter;

// delete &word;

}

void Hangman::setLetter(char l)

{

   letter = l;

}

char Hangman::getLetter()

{

   return letter;

}

void Hangman::setWord()

{

               int n = rand()%10; // randomly select a word from the array and set word to selected one

               word = words[n];

}

string Hangman::getWord()

{

   return word;

}

void Hangman::compare()

{

   string w;

   int i = 8;

   char w1[4], l, y[4] = { '_','_','_' };

   setWord();

   w = getWord();

   strcpy_s(w1, w.c_str());

   do

   {

       cout << "Enter a letter: ";

       cin >> l;

       setLetter(l);

       if (letter == w1[0])

       {

           y[0] = letter;

           cout << y << endl;

       }

       else if (letter == w1[1])

       {

           y[1] = letter;

           cout << y << endl;

       }

       else if (letter == w1[2])

       {

           y[2] = letter;

           cout << y << endl;

       }

       else

       {

           i--;

           cout << "Letter does not match!\nChances left: " << i << endl;

       }

       if (y[0] == w1[0] && y[1] == w1[1] && y[2] == w1[2])

           break;

   } while (i > 0);

   if (i > 0)

       cout << "Congrats on the win!";

   else

       cout << "You lost. ";

}

//end of Hangman.cpp

// main.cpp

#include "Hangman.h"

#include <string.h>

#include <iostream>

#include <ctime>

using namespace std;

int main()

{

               srand(time(NULL));

   int x;

   Hangman hM;

   do

   {

       cout << "Welcome to Hangman!\n";

       cout << "The game is handled from within the class, so have fun!\n";

       hM.compare();

       cout << "Would you like to play again?(Enter 1 for yes and 0 for no): ";

       cin >> x;

   } while (x != 0);

   system("pause");

   return 0;

}

//end of main.cpp

Add a comment
Know the answer?
Add Answer to:
I have this program using class that plays the game hangman the only thing that I...
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
  • c++. I'm working on a hangman game. It has to utilize a class. It should show...

    c++. I'm working on a hangman game. It has to utilize a class. It should show the number of attempts that have been made, the length of the word represented by dots, and then the alphabet. There also needs to be input validation. If the word was horse, the very first attempt at the user guessing should display " 1 ..... choose: abcdefghijklmnopqrstuvwxyz ". As the user guesses letters, the number of attempts should go up, the letter should be...

  • I have a queue and stack class program that deals with a palindrome, I need someone...

    I have a queue and stack class program that deals with a palindrome, I need someone to help to put in templates then rerun the code. I'd greatly appreciate it. It's in C++. Here is my program: #include<iostream> #include<list> #include<iterator> #include<string> using namespace std; class Queue { public: list <char> queue; Queue() { list <char> queue; } void Push(char item) { queue.push_back(item); } char pop() { char first = queue.front(); queue.pop_front(); return first; } bool is_empty() { if(queue.empty()) { return...

  • Hangman using while and fo loops, if's, and functions.

    I would like to preface this by saying this is NOT a current homework assignment; but it is an assignment from 3 years ago before I dropped out of school. I am self teaching and am revisiting an old assignment. I am NOT asking for the entire program, I'm simply looking for help building the skeleton for the initial start of the game.MORE INFO: Player 1 will enter word(of any length / i have been using "Testing") for Player 2...

  • hey dear i just need help with update my code i have the hangman program i...

    hey dear i just need help with update my code i have the hangman program i just want to draw the body of hang man when the player lose every attempt program is going to draw the body of hang man until the player lose all his/her all attempts and hangman be hanged and show in the display you can write the program in java language: this is the code bellow: import java.util.Random; import java.util.Scanner; public class Hangmann { //...

  • Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The...

    Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The game requires the user to guess a secret word. The game does not end until the user guesses the word. After they win the game, they are prompted to choose to play again. The secret word that user must guess is "valentine". It is a case-sensitive analysis With each guess... If the guess does not have an equal number of characters, tell the user...

  • JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The...

    JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The game chooses a random phrase from the list. This is the phrase the player tries to guess. The phrase is hidden-- all letters are replaced with asterisks. Spaces and punctuation are left unhidden. So if the phrase is "Joe Programmer", the initial partially hidden phrase is: *** ********** The user guesses a letter. All occurrences of the letter in the phrase are replaced in...

  • I created a struct for Car and now I need to turn it into a class...

    I created a struct for Car and now I need to turn it into a class for Car. Below is the code that I wrote for the structure. For the class, we are suppose to make the data in the class Car private, revise the input so the input function will only read the data from the user, and then it will call an additional function named setUpCar which will put the data into the object. Not sure how to...

  • C++ Language I have a class named movie which allows a movie object to take the...

    C++ Language I have a class named movie which allows a movie object to take the name, movie and rating of a movie that the user inputs. Everything works fine but when the user enters a space in the movie's name, the next function that is called loops infinetly. I can't find the source of the problem. Down below are my .cpp and .h file for the program. #include <iostream> #include "movie.h" using namespace std; movie::movie() { movieName = "Not...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

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