Hangman

So I'm coding a "Hangman" game, where the user tries to guess a word by entering letters one at a time. Every wrong guess gets the user closer to getting "hanged". What I can't figure out though is, I have two functions apart from main(), how to return multiple values from one function. How do I do that? Look at the "searchingThroughGuess" function below.

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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <locale>
using namespace std;

char guess(){
    char x;
    cout << "\nEnter your guess: "; cin >> x;
    if(isalpha(x)){
        if(islower(x))
            x = toupper(x);
        return x;
    }else
        guess();
}

string searchThroughGuess(char playerGuess, string actualword, string secretword, int guesses){
    for(int x=0;x<actualword.size();x++){
        if(playerGuess == actualword[x])
            secretword[x] = playerGuess;
        else if(playerGuess != actualword[x] && 1==(actualword.size()-x)){
            guesses++;
        }
    }
    return secretword, guesses;
}

int main(){
    vector<string> words = {"POTENTIALLY","DANGEROUS","ZEPPELIN"};
    srand(static_cast<unsigned int>(time(0)));
    random_shuffle(words.begin(),words.end());
    string actualword = words[0], secretword, soFar;
    char playerGuess;
    for(int x=0;x<actualword.size();x++){
        secretword += "-";
    }
    const int MAX_ANTAL = 8;
    int guesses = 0;
    while(secretword != actualword && guesses < MAX_ANTAL){
        cout << "\t\t-= Hangman =-\n\nWord: " << secretword << "\nGuessed letters so far: " << soFar << "\nIncorrect guesses left: " << soFar;
        playerGuess = guess();
        secretword, playerGuess = searchThroughGuess(playerGuess, actualword, secretword, soFar);
    }
    if(secretword == actualword)
        cout << "Congratulations! You won!" << endl;
    else
        cout << "You lost!" << endl;
}
Last edited on
You can't return more than one variable. However, you can bypass this limit.

The easiest solution is to pass arguments you want to return as reference:

 
void searchThroughGuess(char playerGuess, string actualWord, string& secretword, int& guesses);

You can also do the same using pointers, but imho reference looks better.

Eventually, if you really want to return few values, you can create structure with multiple variables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct IntAndString
{
 string secretword;
 int guersses;
}

//...code
IntAndString searchThroughGuess(char playerGuess, string actualword, string secretword, int guesses){
//...code
IntAndString toReturn;
toReturn.secretword = secretword;
toReturn.guesses = guesses;

return toReturn;
}


However, from what you are saying, you probably want to use reference way.

Cheers!
What I can't figure out though is, I have two functions apart from main(), how to return multiple values from one function. How do I do that? Look at the "searchingThroughGuess" function below.


You can't. However, you *can* modify the function so that it will modify values passed to it. See below for an example:

1
2
3
4
5
6
7
8
9
10
void change_to_five(int& x) { //note the &, this means it is passed by reference
    x = 5;
}

int main() {
    int x = 3;
    change_to_five(x);
    //now x is 5
    return 0;
}
Reference worked great, even though I don't really get how it works, but thanks for the help, the program works now :)
Last edited on
Topic archived. No new replies allowed.