Text Game not working

My game is very bad and not hard but that's besides the point. I need to find a way to get the random animal that is chosen to loose health. I don't know how to get the random animal singled out. Here is my code and build messages.
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <ctime>
#include <cstdlib>

    int gold = 50;

using namespace std;

class human
{
    public:
    int health, gold;

    int attack();
};

class animals
{
    public:
    int health;

    int animattack ()
    {


       cout << "The animal is attacking you!";
       int iSecret;

       srand ( time(NULL) );

       iSecret = rand() % 100 + 1;

       return 0;
    }

};

int animal ()
{
    int iSecret;

    srand ( time(NULL) );

    iSecret = rand() % 10 + 1;

    if (iSecret == 1)
    {
        cout << "Jaguar";
    }
    else if (iSecret == 2)
    {
        cout << "Snake";
    }
    else if (iSecret == 3)
    {
        cout << "Tiger";
    }
    else if (iSecret == 4)
    {
        cout << "Lion";
    }
    else if (iSecret == 5)
    {
        cout << "Zebra";
    }
    else if (iSecret == 6)
    {
        cout << "Leopard";
    }
    else if (iSecret == 7)
    {
        cout << "Elephant";
    }
    else if (iSecret == 8)
    {
        cout << "Cougar";
    }
    else if (iSecret == 9)
    {
        cout << "Sun Bear";
    }
    else
    {
        cout << "Okapi";
    }

    cout << "!";
    return 0;
}

int attack ()
{

    int iSecret;

    cout << "I'm attacking!";

    srand ( time(NULL) );

    iSecret = rand() % 100 + 1;

    const int g = iSecret;

    cout << " You have applied " << g << " damage.";

    animals.health = animals.health - g;

    if (animals.health > 0)
    {
        cout << "The animals health is " << animals.health;
    }
    else
    {
        cout << "The animal has died";
    }
    return 0;
}

int run ()
{
    cout << "I have run away!";
    gold = gold - 5;
    cout << "I lost 5 gold! I now have " << gold << " gold";
    return 0;
}
int main()
{
    int iSecret;
    srand ( time(NULL) );

    iSecret = rand() % 100 + 1;

    human player;
    player.health = 100;

    animals jaguar, snake, lion, tiger, zebra, leopard, elephant, cougar, sunbear, okapi;
    jaguar.health = iSecret;
    snake.health = iSecret;
    lion.health = iSecret;
    tiger.health = iSecret;
    zebra.health = iSecret;
    leopard.health = iSecret;
    elephant.health = iSecret;
    cougar.health = iSecret;
    sunbear.health = iSecret;
    okapi.health = iSecret;

    char choice;
    cout << "\t\t\tWelcome to gerfy's game!\n\n ";
    cout << "In this game you want are trying to get across";
    cout << " the jungle but there are many\n different animals that will attack you.";
    cout << " To get past the animals you must kill\n them. As you kill the animals you will";
    cout << " you will get gold. You can choose to run away from the animal or attack it.";
    cout << " If you run away from the animal you will get more health. ";
    cout << "\n\n\t\t\t Now lets start the game!";
    cout << "\n\n You run into a ";
    animal();
    cout << "Do you want to [f]ight or [r]un away?";
    cin >> choice;
    if (choice == 'f')
   {
       attack();
   }
   else if (choice == 'r')
   {
       run ();
   }
   else
   {
        cout << "Error";
   }


    return 0;
}


and here is my build messages

1
2
3
4
5
C:\Users\Geoff\Game\main.cpp||In function 'int attack()':|
C:\Users\Geoff\Game\main.cpp|106|error: expected unqualified-id before '.' token|
C:\Users\Geoff\Game\main.cpp|108|error: expected primary-expression before '.' token|
C:\Users\Geoff\Game\main.cpp|110|error: expected primary-expression before '.' token|
||=== Build finished: 3 errors, 0 warnings ===|
Ok...there's some issues as u know.

Line 157 calls a function who's sole purpose is to print the name of a random animal to the screen. This is in no way connected to ur animal class so the animal objects u created are not going to be affected by that function...
1 option is to have the function animal() create a random animal as u wanted and then return that animal as the return type instead of an int.
example:
1
2
3
4
5
6
7
else if (iSecret == 9)
    {
        cout << "Sun Bear";
        animals sunbear;
        //set health
        return sunbear;
   }


Another thing is the design of ur classes. 'health, gold' should be private members. Lines 137 to 147 should not be accessing that member variable directly...Create Member functions like AdjustHealth(int damage) { return health - damage; }

Line 106 and similar are trying to apply things to the class itself and not to an object/instance of the class (which is what u want). Following the first idea about using the function animal() to pick an animal object, u can use that return to either send as a parameter to the attack() function or (probably better) would be to make more member functions that define how an animal object reacts to a human object.

Hope this gets u moving ~

ps: naming conventions - in general functions/classes/objects of classes should all start with capitol letters (class names could be all capitol like ANIMALS and HUMAN). member variables like 'health' should be prefixed by something like m_health. This makes it clear what everything is/where everything belongs and will make things much easier to read and expand in the future

Last edited on
1
2
3
4
5
6
7
int iSecret;

srand ( time(NULL) );

iSecret = rand() % 100 + 1;

return 0;


so what's the purpose of iSecret?
A few things to note...

The reason this won't compile is because of the animals.health = animals.health - g; line in the animal() function and, in fact, any other line in that function that references 'animals'.

In your code, animals is a class, of which you create instances of in main. In order for the animal() function to operator on that class, it needs to be passed an instance or a reference to an instance of that class.

On the whole, the design is sketchy here. I would take a few steps back and rethink. For example, what is the animal() function actually trying to achieve? Why is there a global attack() implemented, when the attack() function for the human class hasn't been defined?

Also, you should only ever seed the PRNG (that is, call srand) once in your entire program, no more. I don't know if it's what you intended, but you're currently setting all animals to the same "random" health.

As for the naming conventions in the above post, it's down to preference but I've never seen a class name that's all constants (the majority of people would reserve such formatting for constants, macros and defines). What I will say on the naming front is try to be clear with what your variables and functions are doing. For example, a function named MakeCashWithdrawal() states its intent a lot clearer than Cash() in its name alone.

To reiterate, my advice is to step back a bit and draw this design out better. You'll get clearer, more robust code if you plan out your designs, rather than coding on the fly.
Ok so I made some changes and now the health is not working correctly. The code compiles but the health comes out to be a really long number. Here is my 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <iostream>
#include <ctime>
#include <cstdlib>

    int gold = 50;

using namespace std;

class human
{
    public:
    int health, gold;

    int attack();
};

class animals
{
    public:
    int health;

    int attack ()
{

    int iSecret;

    cout << "I'm attacking!";

    srand ( time(NULL) );

    iSecret = rand() % 10 + 1;

    const int g = iSecret;

    cout << " You have applied " << g << " damage.";

    health = health - g;

    if (health > 0)
    {
        cout << "The animals health is " << health << "It is now attacking you!";
        animattack();
    }
    else
    {
        cout << "The animal has died";
    }
    return 0;
}


    int animattack ()
    {
       int iSecret;

       srand ( time(NULL) );

       iSecret = rand() % 100 + 1;

       human player;

       player.health = player.health - iSecret;

       cout << "Your health is now " << player.health;

       return player.health;
    }

};

animals animal ()
{
    int iSecret;

    srand ( time(NULL) );

    iSecret = rand() % 10 + 1;

    if (iSecret == 1)
    {
        int health;
        cout << "Jaguar";
         animals jaguar;
        jaguar.health = health;

    srand ( time(NULL) );

    health = rand() % 10 + 1;

    return jaguar;
    }
    else if (iSecret == 2)
    {
        int health;
        cout << "Snake";
         animals snake;
        snake.health = health;

    srand ( time(NULL) );

    health = rand() % 10 + 1;

    return snake;
    }
    else if (iSecret == 3)
    {
        int health;
        cout << "Tiger";
         animals tiger;
        tiger.health = health;

    srand ( time(NULL) );

    health = rand() % 10 + 1;

    return tiger;
    }
    else if (iSecret == 4)
    {
        int health;
        cout << "Lion";
         animals lion;
        lion.health = health;

    srand ( time(NULL) );

    health = rand() % 10 + 1;

    return lion;
    }
    else if (iSecret == 5)
    {
        int health;
        cout << "Zebra";
         animals zebra;
        zebra.health = health;

    srand ( time(NULL) );

    health = rand() % 10 + 1;

    return zebra;
    }
    else if (iSecret == 6)
    {
        int health;
        cout << "Leopard";
         animals leopard;
        leopard.health = health;

    srand ( time(NULL) );

    health = rand() % 10 + 1;

    return leopard;
    }
    else if (iSecret == 7)
    {
        int health;
        cout << "Elephant";
         animals elephant;
        elephant.health = health;

    srand ( time(NULL) );

    health = rand() % 10 + 1;

    return elephant;
    }
    else if (iSecret == 8)
    {
        int health;
        cout << "Cougar";
        animals cougar;
        cougar.health = health;

        srand ( time(NULL) );

        health = rand() % 10 +1;

        return cougar;
    }
    else if (iSecret == 9)
    {
        int health;
        cout << "Sun Bear";
        animals sunbear;
        sunbear.health = health;

    srand ( time(NULL) );

    health = rand() % 10 + 1;

    return sunbear;
    }
    else
    {
        int health;
        cout << "Okapi";
         animals okapi;
        okapi.health = health;

    srand ( time(NULL) );

    health = rand() % 10 + 1;

    return okapi;
    }

    cout << "!";
}

int run ()
{
    cout << "I have run away!";
    gold = gold - 5;
    cout << "I lost 5 gold! I now have " << gold << " gold";
    return 0;
}
int main()
{
    int iSecret;
    srand ( time(NULL) );

    iSecret = rand() % 100 + 1;

    human player;
    player.health = 100;

    char choice;
    cout << "\t\t\tWelcome to gerfy's game!\n\n ";
    cout << "In this game you want are trying to get across";
    cout << " the jungle but there are many\n different animals that will attack you.";
    cout << " To get past the animals you must kill\n them. As you kill the animals you will";
    cout << " you will get gold. You can choose to run away from the animal or attack it.";
    cout << " If you run away from the animal you will get more health. ";
    cout << "\n\n\t\t\t Now lets start the game!";
    cout << "\n\n You run into a ";
    animal();
    cout << " Do you want to [f]ight or [r]un away?";
    cin >> choice;
    if (choice == 'f')
   {
       animals jaguar;
       jaguar.attack();
   }
   else if (choice == 'r')
   {
       run ();
   }
   else
   {
        cout << "Error";
   }


    return 0;
}
Hey. Sorry for the delay.

To answer ur question it seems that line 81 declares a 'health' int then line 84 assigns the uninitialized 'health' variable to the 'jaguar' object. 'health' is finally assigned a value on line 88 but should have been assigned a value before line 84...

Those classes still need a lot of work...right now they r very contorted and it's difficult to follow what each part is supposed to do even tho it's supposed to be simple tasks. It doesn't look like they'll behave the way u want... I strongly suggest reading some online tutorials on basic classes. Sam's teach urself c++ in 21 days is good enough to get u started. Right now u'r mixing c style functions with member functions - and structures with classes.

Keep at it. It'll get easier. Just start smaller and learn why each thing is done before moving on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class CAT //CAT or Cat..much easier to read...
{
   Meow(); //Because CATS go meow lol
   SetAge(int setage) {  return m_age = setage;  }  //if the function is rly short it can be inline...
private:
   int m_age; //private because only a CAT should be able to access this
        //the 'm_' is there to make it clear it is a member variable and not a regular one
}

CAT::Meow() //proper way to implement member functions
{
   cout << "meowwwwwwww. My age is " << m_age;
}

int main()
{
   CAT Frisky; //Similar to class name, 
   Frisky.SetAge(2); //notice how the objects 'm_age' is not being accessed directly ?
//m_age should only ever appear in the class CAT implementation
   Frisky.Meow();
return 0;
}

meowwwwwwww. My age is 2

Last edited on
Topic archived. No new replies allowed.