hangman with functions prototypes

I'm working out of a book call beginning C++ through game programming and on one of the exercises I'm suppose to remake a hangman program on the last chapter but this time using function prototypes to get the players guess and another one to see if the player is right. I wrote out the program and everything seem to be working just fine but when I try to run it a compile error shows up if anyone can help me fix it, it would be appreciated.

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

using namespace std;

//adding a global vector so i can get to the words from any function.
vector<string>words;
string used = "";
const string THE_WORD = words[0];
string soFar(THE_WORD.size(), '-');
const int MAX_WRONG = 8;
int wrong = 0;
char guess; 

void playerGuess()
{
	cout << "\t\tWelcome to hangman v2\n\n";
	cout << "You have " << MAX_WRONG - wrong << " guesses left\n\n";
	cout << "Letters used: " << used << endl << endl;
	cout << "So far the word is:\n";
	cout << soFar << endl << endl;
	cout << "Enter guess:\n";
	cin >> guess;
	guess = toupper(guess); //turn the letters to the upper case.
}

void right()
{
	while (used.find(guess) != string::npos)
	{
		cout << "\nYou have already entered " << guess << endl;
		cout << "Enter guess:\n";
		cin >> guess;
		guess = toupper(guess);
	}

	used += guess;

	if (THE_WORD.find(guess) != string::npos)
	{
		cout << "That's right " << guess << " is in the word.\n";

		//updating the word
		for (int i = 0; i < THE_WORD.length(); ++i)
		{
			if (THE_WORD[i] == guess)
			{
			   soFar[i] = guess;
			}
		}
	}

	else 
	{
		cout << "Sorry " << guess << " is not in the word.\n";
		wrong++;
	}

	//stopping the game.
	if (wrong == MAX_WRONG)
	{
		cout << "You have been hanged\n";
	}

	else 
	{
		cout << "That's right the word was " << THE_WORD << endl;

	}
}

int main()
{
	words.push_back("TERRENCE");
	words.push_back("HOUSE");
	words.push_back("ZOO");
	words.push_back("WORK");
	words.push_back("JOBS");
	words.push_back("MONEY");
	
	srand(static_cast<unsigned int>(time(0)));
	random_shuffle(words.begin(), words.end());

	while (MAX_WRONG != wrong && soFar != THE_WORD)
	{
		playerGuess();
		right();
	}
	
	return 0;
}
Look at Line 11 and then Line 13.

You declared a vector at Line 11. You did not add any elements to the container. Then, at Line 13, you are trying to access the first element from an empty vector.

I'd try fixing that first.
Last edited on
Topic archived. No new replies allowed.