passing constant to function

I know i'm probably missing something simple here, but i'm stummped. I can't see why i can't pass the constant " THE_WORD " to my function " testGuess " I guess there is a scope problem here that i'm not seeing.
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
// Hang Man version 2  adding functions
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;

char getGuess();
bool testGuess(char guess, const string THE_WORD );




int main()
{
    
   
    //setup
    const int MAX_WRONG = 8;    //maximum number of incorrect guesses
    vector<string> words;       //collection of words to guess
      words.push_back("GUESS");
      words.push_back("HANGMAN");
      words.push_back("DIFFICULT");
      
    srand(time(0));
    random_shuffle(words.begin(), words.end());
    const string THE_WORD = words[0];
  
    int wrong = 0;                   
    string soFar(THE_WORD.size(), '-');
    string used = "";
    char guess;
    
    cout << "Welcome to HangMan ver. 1, Good Luck!!\n";
    
    //THE MAIN LOOP OF THE GAME
    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 << endl;
          cout << "\nSo far, the word is:\n" << soFar << endl;
          
          //function call to get guess
          guess = getGuess();
          
          while (used.find(guess) != string::npos)
          {
                cout << "\nYou've already guessed " << guess << endl;
                
                guess = getGuess();
          }
          
          used += guess;
          
          if (testGuess(guess, THE_WORD) == true)
          {
             cout << "That's right! " << guess << " is in the word.\n";
           
             //update soFar to include newly guessed letter
             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;
          }
    }
    //shutting down the game
    if (wrong == MAX_WRONG)
       cout << "\nYou've been hanged!";
    else
        cout << "\nThe word was " << THE_WORD << endl;
        
        system("pause");
        return 0;
}
        
//function definitions

char getGuess()
{
     char response;
	 cout << "Please enter guess: " << endl;
	 cin >> response;
	 response = toupper(response);
	 return response;
}
  
bool testGuess(char guess, const string THE_W0RD)
{
     if (THE_WORD.find(guess) != string::npos)
     
     return true;
     
     else
     
     return false;
     
         
}   
Last edited on
closed account (z05DSL3A)
1
2
3
4
bool testGuess(char guess, const string THE_W0RD)
{
if (THE_WORD.find(guess) != string::npos)
<--- THE_W0RD here

<-- Looks different to here. 
Have you used a zero instead of an oh?
I don't know HOW i did that, or how you caught it ( good eye sir ), but you were right! I knew i was overlooking something simple, but i never dreamed it was that simple. Thanks Grey Wolf, again, good eye!
closed account (z05DSL3A)
I would normally have asked for the code to be put in [code] tags, but I may have missed it if you had. In my browser the zero and oh are quite different for the normal text but not so much in the [code] font.
I'm am glad you caught it for me. It was driving me crazy. On my computer, my IDE and my browser the 0 and O are almost identical. I re-typed every instance of THE_WORD to be sure, and lo and behold.... success. This would have drivin me to the edge trying to figure what was wrong. Anytime i get stuck, it's almost always something simple. Thanks again. Now i can move on to my next problem set. I love this language.
closed account (z05DSL3A)
I would recommend changing the font used by your IDE if you can, one that makes a clear distinction between similar letters and numbers.

I use Consolas (edit: on Windows, I can't remember what I use on my Mac)
http://en.wikipedia.org/wiki/Consolas
Last edited on
Topic archived. No new replies allowed.