Error in switch

I have some errors in my code. I've taken out the classes and namespaces that I was using, because I don't really have a need for them. Don't mind me newbie tinkering, lol. The errors I'm facing must be logical, because the program compiles fine.

the problems I'm facing are this..

when I run the program EnemyHealth doesn't get updated based on the math that's done in the switch. Also the switch displays the default message along with my output in the case, instead of just what's within the case.

does anyone see a reason for this?

anyway here's the header.

1
2
3
4
5
6
7
8
9
10
11
#ifndef OGREWEAPONSTRIKES_H
#define OGREWEAPONSTRIKES_H
#include<string>
using std::string;
// this namespace is for holding classes and functions dealing with weapon strikes.

int OgreRanSwordStrikesBtl(int OgreSwordStrikesID, int DamageAmount, int EnemyHealth, string EnemyName);



#endif // OGREWEAPONSTRIKES_ 


the cpp

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
#include<iostream>
#include "OgreTitanium.h"
# include<cstdlib>
#include<string>
#include<ctime>
using std::string;
using namespace std;

//***The following code is for battle weapon strike functions. These functions subtract damage, and return remaining enemy health.

//this is random sword strikes function. it's for random sword strikes.
int OgreRanSwordStrikesBtl(int OgreSwordStrikesID, int DamageAmount, int EnemyHealth, string EnemyName){



switch (OgreSwordStrikesID){
    //random attack display will determine which text will be displayed for the attack message.
    //if it's a 1 it will generate for light attacks, if it's a 2 it will generate for critical attacks.

    case 1:
    srand(time(0));
    int RandomAttackDisplay;
    RandomAttackDisplay = (rand()%20)+1;


    switch (RandomAttackDisplay){

        case 1:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "A light slash doing minimal damage of " << DamageAmount << " to " << EnemyName <<"." <<endl;
        break;
        case 2:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "A light slash does minimal damage to " << EnemyName <<" " << DamageAmount <<"." << endl;
        break;
        case 3:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "A light slash cuts " << EnemyName << " for " << DamageAmount <<" damage." << endl;
        break;
        case 4:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Sword grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 5:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Sword pokes " << EnemyName << " doing " << DamageAmount << " damage. " << endl;
        break;
        case 6:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Sword lightly pokes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 7:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Blade grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 8:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Sword jabs " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 9:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Sword lightly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 10:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Sword barely grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 11:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Blade hardly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 12:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Blade lightly jabs " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 13:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 14:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Sword hardly pokes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 15:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 16:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Blade barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 17:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Blade hardly cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 18:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Blade lightly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 19:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Sword lightly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 20:
        EnemyHealth = EnemyHealth - DamageAmount;
        cout << "Sword hardly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        default:
        cout << "There has been an error." << endl << "This is an internal error" << endl;
        cout << "please report the bug to creator." << endl;
    }


    case 2:

    switch (OgreSwordStrikesID){

        case 1:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 2:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 3:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 4:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 5:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 6:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 7:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 8:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 9:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 10:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 11:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 12:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 13:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 14:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 15:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 16:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 17:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 18:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 19:
        EnemyHealth = EnemyHealth - DamageAmount;
        case 20:
        EnemyHealth = EnemyHealth - DamageAmount;
        default:
        cout << "There has been an error. check to make sure" << endl << "your variables are correct and are" << endl;
        cout << "passed into the function in the proper order." << endl;

    }

}

return EnemyHealth;



}


and main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include<string>
#include "OgreTitanium.h"
using namespace std;

int main()
{
int EnemyHealth = 100;


OgreRanSwordStrikesBtl(1,22,EnemyHealth,"a rat");

cout << EnemyHealth;

}
Last edited on
I'm wondering, does the break command cause the breaking of the case alone, or would it make the program exit the function all together? If that's the case that would explain while the return value isn't coming through. Is it possible to put the return value within the cases too?
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.
Hey, thanks, that was a great lesson. I'm still having a bit go trouble I'll re post my modified code. Thanks for letting me know about pointers, I'm gonna go watch a few videos on it, and see what I can some up with. I probably made a simple mistake trying to do the implementation.

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include<string>
#include "OgreTitanium.h"
using namespace std;

int main()
{
int EnemyHealth = 100;


OgreRanSwordStrikesBtl(1,22,&EnemyHealth,"a rat");

cout << EnemyHealth;

}


header

1
2
3
4
5
6
7
8
9
10
11
#ifndef OGREWEAPONSTRIKES_H
#define OGREWEAPONSTRIKES_H
#include<string>
using std::string;
// this namespace is for holding classes and functions dealing with weapon strikes.

int OgreRanSwordStrikesBtl(int OgreSwordStrikesID, int DamageAmount, int &EnemyHealth, string EnemyName);



#endif // OGREWEAPONSTRIKES_ 


cpp

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
#include<iostream>
#include "OgreTitanium.h"
# include<cstdlib>
#include<string>
#include<ctime>
using std::string;
using namespace std;

//***The following code is for battle weapon strike functions. These functions subtract damage, and return remaining enemy health.

//this is random sword strikes function. it's for random sword strikes.
int OgreRanSwordStrikesBtl(int OgreSwordStrikesID, int DamageAmount, int &EnemyHealth, string EnemyName){



switch (OgreSwordStrikesID){
    //random attack display will determine which text will be displayed for the attack message.
    //if it's a 1 it will generate for light attacks, if it's a 2 it will generate for critical attacks.

    case 1:
    srand(time(0));
    int RandomAttackDisplay;
    RandomAttackDisplay = (rand()%20)+1;


    switch (RandomAttackDisplay){
        EnemyHealth-=DamageAmount;
        case 1:
        cout << "A light slash doing minimal damage of " << DamageAmount << " to " << EnemyName <<"." <<endl;
        break;
        case 2:
        cout << "A light slash does minimal damage to " << EnemyName <<" " << DamageAmount <<"." << endl;
        break;
        case 3:
        cout << "A light slash cuts " << EnemyName << " for " << DamageAmount <<" damage." << endl;
        break;
        case 4:
        cout << "Sword grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 5:
        cout << "Sword pokes " << EnemyName << " doing " << DamageAmount << " damage. " << endl;
        break;
        case 6:
        cout << "Sword lightly pokes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 7:
        cout << "Blade grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 8:
        cout << "Sword jabs " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 9:
        cout << "Sword lightly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 10:
        cout << "Sword barely grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 11:
        cout << "Blade hardly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 12:
        cout << "Blade lightly jabs " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 13:
        cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 14:
        cout << "Sword hardly pokes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 15:
        cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 16:
        cout << "Blade barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 17:
        cout << "Blade hardly cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 18:
        cout << "Blade lightly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 19:
        cout << "Sword lightly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 20:
        cout << "Sword hardly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        default:
        cout << "There has been an error." << endl << "This is an internal error" << endl;
        cout << "please report the bug to creator." << endl;
    }


    case 2:

    switch (OgreSwordStrikesID){
        EnemyHealth-=DamageAmount;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
        case 10:
        case 11:
        case 12:
        case 13:
        case 14:
        case 15:
        case 16:
        case 17:
        case 18:
        case 19:
        case 20:
        default:
        cout << "There has been an error. check to make sure" << endl << "your variables are correct and are" << endl;
        cout << "passed into the function in the proper order." << endl;

    }

}

return EnemyHealth;



}


I'm getting errors in main.

error: invalid conversion from 'int*' to 'int'|
|error: initializing argument 3 of 'int OgreRanSwordStrikesBtl(int, int, int, std::string)'|
||=== Build finished: 2 errors, 0 warnings ===|


I'm using code blocks for writing my code. I do have visual studio though, maybe I should cross over? F10, and F11 do nothing in code blocks, lol. It does have a step into, and step out button through. I found that easier to use in visual studio when I was messing around with vbnet. I haven't quite figured out the entire process of debugging yet, and haven't even done so much as get past attempting it in Code::Blocks.
Also I watched a couple videos on pointers, and it said something about using the *symbol as well. I'll show changes to my code after watching the video. The code now compiles, but still there's no change to the variable. Not sure what I'm doing wrong..

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include<string>
#include "OgreTitanium.h"
using namespace std;

int main()
{
int EnemyHealth = 100;


OgreRanSwordStrikesBtl(1,22,&EnemyHealth,"a rat");

cout << EnemyHealth;

}


Header

1
2
3
4
5
6
7
8
9
10
11
#ifndef OGREWEAPONSTRIKES_H
#define OGREWEAPONSTRIKES_H
#include<string>
using std::string;
// this namespace is for holding classes and functions dealing with weapon strikes.

int OgreRanSwordStrikesBtl(int OgreSwordStrikesID, int DamageAmount, int *EnemyHealth, string EnemyName);



#endif // OGREWEAPONSTRIKES_ 


cpp

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
#include<iostream>
#include "OgreTitanium.h"
# include<cstdlib>
#include<string>
#include<ctime>
using std::string;
using namespace std;

//***The following code is for battle weapon strike functions. These functions subtract damage, and return remaining enemy health.

//this is random sword strikes function. it's for random sword strikes.
int OgreRanSwordStrikesBtl(int OgreSwordStrikesID, int DamageAmount, int *EnemyHealth, string EnemyName){



switch (OgreSwordStrikesID){
    //random attack display will determine which text will be displayed for the attack message.
    //if it's a 1 it will generate for light attacks, if it's a 2 it will generate for critical attacks.

    case 1:
    srand(time(0));
    int RandomAttackDisplay;
    RandomAttackDisplay = (rand()%20)+1;


    switch (RandomAttackDisplay){
        *EnemyHealth-=DamageAmount;
        case 1:
        cout << "A light slash doing minimal damage of " << DamageAmount << " to " << EnemyName <<"." <<endl;
        break;
        case 2:
        cout << "A light slash does minimal damage to " << EnemyName <<" " << DamageAmount <<"." << endl;
        break;
        case 3:
        cout << "A light slash cuts " << EnemyName << " for " << DamageAmount <<" damage." << endl;
        break;
        case 4:
        cout << "Sword grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 5:
        cout << "Sword pokes " << EnemyName << " doing " << DamageAmount << " damage. " << endl;
        break;
        case 6:
        cout << "Sword lightly pokes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 7:
        cout << "Blade grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 8:
        cout << "Sword jabs " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 9:
        cout << "Sword lightly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 10:
        cout << "Sword barely grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 11:
        cout << "Blade hardly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 12:
        cout << "Blade lightly jabs " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 13:
        cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 14:
        cout << "Sword hardly pokes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 15:
        cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 16:
        cout << "Blade barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 17:
        cout << "Blade hardly cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 18:
        cout << "Blade lightly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 19:
        cout << "Sword lightly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 20:
        cout << "Sword hardly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        default:
        cout << "There has been an error." << endl << "This is an internal error" << endl;
        cout << "please report the bug to creator." << endl;
    }


    case 2:

    switch (OgreSwordStrikesID){
        *EnemyHealth-=DamageAmount;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
        case 10:
        case 11:
        case 12:
        case 13:
        case 14:
        case 15:
        case 16:
        case 17:
        case 18:
        case 19:
        case 20:
        default:
        cout << "There has been an error. check to make sure" << endl << "your variables are correct and are" << endl;
        cout << "passed into the function in the proper order." << endl;

    }

}

return *EnemyHealth;



}


No errors in this, but function isn't doing what I'd like it to do, lol.
Last edited on
You're not breaking out after the first case in the outer switch.

1
2
3
4
5
6
7
8
    case 20:
    cout << "Sword hardly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
    break;
    default:
    cout << "There has been an error." << endl << "This is an internal error" << endl;
    cout << "please report the bug to creator." << endl;
}
//<- need to put break; here. Otherwise, it will also go into the second case. 


Also, there's no need to return *EnemyHealth; since it also "gets returned" via the pointer.
ugh, this is hard to understand. I thought the 'error message' that I created was coming from the first case. I didn't even want the second case to run, because when I call the function in main, the first argument is OgreSwordStrikesID which is set to 1. That's only supposed to activate the first case. I only want it to go into the second case if that value is 2.

like this...

function is called from main via this line when first argument is 1..

OgreRanSwordStrikesBtl(1,22,&EnemyHealth,"a rat");

run this 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
switch (OgreSwordStrikesID){
    //random attack display will determine which text will be displayed for the attack message.
    //if it's a 1 it will generate for light attacks, if it's a 2 it will generate for critical attacks.

    case 1:
    srand(time(0));
    int RandomAttackDisplay;
    RandomAttackDisplay = (rand()%20)+1;


    switch (RandomAttackDisplay){
        *EnemyHealth-=DamageAmount;
        case 1:
        cout << "A light slash doing minimal damage of " << DamageAmount << " to " << EnemyName <<"." <<endl;
        break;
        case 2:
        cout << "A light slash does minimal damage to " << EnemyName <<" " << DamageAmount <<"." << endl;
        break;
        case 3:
        cout << "A light slash cuts " << EnemyName << " for " << DamageAmount <<" damage." << endl;
        break;
        case 4:
        cout << "Sword grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 5:
        cout << "Sword pokes " << EnemyName << " doing " << DamageAmount << " damage. " << endl;
        break;
        case 6:
        cout << "Sword lightly pokes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 7:
        cout << "Blade grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 8:
        cout << "Sword jabs " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 9:
        cout << "Sword lightly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 10:
        cout << "Sword barely grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 11:
        cout << "Blade hardly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 12:
        cout << "Blade lightly jabs " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 13:
        cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 14:
        cout << "Sword hardly pokes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 15:
        cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 16:
        cout << "Blade barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 17:
        cout << "Blade hardly cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 18:
        cout << "Blade lightly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 19:
        cout << "Sword lightly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 20:
        cout << "Sword hardly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        default:
        cout << "There has been an error." << endl << "This is an internal error" << endl;
        cout << "please report the bug to creator." << endl;
    }


function is called from main when the first argument is 2

OgreRanSwordStrikesBtl(2,22,&EnemyHealth,"a rat");

run this 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
case 2:

    switch (OgreSwordStrikesID){
        *EnemyHealth-=DamageAmount;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
        case 10:
        case 11:
        case 12:
        case 13:
        case 14:
        case 15:
        case 16:
        case 17:
        case 18:
        case 19:
        case 20:
        default:
        cout << "There has been an error. check to make sure" << endl << "your variables are correct and are" << endl;
        cout << "passed into the function in the proper order." << endl;

    }



I tried taking the return line out of the function, and the display is still something like

'sword lightly grazes a rat doing 22 damage
100'

the second number is from main when I cout << EnemyHealth;

I'd like this number to change, considering it's supposed to be the new value after damage is subtracted from EnemyHealth.



Last edited on
Maybe I should trade the first switch statement, which determines which switch is run for an if statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (OgreSwordStrikesID = 1) {

switch here here

}

else if (OgreSwordStrikesId = 2) {

switch here.

}

else {

error message

}
Last edited on
I went ahead and changed the code a bit, but I'm but I'm still facing the same problem. Even with an if statement in my code it still seems to only run the first if statement, skipping the second if the first argument is set to 1, even though it's 2. This is something I don't quite understand, lol...

main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include<string>
#include "OgreTitanium.h"
using namespace std;

int main()
{
int EnemyHealth = 100;


OgreRanSwordStrikesBtl(2,22,&EnemyHealth,"a rat");

cout << EnemyHealth;

}


cpp

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
#include<iostream>
#include "OgreTitanium.h"
# include<cstdlib>
#include<string>
#include<ctime>
using std::string;
using namespace std;

//***The following code is for battle weapon strike functions. These functions subtract damage, and return remaining enemy health.

//this is random sword strikes function. it's for random sword strikes.
int OgreRanSwordStrikesBtl(int OgreSwordStrikesID, int DamageAmount, int *EnemyHealth, string EnemyName){



if (OgreSwordStrikesID=1){
    //random attack display will determine which text will be displayed for the attack message.
    //if it's a 1 it will generate for light attacks, if it's a 2 it will generate for critical attacks.

    srand(time(0));
    int RandomAttackDisplay;
    RandomAttackDisplay = (rand()%20)+1;


    switch (RandomAttackDisplay){
        *EnemyHealth-=DamageAmount;
        case 1:
        cout << "A light slash doing minimal damage of " << DamageAmount << " to " << EnemyName <<"." <<endl;
        break;
        case 2:
        cout << "A light slash does minimal damage to " << EnemyName <<" " << DamageAmount <<"." << endl;
        break;
        case 3:
        cout << "A light slash cuts " << EnemyName << " for " << DamageAmount <<" damage." << endl;
        break;
        case 4:
        cout << "Sword grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 5:
        cout << "Sword pokes " << EnemyName << " doing " << DamageAmount << " damage. " << endl;
        break;
        case 6:
        cout << "Sword lightly pokes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 7:
        cout << "Blade grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 8:
        cout << "Sword jabs " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 9:
        cout << "Sword lightly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 10:
        cout << "Sword barely grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 11:
        cout << "Blade hardly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 12:
        cout << "Blade lightly jabs " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 13:
        cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 14:
        cout << "Sword hardly pokes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 15:
        cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 16:
        cout << "Blade barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 17:
        cout << "Blade hardly cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 18:
        cout << "Blade lightly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 19:
        cout << "Sword lightly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        case 20:
        cout << "Sword hardly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
        break;
        default:
        cout << "There has been an error." << endl << "This is an internal error" << endl;
        cout << "please report the bug to creator." << endl;
    }
}

    else if (OgreSwordStrikesID=2)

    {

        cout << OgreSwordStrikesID;
    }

}


header

1
2
3
4
5
6
7
8
9
10
11
#ifndef OGREWEAPONSTRIKES_H
#define OGREWEAPONSTRIKES_H
#include<string>
using std::string;
// this is where the function prototypes are held.

int OgreRanSwordStrikesBtl(int OgreSwordStrikesID, int DamageAmount, int *EnemyHealth, string EnemyName);



#endif // OGREWEAPONSTRIKES_ 
Last edited on
Topic archived. No new replies allowed.