Beginner of beginners; lost on how to complete this assignment

im trying to code hangman; im familiar with c++ and i understand the content in class but when it comes to completing the assignments im completely lost.

this is it: can someone help me code it out? i completed most of it; my problem is in main()

Create a class Hangman with the following private attributes:
􀁸 Words: should be a static string array with 10 elements.
􀁸 Word Name: should be a string attribute.
􀁸 Chosen Letters: should be a pointer to a Boolean attribute.
􀁸 Letter: should be a character attribute.
􀁸 Number of Mistakes: should be an integer attribute.
􀁸 Word Length: should be an integer attribute.
The class Hangman should contain the following behaviors:
􀁸 Constructor with no parameters:
o Declare and initialize a random number between 0 and 9.
o Assign Word Length to the length of a text inside Words based on the generated random number (Use the length function from the string class).
o Dynamically allocate Chosen Letters based on the Word Length.
o Assign Word Name to the chosen random text from Words.
o Set the Number of Mistakes to 0 and initialize the array Chosen Letters to false.
􀁸 Destructor:
o Dynamically de-allocate Chosen Letters.
􀁸 Constant get function for Number of Mistakes.
􀁸 Check Value function:
o The function should not take any parameters.
o If the value of Letter found within the array Word Name, set the same element index of Chosen Letters to true and return true; otherwise return false.
􀁸 Is Word Complete constant function:
o The function should not take any parameters.
o The function must return false if one element inside Chosen Letters is equal to false; otherwise return true.
􀁸 Overloaded operators (<<, >>, ++):
o operator<< must output the correct letters on screen; otherwise print an underscore “_”.
o operator>> must input the letter chosen by the user into the attribute Letter.
o operator++ (prefix) must increment the value of Number of Mistakes.
A header file and source file must be created for the Hangman class.
Another source file must be created for the main function:
􀁸 Create a dynamic Hangman object.
􀁸 Allow the user to have at most 6 mistakes before he loses the game.
􀁸 The user must input a letter through the object.
􀁸 Check if the letter is found within the word.
􀁸 Print the word on the screen through the object.
􀁸 If the user recognizes the whole word, print the text “Congratulations! You won the game.” otherwise print “Sorry! You lost the game.”
􀁸 Ask the user if he wants to play again by choosing either the character Y for yes or the character N for no.
􀁸 De-allocate the object at the end of the program.
􀁸 Clear the text on the screen using the following line: system(“cls”);
Hello mojoe88,


Create a class Hangman with the following private attributes:
	o Words: should be a static string array with 10 elements.

	o Word Name: should be a string attribute.

	o Chosen Letters: should be a pointer to a Boolean attribute.

	o Letter: should be a character attribute.

	o Number of Mistakes: should be an integer attribute.

	o Word Length: should be an integer attribute.

The class Hangman should contain the following behaviors:
	o Constructor with no parameters:

	o Declare and initialize a random number between 0 and 9.

	o Assign Word Length to the length of a text inside Words based on the generated random number (Use the length function from the string class).

	o Dynamically allocate Chosen Letters based on the Word Length.

	o Assign Word Name to the chosen random text from Words.

	o Set the Number of Mistakes to 0 and initialize the array Chosen Letters to false.

	o Destructor:

	o Dynamically de-allocate Chosen Letters.

	o Constant get function for Number of Mistakes.

	o Check Value function:
	o The function should not take any parameters.
	o If the value of Letter found within the array Word Name, set the same element index of Chosen Letters to true and return true; otherwise return false.

	o Is Word Complete constant function:

	o The function should not take any parameters.

	o The function must return false if one element inside Chosen Letters is equal to false; otherwise return true.

	o Overloaded operators (<<, >>, ++):

	o operator<< must output the correct letters on screen; otherwise print an underscore “_”.

	o operator>> must input the letter chosen by the user into the attribute Letter.

	o operator++ (prefix) must increment the value of Number of Mistakes.

A header file and source file must be created for the Hangman class.

Another source file must be created for the main function:
	o Create a dynamic Hangman object.

	o Allow the user to have at most 6 mistakes before he loses the game.

	o The user must input a letter through the object.

	o Check if the letter is found within the word.

	o Print the word on the screen through the object.

	o If the user recognizes the whole word, print the text “Congratulations! You won the game.” otherwise print “Sorry! You lost the game.”

	o Ask the user if he wants to play again by choosing either the character Y for yes or the character N for no.

	o De-allocate the object at the end of the program.

	o Clear the text on the screen using the following line: system(“cls”);


By breaking up what you had it makes it easier to work on in small steps.

It looks like most of the work is done through the class object, so I would start with that. Get something started. You can always add to it later.

The assignment looks like you will be using pointers and dynamic memory allocation.

The ctor may be empty, but the dtor will need to delete the memory that you create through the class.

Write some code and post it. If you have a problem say what it is.

Hope that helps,

Andy
i completed most of it; my problem is in main()

Please post the code that you have so we can help you with it.
thanks for the feedback!

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
#ifndef HANGMAN_H
#define HANGMAN_H

#include <iostream>
#include <string>
using namespace std;

class Hangman
{
	friend ostream &operator<<(ostream &, const Hangman &);
	friend istream &operator>>(istream &,  Hangman &);

private:
	static string Words[10];
	string wordName;
	bool *chosenLetters;
	char Letter;
	int mistakeCount;
	int wordLen;

public:
	Hangman();
	~Hangman();
	int getNumberOfMistakes() const;
	bool checkValue();
	bool IsWordComplete() const;
	Hangman &operator++();

};

#endif

#include "HangMan.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

Hangman::Hangman()
{
	srand(time(0));
	int number = rand() % 9;
	this->wordLen = size(Words[number]);
	this->chosenLetters = new bool[this->wordLen];
	this->wordName = Words[number];
	this->mistakeCount = 0;
	this->chosenLetters = false;
}

Hangman::~Hangman()
{
	delete [] this->chosenLetters;
}

int Hangman::getNumberOfMistakes() const
{
	return this->mistakeCount;
}

bool Hangman::checkValue()
{
	for (int i = 0; i < wordLen; i++)
	{
		if (this->wordName[i] == Letter)
		{	chosenLetters[i] = true;
		return true;}
		else
			return false;
	}
}

bool Hangman::IsWordComplete() const
{
	for (int i = 0; i < wordLen; i++)
	{
		if (this->chosenLetters[i] == false)
			return false;
		else
			return true;
	}
}

ostream &operator<<(ostream & output, const Hangman & s)
{
	for (int i = 0; i < s.wordLen; i++)
		output << " _ ";

	if (s.checkValue = true)
		output << " " << s.Letter;
	else
		output << " _";

	return output;
}

istream &operator>>(istream & input, Hangman & s)
{
	input >> s.Letter;

	return input;
}

Hangman &Hangman::operator++()
{
	if (!checkValue)
		++mistakeCount;
}

#include "HangMan.h"
#include <iostream>
#include <string>
using namespace std;

void main()
{
	Hangman *h1 = new Hangman;
	int mistakes = 6;
	char letter;
	while (mistakes <= 6)
	{

		cout << "Enter Letter: ";
		cin >> letter;
		if(h1->checkValue)
	}
}
Last edited on
Hello mojoe88,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



In main it is int main() not void main() as "main" will return an "int" whether you tell it to or not.

The while loop is an endless loop because "mistakes" starts at six and never changes. I am thinking more like this:

1
2
3
4
5
6
constexpr int MAX_MISTAKES{ 6 };

while (h1->GetMistakes() < MAX_MISTAKES)
{

}

Because the class is where you keep track of the mistakes.

Your "cout" and "cin" need to be done through the class object.

The if statement is OK except that that "checkValue" needs to end with a set of ()s.

Given the code:
1
2
3
4
class Hangmen
{

};

Everything between the {}s is private to start with. The use of "public:" or "protected:" will change that. I would move the first two lines to the bottom of the public section where I see them most often.

You also need to overload the "++" operator.

Hope that helps,

Andy
hello Handy Andy,

that was very helpful, thanks.

The professor likes us to use void for main; I'm going to keep it void to avoid losing any points over silly things.

The mistakes remark was accurate.

best,
mj
your professor is forcing you to do something the wrong way, which reflects on his ability and quality and taking points off for doing it right is a serious indication of a problem. You may want to avoid him in future classes if possible, if not, just be aware that he may not know c++ very well.
Topic archived. No new replies allowed.