Hangman with functions

Hello fellow programmers! I have an exercise to make: I need to rewrite the Hangman game i have written here, but with functions. I need to include a function to get the player’s guess and another function to determine whether the player’s guess is in the secret word. I have done the first function but i am having problems with the other one. It doesn't compile.

This is the Hangman with no functions:

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

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

using namespace std;

int main()
{
    //setup
    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(time(0));
    random_shuffle(words.begin(), words.end());
    const string THE_WORD = words[0];          // word to guess
    int wrong;                                 // number of incorrect guesses
    string sofar(THE_WORD.size(), '-');        // word guessed so far
    string used = "";                          // letters already guessed;   
    
    cout<<"\nWelcome to hangman. 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<<endl;
          cout<<"\nSo far the word is:\n"<<sofar<<endl;
          char guess;
          cout<<"\n\nEnter your guess: ";
          cin>>guess;
          guess = toupper(guess); // make uppercase since secret word is uppercase
          while (used.find(guess) != string::npos)
          {
                cout<<"\nYou have already guessed "<<guess<<endl;
                cout<<"Enter your guess: ";
                cin>>guess;
                guess = toupper(guess);
          }
          
          used += guess;
          
          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 (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;
          }
    }
    
    // shut down
    if (wrong == MAX_WRONG)
       cout<<"\nYou've been hanged!";
    else
        cout<<"\nYou guessed it!";
    
    cout<<"The word was "<<THE_WORD<<endl;
    
    system ("PAUSE");
    return 0;
}


And here is my code for Hangman with functions:
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
// Hangman - with functions

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

using namespace std;

char getguess(string prompt);
void yesorno();

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

    cout<<"Welcome to Hangman. 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<<endl;
          cout<<"\nSo far the word is:\n"<<sofar<<endl;
          char guess;

          guess = getguess("Enter your guess: ");
          guess = toupper(guess);
          while (used.find(guess) != string::npos)
          {
              cout<<"You have already guessed "<<guess<<endl;
              guess = getguess("Enter your guess: ");
              guess = toupper(guess);
          }

          used += guess;

          yesorno();
    }

    // shut down
    if (wrong == MAX_WRONG)
        cout<<"\nYou have been hanged!";
    else
        cout<<"\nYou guessed it!";

    cout<<"The word was "<<THE_WORD<<endl;

    return 0;
}

char getguess(string prompt)
{
    char character;
    cout<<prompt;
    cin>>character;
    return character;
}

void yesorno()
{
    if (THE_WORD.find(guess) != string::npos)
    {
        cout<<"That is right! "<<guess<<" is 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;
    }
}
I suggest reading from this page, http://www.cplusplus.com/doc/tutorial/functions/ , the section arguments passed by value and by reference ,which is about halfway down the page.
Ok i will see to it and then give you further information. Thanks btw.
@Mats OK i think i fixed it and it compiles without errors, but another problem showed up now, it just ignores the main loop and tells the answer directly. Any ideas?

Here is the 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Hangman - Rewrited with functions
// End of chapter - Functions

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

using namespace std;

void prompt();
char getguess(char& a);
void yesorno(const string& a, char& b, string& c, int& d);

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

    cout<<"Welcome to Hangman. 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<<endl;
          cout<<"\nSo far the word is:\n"<<sofar<<endl;
          char guess;

          prompt();
          getguess(guess);
          guess = toupper(guess);
          while (used.find(guess) != string::npos)
          {
              cout<<"You have already guessed "<<guess<<endl;
              prompt();
              getguess(guess);
              guess = toupper(guess);
          }

          used += guess;

          yesorno(THE_WORD, guess, sofar, wrong);
    }

    // shut down
    if (wrong == MAX_WRONG)
        cout<<"\nYou have been hanged!";
    else
        cout<<"\nYou guessed it!";

    cout<<"The word was "<<THE_WORD<<endl;

    return 0;
}

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

char getguess(char& a)
{
    cin>>a;
    return a;
}

void yesorno(const string& a, char& b, string& c, int& d)
{
    if (a.find(b) != string::npos)
    {
        cout<<"That't right "<<b<<" is in the word.\n";
        // update sofar to include newly guessed letter.

        for (int i=0; i<a.length(); ++i)
            if (a[i] == b)
                c[i] = b;

    }
    else
    {
        cout<<"Sorry "<<b<< " isn't in the word.\n";
        ++d;
    }

}
int wrong holds a junk value. You should initialize it to zero, otherwise that main loop could fail.
That worked. Thanks a lot!! Merry Christmas :)
Topic archived. No new replies allowed.