Hangman Game with functions

The assignment in the book I am currently reading was to reconstruct the hangman game using methods. Which I did. However when compiling, I am receiving a Microsoft C++ Runtime Error! The error is stating guess has not been initialized, yet I've initialized it as type char.

The error list:
Warning C4700: uninitialized local variable 'guess'


This is section of code the error list is pointing

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
// hangman_2.0_chapter5.cpp : Defines the entry point for the console application.
//

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

using namespace std;

void prompt();
char getguess();
void yesorno(const string, char, string, int);


int main()
{


	//set up
	const int MAX_WRONG = 8;             //maximum number of incorrect guesses allowed

	vector<string> words;               //collection of possible words to guess
	words.push_back("GUESS");
	words.push_back("HANGMAN");
	words.push_back("DIFFICULT");

	srand(static_cast<unsigned int>(time(0))); //random the word to be guessed
	random_shuffle(words.begin(), words.end());

	const string THE_WORD = words[0];   /* words is a vector of possible words to guess.
										THE_WORD has been assigned the first word in the
										word to be guessed */

	int wrong = 0;                      // number of incorrect guesses
	string soFar(THE_WORD.size(), '_'); // word guessed so far.. each letter in word rep by '_'
	string used = "";                   // letters already guessed

	cout << "Welcome to Hangman 2.0. Good Luck!\n";

	// main loop
	while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
	{
		cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
		cout << "\nYou've used the following letters:\n" << used;
		cout << "\nSo far the word is: \n " << soFar;

		// Getting the Players' Guess
		
		prompt();
		char guess = getguess();

		while (used.find(guess) != string::npos)
		{
			cout << "\nYou've already guessed " << guess;
			prompt();
			char guess = getguess();
			//guess = toupper(guess);
		}

		used += guess;

		yesorno(THE_WORD, guess, soFar, wrong); //calling function yesorno(const, char, string, int);

	}										//End the Game
											//shut down
	if (wrong == MAX_WRONG)
	{
		cout << "\nYou've used you're last guess! GAME OVER";
	}
	else
	{
		cout << "\nYou guessed it!";
	}

	cout << "\nThe word was " << THE_WORD;

	return 0;
}

void prompt()
{
	cout << "Enter your guess: ";
}

char getguess()
{
	char guess;
    cin >> guess; //getting character / answer from user
	guess = toupper(guess); //make uppercase since secret word is uppercase
	return guess; // returning the answer to main;
}

void yesorno(const string THE_WORD, char guess, string soFar, int wrong)
{
	if (THE_WORD.find(guess) != string::npos)
	{
		cout << "That's right! " << guess << " is in the word.\n";

		//update soFar to include newly guessed letter
		for (unsigned int i = 0; i < THE_WORD.length(); ++i)
		{
			if (THE_WORD[i] == guess)
			{
				soFar[i] = guess;
			}
		}
	}
	else
	{
		cout << "Sorry, " << guess << " isn't in the word.\n";
		++wrong;
	}
}
Last edited on
newbieGameProgrammer101

What I have found using Visual Studio and encountering the same error is thatchar guess; is defined, but has no value and could contain junk. The usual fix for me has been char guess{ 'a' };.

I believe that the error Warning C4700: uninitialized local variable 'guess' means that you did not initialize the variable when it was define or setting guess to something shortly after it was defined. Better to initialize the variable when it is defined anything will do even a space. Try to pick something that the user is not likely to use.

hope that helps,

Andy
@Handy Andy thank you for explaining why the error was occurring that was very useful in helping me know how to solve this issue if it occurs in future code; which i shouldn't because i'll be initializing when i define a variable. :)

Additonally, I'm receiving an error
Warning C4018: '<': signed/unsigned mismatch

I read the documentation on a C4018 error (https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(C4018)&rd=true).

After reading the documentation I believe the error is being caused because within my for loop int i is an integer which is being compared '<' to THE_WORD.length() is an unsigned number.

Last edited on
The good news is that you're really close to having a working program.

Initializing guess won't help in this case. You are trying to assign it inside getguess() but you're passing it by value, so getguess just changes the local value.

Change getguess() to use a local variable and return the result:
1
2
3
4
5
6
7
char getguess()
{
	char guess;
	cin >> guess; //getting character / answer from user
	guess = toupper(guess); //make uppercase since secret word is uppercase
	return guess; // returning the answer to main;
}


And change the code that gets it at lines 51-53 to:
1
2
prompt();
char guess = getguess();

Make a similar change at line 59.

You have a similar problem with function yesorno(). The soFar and wrong parameters must be passed by reference.

If you fix these issues, I think you'll find that the program works.
Hi, I have applied your suggestions and the program compiles without any errors. But it's not working as intended. The player can make as many attempts as he or she wants and the MAX_WRONG = 8. none of the characters are being revealed which points to the yesorno() function isn't working properly.

You mentioned yesorno() parameters soFar and wrong should be passed by reference. How do I go about doing that? Aren't I passing them by reference as parameters? I'm really confused.

void yesorno(const string THE_WORD, char guess, string soFar, int wrong)

Last edited on
Aren't I passing them by reference as parameters?

No. You're passing them by value, which means you're passing a copy of the argument.

When you update soFar at line 107 or wrong at line 114, you're changing the copy, not the instance in the caller.

Your function signature should look like this:
 
void yesorno(const string THE_WORD, char guess, string & soFar, int & wrong)

Note the & on soFar and wrong. This indicates the argument is passed by reference.
Don't forget to update the function prototype at line 15 so it matches.

See the section: Arguments passed by value and by reference
on the following page:
http://www.cplusplus.com/doc/tutorial/functions/


Last edited on
@AbstractionAnon, @dhayden , @handyandy Thank you for all your help! The program is now working properly.
Topic archived. No new replies allowed.