Trouble with Classes

Mar 21, 2011 at 4:21pm
Im making a game in school where I have a series of questions and the response adds to or subtracts from the health of a player contained in a class. I am trying to make a function that does this so i dont need to type it out for every question, but when i try to access the health values after the funtion called the member function, it hasn't changed.
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
class health 
{
public:
    
    void healthchange(int amount)
    {
        int temp = healthval;
        
        temp += amount;
        
        if (temp <= 0) {
            cout << "You're dead" << endl;
            healthval = 0;
        }
        else
        {
            healthval = temp;
        }
    }
    void checkhealth()
    {
        cout << "You have " << healthval << "% Health left" << endl;
    }
    
    int healthval;
    
};

void Question(string szQuestion, string szAnswer1, string szAnswer2, string szChoice1, string szChoice2, int nHealthCh1, int nHealthCh2, health player)
{
        string szIn;
        
        cout << szQuestion << endl;
        cin >> szIn;
        
        if (szIn == szChoice1) {
            cout << szAnswer1 << endl; 
            player.healthchange(nHealthCh1);
        }
        if (szIn == szChoice2) {
            cout << szAnswer2 << endl;
            player.healthchange(nHealthCh2);
        }
        if (szIn == "health") {
            player.checkhealth(); 
        }
        else{ 
            cout << "Your only options are: " << szChoice1 << " and " << szChoice2 << "." << endl;
            cin >> szIn;
            if (szIn == szChoice1) {
                cout << szAnswer1 << endl; 
                player.healthchange(nHealthCh1);
            }
            if (szIn == szChoice2) {
                cout << szAnswer2 << endl;
                player.healthchange(nHealthCh2);
            }
        }
        
}

int main (int argc, const char * argv[])
{
    health Player1, Player2;
    
    Player1.healthval = 100;
    Player2.healthval = 100;
    
    string szQuestion, szAnswer1, szAnswer2,szChoice1, szChoice2;
    int nHealth1, nHealth2;
    szQuestion = "Whats up?";
    szAnswer1 = "DATS COOL";
    szAnswer2 = "YUUPPP";
    szChoice1 = "1";
    szChoice2 = "2";
    nHealth1 = 5;
    nHealth2 = -5;
    
    for (;;)
    {
    Question(szQuestion, szAnswer1, szAnswer2 ,szChoice1, szChoice2, nHealth1, nHealth2, Player1);
    }
    return 0;
}

i apologize for the stupid responses... Please help??
Mar 21, 2011 at 4:55pm
You need to pass health by reference.

1
2
3
4
5
void Question(string szQuestion, string szAnswer1, string szAnswer2, string szChoice1,
     string szChoice2, int nHealthCh1, int nHealthCh2, health& player)
{
....
}
Mar 21, 2011 at 5:56pm
Thanks, so, when else do i need to referance it like that? just in the function definition, or what?
Mar 22, 2011 at 12:15am
You will need to specify that the variable is passed by reference in both declaration and definition.
Topic archived. No new replies allowed.