Help needed with Hangman game

I am a beginner beginner and have a few issues with this code.

First of all, I don't know how to code the number of words into the random.
The random doesn't work.
And the letterFill is causing errors too.

Please help. :)

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

/* I'm going to attempt to create a hangman game that can be changed to suit the players weekly spelling words.
This game will be aimed at Primary School aged Children.*/

using namespace std;
const int MAX_TRIES = 5;
int letterFill(char, string, string&);

int main()
{
	string name;
	char letter;
	int num_of_wrong_guesses = 0;
	int amount;
	int words;
	string word;
	srand(time(NULL));

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

	// Entering the amount of words to be used.  

	std::cout << "How many words do you have? (Please enter a number between 2 and 10)" << std::endl;
	cin >> amount;

	switch (amount) {

		// Two words
	case '2':
		cout << "\n Please enter 2 words" << endl;
		cin >> words;


		// Three words
	case '3':
		cout << "\n Please enter 3 words" << endl;
		cin >> words;
		break;

		// Four words
	case '4':
		cout << "\n Please enter 4 words" << endl;
		cin >> words;
		break;

		// Five words
	case '5':
		cout << "\n Please enter 5 words" << endl;
		cin >> words;
		break;

		// Six words
	case '6':
		cout << "\n Please enter 6 words" << endl;
		cin >> words;
		break;

		// Seven words
	case '7':
		cout << "\n Please enter 7 words" << endl;
		cin >> words;
		break;

		// Eight words
	case '8':
		cout << "\n Please enter 8 words" << endl;
		cin >> words;
		break;

		// Nine words
	case '9':
		cout << "\n Please enter 9 words" << endl;
		cin >> words;
		break;

		// Ten words
	case '10':
		cout << "\n Please enter 10 words" << endl;
		cin >> words;
		break;

		// Any other value entered
	default:
		cout << "Invalid choice" << endl;
		break;
	}

	cout << "\n Thank you.  Now lets play Hangman!" << endl;

	// instert a code to choose a word at random

	words = rand() % 10 + 1;

	// code for actual gameplay

	string unknown(word.length(), '*');
	cout << "\n Each letter is represented by an asterisk.";
	cout << "\n You have to type only one letter in each try.";
	cout << "\n You have " << MAX_TRIES << " tries to guess your word";
	cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";


	while (num_of_wrong_guesses < MAX_TRIES)
	{
		cout << "\n\n" << unknown;
		cout << "\n Guess a letter: ";
		cin >> letter;

		if (letterFill(letter, word, unknown) == 0)
		{
			cout << endl << "Sorry, that letter isn't there!" << endl;
			num_of_wrong_guesses++;
		}
		else
		{
			cout << endl << "You have found a letter!" << endl;
		}
		cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
		cout << " guesses left." << endl;


		if (word == unknown)
		{
			cout << word << endl;
			cout << "You got it right!";
			break;
		}
	}

	// Game over
	if (num_of_wrong_guesses == MAX_TRIES)
	{
		cout << "\n Sorry, you lose... you've been hanged." << endl;
		cout << "The word was : " << word << endl;
	}

	cin.ignore();
	cin.get();
	return 0;
}
Remove that whole switch, replace it with:

1
2
cout << "\n Please enter " << amount << " words" << endl;
cin >> words;

Lol I've just shortened your code by 60 lines, you're welcome :D
Last edited on
It's for class so I *have* to include the switch part.
well, then I'm sorry you have to atend such a class :\
It's all part of learning!
Topic archived. No new replies allowed.