help with errors on hangman rewritten by functions

hello i need to rewrite the hangman in ch 4 to separate it into 3 functions but having a little trouble..i'm getting some errors
Error 1 error C2065: 'used' : undeclared identifier.
Error 2 error C2228: left of '.find' must have class/struct/union
Error 3 error C1903: unable to recover from previous error(s); stopping compilation
4 IntelliSense: identifier "used" is undefined
5 IntelliSense: identifier "THE_WORD" is undefined
6 IntelliSense: identifier "guess" is undefined
7 IntelliSense: identifier "soFar" is undefined
8 IntelliSense: identifier "wrong" is undefined

any help is well 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
  // Hangman
// The classic game of hangman

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

using namespace std;
char askplayer(); 
void chekguess();

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_shuffle(words.begin(), words.end());
    const string THE_WORD = words[0];            // word to guess
    int wrong = 0;                               // number of incorrect guesses 
    string soFar(THE_WORD.size(), '-');          // word guessed so far
    string used = "";                            // letters already guessed

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

    // main loop
    while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
    {
		char guess=askplayer(); 
		chekguess(); 
	}
	 if (wrong == MAX_WRONG)
        cout << "\nYou've been hanged!";
    else
        cout << "\nYou guessed it!";
    
    cout << "\nThe word was " << THE_WORD << endl;

    return 0;
}
char askplayer ()
{
       
        char guess;
        cout << "\n\nEnter your guess: ";
        cin >> guess;
        guess = toupper(guess); //make uppercase since secret word in uppercase
        while (used.find(guess) != string::npos)
        {
            cout << "\nYou've already guessed " << guess << endl;
            cout << "Enter your guess: ";
            cin >> guess;
            guess = toupper(guess);
        }

        used += guess;
		return guess;
}
void chekguess() 
{
        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;
        }
    }
I was trying to play the Hangman so I tweaked your codes to at least make it run.

Basically I was just passing your local (in the main() function) variables into the function that needed access to it.
You could organize your variables better though, or use pointers maybe?

Check this our for reference in passing arguments = http://www.cplusplus.com/doc/tutorial/functions/

Good luck,
Here's the edited code :
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;
char askplayer(string &used); 
void chekguess(char guess, const string &THE_WORD, string &soFar, int &wrong);

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_shuffle(words.begin(), words.end());
    const string THE_WORD = words[0];            // word to guess
    int wrong = 0;                               // number of incorrect guesses 
    string soFar(THE_WORD.size(), '-');          // word guessed so far
    string used = "";                            // letters already guessed

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

    // main loop
    while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
    {
		char guess=askplayer(used); 
		chekguess(guess, THE_WORD, soFar, wrong); 
	}
	 if (wrong == MAX_WRONG)
        cout << "\nYou've been hanged!";
    else
        cout << "\nYou guessed it!";
    
    cout << "\nThe word was " << THE_WORD << endl;

    return 0;
}
char askplayer (string &used)
{
       
        char guess;
        cout << "\n\nEnter your guess: ";
        cin >> guess;
        guess = toupper(guess); //make uppercase since secret word in uppercase
        while (used.find(guess) != string::npos)
        {
            cout << "\nYou've already guessed " << guess << endl;
            cout << "Enter your guess: ";
            cin >> guess;
            guess = toupper(guess);
        }

        used += guess;
		return guess;
}
void chekguess(char guess, const string &THE_WORD, 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
thx very much for the help i found the errors i was making thx to you i also added another part to keep track, here is the new program i did works perfectly fine

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
// Hangman
// The classic game of hangman

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

using namespace std;
const int MAX_WRONG = 8;  // maximum number of incorrect guesses allowed 
int wrong = 0;                               // number of incorrect guesses 
void displayresult (string used,string &soFar);     //will display number of incorrect guesses, show word guessed so far and words used
char askplayer(string &used);                      //ask for players guess function
void chekguess(char guess, const string &THE_WORD, string &soFar, int &wrong);  //checks players guess


int main()
{
    // set-up 

    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_shuffle(words.begin(), words.end());
    const string THE_WORD = words[0];            // word to guess 
    string soFar(THE_WORD.size(), '-');          // word guessed so far
    string used = "";                            // letters already guessed

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

    // main loop
    while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
    {
		displayresult(used,soFar); 
		char guess=askplayer(used); 
		chekguess(guess, THE_WORD, soFar, wrong); 
	}
	 if (wrong == MAX_WRONG)
        cout << "\nYou've been hanged!";
    else
        cout << "\nYou guessed it!";
    
    cout << "\nThe word was " << THE_WORD << endl;
	system ("pause"); 

    return 0;
}
//displays data of guesses left, word guessed so far and letters used
void displayresult(string used,string &soFar)   
{
	cout<< "\n\n You have " << (MAX_WRONG- wrong);
	cout<< " Incorrect guesses left.\n";
	cout<< "\nYou've used the following letters:\n" << used; 
	cout<< "\nSo far, the word is:"<<soFar << endl; 
}
// players guess
char askplayer (string &used)
{
       
        char guess;
        cout << "\n\nEnter your guess: ";
        cin >> guess;
        guess = toupper(guess); //make uppercase since secret word in uppercase
        while (used.find(guess) != string::npos)
        {
            cout << "\nYou've already guessed " << guess << endl;
            cout << "Enter your guess: ";
            cin >> guess;
            guess = toupper(guess);
        }

        used += guess;
		return guess;
}
// chek payers guess
void chekguess(char guess, const string &THE_WORD, 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;
    }
}
Sure, yeah that's a nice improvement.

Wouldn't it be better if you keep your collection of words in a text file via File I/O :)

So you can randomly take just one word from the text file everytime the game restarts.
Topic archived. No new replies allowed.