How to establish recurring loop within a return function for Hangman game

Hi, I am trying to establish a game loop for my game where if the player wins or loses a game of Hangman he has the option of playing again. I am having difficulty creating a loop for this to work. Please help if you can. Thanks.


#include <iostream>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <string>
#include <vector>

using namespace std;

void askGuess();
bool match(string word, char letter);
const int MAX_WRONG = 2; // max a player can get wrong
int wrong = 0; //incorrect letters
char restart;

string THE_WORD = ""; // randomized word to guess
string soFar = "";
string used = ""; //guessed letters

int main()
{
vector<string> words; //hangman words
words.push_back("HERO");
words.push_back("SUPERMAN");
words.push_back("PENGUIN");
words.push_back("IPOD");
words.push_back("SUNBURN");
words.push_back("FOLDER");
words.push_back("MONKEY");
words.push_back("CALCULATOR");

srand( static_cast< unsigned int > ( time(0) ));
random_shuffle(words.begin(), words.end());
THE_WORD =words[0];
soFar = THE_WORD;
soFar.assign(THE_WORD.size(), '-');


cout << "\n\t Welcome to another exciting game of Hangman!\n\nSave Professor Wesley from getting hanged by guessing the right word!" << endl;

while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
{

cout << "\nYou've guessed these letters already (" << used << ")" << endl;
cout << "\nWord so far: " << soFar << endl;
cout << "\n\tYou have (" << (MAX_WRONG - wrong) << ") more letters to guess before Professor Wesley gets it!" << endl;
askGuess();
}

if (wrong == MAX_WRONG)
{ system ("cls");
cout << "\nPoor Professor Wesley was hanged!";
cout << "\n\n\tPlay Again? (y/n)";
cin >> restart;
cin.get();
if (restart == 'Y' || restart == 'y')
{
return 0;
}

}
else
{
cout << "You saved Professor Wesley from being hanged!\n";
cin.get();
cout << "\nThe word was " << THE_WORD << endl;
cin.get();

}
return 0;
}

void askGuess()

{
char guess;
cout << "\nEnter your guess: ";
cin >> guess;
guess = toupper(guess);
while (match(used,guess))
{
cout << "You've already guessed " << guess;
cout << "\nEnter your guess: ";
cin >> guess;
guess = toupper(guess);
}
used += guess;
if (match(THE_WORD, guess))
{
system("cls");
cout << "\tSweet! \'" << guess << "\' is in the word.\n";
for (unsigned int i = 0 ; i < THE_WORD.length () ; ++i )
{
if (guess == THE_WORD[i])
soFar[i] = guess;
}
}
else
{
system("cls");
cout << "\tNope! There is no \'" << guess << "\' in the word.\n";
++wrong;
cin.get();
}
}

bool match(string word, char letter)
{
return (wohttp://www.cplusplus.com/forum/post.cgi?w=new&i=851#rd.find(letter) != string::npos);
}
Something like this maybe?:

1
2
3
4
5
6
7
8
9
10
11
12

char play_again = 'y';

while(play_again == 'y' || play_again == 'Y')
{
    // ... do hangman game

    std::cout << "Do you want to play again? ";
    std::cin >> play_again;
    std::cin.get(); // bypass return key char
}
Last edited on
It keeps saying 'unqualified-id before 'while'

not sure what that means and how to fix it. thanks for the response galik btw.
It works fine for me. Can you post the new code? Please put the code in between the tags: [code] ... [/code]
Last edited on
Okay galik, i basically copied and pasted what you wrote into my code, but i seem to retrieve the same error. This assignment is due in the next hour, hopefully we can make something happen by then :). Thanks man.

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

#include <iostream>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <string>
#include <vector>

using namespace std;

void askGuess();
bool match(string word, char letter);
const int MAX_WRONG = 6; // max a player can get wrong
int wrong = 0; //incorrect letters
char restart;
char play_again = 'y';

string THE_WORD = ""; // randomized word to guess
string soFar = "";
string used = ""; //guessed letters



while(play_again == 'y' || play_again == 'Y')
{


int main()
{
	vector<string> words; //hangman words
	words.push_back("HERO");
	words.push_back("SUPERMAN");
	words.push_back("PENGUIN");
	words.push_back("IPOD");
	words.push_back("SUNBURN");
	words.push_back("FOLDER");
	words.push_back("MONKEY");
	words.push_back("CALCULATOR");

	srand( static_cast< unsigned int > ( time(0) ));
	random_shuffle(words.begin(), words.end());
	THE_WORD =words[0];
	soFar = THE_WORD;
	soFar.assign(THE_WORD.size(), '-');


	cout << "\n\t Welcome to another exciting game of Hangman!\n\nSave Professor Wesley from getting hanged by guessing the right word!" << endl;

	while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
	{

		cout << "\nYou've guessed these letters already (" << used << ")" << endl;
		cout << "\nWord so far: " << soFar << endl;
		cout << "\n\tYou have (" << (MAX_WRONG - wrong) << ") more letters to guess before Professor Wesley gets it!" << endl;
		askGuess();
	}

	if (wrong == MAX_WRONG)
	{   system ("cls");
        cout << "\nPoor Professor Wesley was hanged!";
        cout << "\n\n\tPlay Again? (y/n)";
        cin >> restart;
        cin.get();
        if (restart == 'Y' || restart == 'y')
        {
            askGuess();
        }

	}
	else
	{
		cout << "You saved Professor Wesley from being hanged!\n";
		cout << "\nThe word is " << THE_WORD << endl;
        cout << "\n\n\tPlay Again? (y/n)";
        cin >> restart;
        cin.get();
        if (restart == 'Y' || restart == 'y')
        {
            askGuess();
        }

	}
	return 0;
}

void askGuess()

{
	char guess;
	cout << "\nEnter your guess: ";
	cin >> guess;
	guess = toupper(guess);
	while (match(used,guess))
	{
		cout << "You've already guessed " << guess;
		cout << "\nEnter your guess: ";
		cin >> guess;
		guess = toupper(guess);
	}
	used += guess;
	if (match(THE_WORD, guess))
	{
	    system("cls");
		cout << "\tSweet! \'" << guess << "\' is in the word.\n";
		for (unsigned int i = 0 ; i < THE_WORD.length () ; ++i )
		{
			if (guess == THE_WORD[i])
			soFar[i] = guess;
		}
	}
	else
	{
		system("cls");
		cout << "\tNope! There is no \'" << guess << "\' in the word.\n";
		++wrong;
		cin.get();
	}
}

bool match(string word, char letter)
{
	return (word.find(letter) != string::npos);
}

// ... do hangman game

    std::cout << "Do you want to play again? ";
    std::cin >> play_again;
    std::cin.get(); // bypass return key char
}
You have to put that code inside the main function.... you can't just put while loops outside of functions.
Topic archived. No new replies allowed.