Well, when you deal with switch statements you should always encase your cases in brackets. You first switch statement was very hard to read and it took me a while to notice that the first switch statement did not have any breaks so it actually will run the second switch statement once it is done. what was the output you were getting when you ran it? I can guess that the enemy name wasn't displayed because usually to display a string you need to use the function c_str() to get it to work with cout.
1 2
|
string s = "a string";
cout << s.c_str();
|
That will help with your display now. As for your code in regards to the math of enemy health, what you did is syntax correct, but I would change it a bit.
1 2
|
EnemyHealth = enemyhealth - DamageAmount;
EnemyHealth-=DamageAmount;
|
The first is your way which works fine, but the second is an easier way to type it and does the exact same thing. +=. -=, *= and /= all take the amount on the right and change the left variable in respect to the right and place it in the right so.
1 2 3 4 5
|
int a = 10, b = 2;
a+=b; // a would be 12
a-=b; // a would be 8
a *= b; // a would be 20
a /= b; // a would be 5
|
Hopefully that helps you in the future. What I don't understand is since you have that line in all your switch statements, why type it 20 times when you can place it before the switch statement once? It would make your code way smaller and your processor wouldn't have as much work to do. I know you said you were a newbie, so hopefully this helps in your future coding ideas.
Oh, by the way, you code as it is runs fine. You made a simple error in your main function. Try this
1 2 3 4 5 6 7 8 9
|
int main()
{
int EnemyHealth = 100;
OgreRanSwordStrikesBtl(1,22,&EnemyHealth,"a rat");
cout << EnemyHealth;
}
|
I changed EnemyHealth to &EnemyHealth because you want to pass the memory location and not the numebr to the function. You can actually set up the function like this
1 2 3 4
|
void SomeFunction(int &number)
{
number++;
}
|
If you set up the function like this, then when you pass something into it and check it later, it will change.
1 2 3
|
int a = 10;
SomeFunction(a);
cout << a << endl;
|
a will output as 11 now instead of 10. You see, putting & in front of a variable tells you not to use the variable but rather the memory location instead. When you create a variable, you assign space for it. using int, you assign 32 bits of space for it. This is all part of the pointers learning portion of C/C++ which is great knowledge once you understand it. If you do, then this lesson here will be for others who find this and wanted to understand it themselves.
An easier way for you to figure this out for yourself in the future is to use the F10 and F11 keys. using these keys you can go through line by line each piece of code. F10 will run a whole function, skipping all the code inside it while F11 will make you go through each line in the function also. This way you could see how your variables change and what is going on. If you did this, you would see your code worked great but once you left the function, everything was still the same.