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.
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:
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
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
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.
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;
}