Hangman Game

Hi, I am relatively new with C++ and was wondering if someone could help me make a program. I would like to make a hangman game that allows the user to enter a secret word (all capitals or lowercase) and a guess. I would like the program to produce x amount of underscores, where x is the length of the word. The user should be able to guess incorrectly a maximum of 5 times. Also it would be great if each underscore is replaced with the letter from the guess if the letters are the same.

I know people aren't usually to keen of creating programs for free, but I imagine that this little game wouldn't take long for an experienced programmer. So if you have extra time to create a game any help is greatly appreciated.

Here is an example of what I would like the end program to look like-

Example:
Enter the secret word (all capitals or lowercase): hangman
_ _ _ _ _ _ _ 7 amount of characters based on the word
You have 5 tries left:
Guess:h
h _ _ _ _ _ _
You have 5 tries left:
Guess: t
You have 4 tries left
h _ _ _ _ _ _
Guess:a
h a _ _ _ a _
You have 4 tries left:
Last edited on
this forum has an example with room to add more words

http://www.cplusplus.com/forum/beginner/112286/
Here is a simple hangman game I just created.

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
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

const int TRIES = 5;

int main() {
	while (1) {
		system("cls");

		cout << "Enter the secret word (all capitals or lowercase): ";

		string word;
		getline(cin, word);

		int word_size = word.size();
		string word_hidden(word_size, '_');

		string word_lowercase = word;
		transform(word.begin(), word.end(), word_lowercase.begin(), ::tolower);

		for (int i = TRIES; i > 0; i--) {
			cout << "You have " << i << " tries left!" << endl;

			string char_try;

			cout << "Guess: ";
			getline(cin, char_try);

			if (char_try.size() > 1) {
				if (char_try == word_lowercase) {
					cout << endl << char_try << " is right! You win!" << endl;
					break;
				}
				else cout << char_try << " is not the right word!" << endl;
			}
			else {
				bool guessed_right = false;

				for (int j = 0; j < word_size; j++) {
					if (char_try[0] == word_lowercase[j]) {
						word_hidden[j] = word[j];
						guessed_right = true;
					}
					else if (word_lowercase[j] == ' ') word_hidden[j] = ' ';
				}

				if (guessed_right == true) i++;

				if (!(word_hidden.find('_') + 1)) {
					cout << endl << "You successfully guessed all the characters of the word!" << endl;
					break;
				}
			}

			cout << word_hidden << endl;
		}

		if ((word_hidden.find('_') + 1)) cout << endl << "You failed to guess the word!" << endl;

		cout << endl << "The word is: " << word << endl << endl;

		system("pause");
	}
}
Last edited on
this little game wouldn't take long for an experienced programmer

You are correct; it is pretty simple. However, this not a free homework site. If you want someone to do it, then you must offer some type of monetary value. Do you really think the experience programmers will stop what they are doing to do a simple homework assignment? You placed your post in the right place. Be willing to pay and I am sure someone will be more than happy to do it. Just like you, we have a life and we do not sit behind keyboards hoping to do homework for free (regardless of how simple do you think it is).
It's my first time here. But isn't this section for jobs? Aren't you're supposed to offer a job or ask one at the very least. I don't think this is a place to ask for help for homework...
Last edited on
Topic archived. No new replies allowed.