Problem with passing parameters.

Hey guys, my next task is to split up the main function into separate functions; it compiles but when I play the hangman game, it doesn't replace the dash with the appropriate lettering. Everything else seems to work.

Thanks,
Kind regards.

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
  // Hangman Game Program in main()

#include <iostream> // Input/Output stream
#include <string> // String functions
#include <ctime> // Data and Time information
#include <cctype> // Transform individual Characters/ Converting characters to uppercase
#include <vector> // Data/Element containers
#include <algorithm> // Collection of functions designed for ranges of elements

using namespace std;

char getGuess();
void isGuessInWord(string, char, string);


string used = "";

int wrong = 0;

int main(){

	//setup

	const int MAX_WRONG = 8;					// Create a new integer constant that holds the amount of wrong tries the user makes.

	vector<string> words;						// Vector with string elements that has a 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 generator that uses the time as the seed.
	random_shuffle(words.begin(), words.end()); // Shuffles the words from begin to end in a random sequence determined by the generator seed.
	const string THE_WORD = words[0];			// Number of incorrect guesses
	string soFar(THE_WORD.size(), '-');			// Word guessed so far


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

		char guess;
		guess = getGuess();
		
		while (used.find(guess) != string::npos) {
			cout << "\nYou've already guessed " << guess << endl;
			cout << "Enter your guess: ";
			cin >> guess;
			guess = toupper(guess);
		}

		used += guess;

		isGuessInWord(THE_WORD, guess, soFar);
	}

	if (wrong == MAX_WRONG) {
		cout << "\nYou've been hanged!";
	}
	else {
		cout << "\nYou guessed it!";
	}

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

	return 0;
}

char getGuess() {

	char guess;
	cout << "\n\nEnter your guess: ";
	cin >> guess;
	guess = toupper(guess); 
	
	return guess;
}

void isGuessInWord(string THE_WORD, char guess, string soFar) {

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

		for (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
isGuessInWord(), when called, receives copies of its parameters that it works on, so any changes you make to its parameters does not affect main() at all. You could pass soFar by reference in order to have the changes reflected outside of the function.

See: http://www.cplusplus.com/doc/tutorial/functions/#reference
closed account (iAk3T05o)
Not a solution but there is no need to use global variables or include all those comments in between the #include's (except <cctype>).
Zhuge, you sir are awesome. Thank you very much! I'm not to familiar with referencing because that's in the next section in this book.

Thanks again.

@Nathan - I heavily comment at the minute so everything sticks, it helps me. I would comment that heavily otherwise.
Topic archived. No new replies allowed.