Getting variables from different functions

Ok so im making this dinosaur battle game and i need to get: A from A = rand()
because i need to use it in a different function and i dont know how to do that, i used to but then i forgot. so how do i access variables in different function is m question, can you please include an example showing me, here is my code if you need it.

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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <iostream>
#include <string>
#include <ctime>
#include <random>

using namespace std;

void MAINPROGRAM();
void DinosaurRandomize();
void TRex();
void Player();
void Entrance();
void VRaptor();
void Hospital();

int PlayerHealth = 100;
int TRexHealth = 100;
int VRaptorHealth = 100;
int opponentH = 0;

bool turn = true;


int main()
{

}

void Player()
{
    int Shotgun = 5;
    int Knife = 3;
    int Fist = 1;
    int RocketLauncher = 7;

    string choice;

        cout << "1. Use Shotgun - 5 DMG" << endl;
        cout << "2. Use Knife - 3 DMG" << endl;
        cout << "3. Use Fist - 1 DMG" << endl;
        cout << "4. Use Rocket Launcher - 7 DMG" << endl;
        cin >> choice;

    while(turn != false)
    {
        if(PlayerHealth <= 0)
        {
            cout << "You have lost! Press ENTER to go back to the entrance." << endl;
            Hospital();
        }

        if(choice == "1." || choice == "1")
        {
            cout << "\n";
            cout << "You used the Shotgun!" << endl;
            cout << "\n";
            opponentH -= 5;
            cout << "T-Rex's Health: " << TRexHealth << endl;
            turn == false;
            TRex();
        }
        else if(choice == "2." || choice == "2")
        {
            cout << "\n";
            cout << "You used the Knife!" << endl;
            cout << "\n";
            opponentH -= 3;
            cout << "T-Rex's Health: " << TRexHealth << endl;
            turn == false;
            TRex();
        }
        else if(choice == "3." || choice == "3")
        {
            cout << "\n";
            cout << "You used your fist!" << endl;
            cout << "\n";
            opponentH -= 1;
            cout << "T-Rex's Health: " << TRexHealth << endl;
            turn == false;
            TRex();
        }
        else if(choice == "4." || choice == "4")
        {
            cout << "\n";
            cout << "You used the Rocket Launcher" << endl;
            cout << "\n";
            opponentH -= 7;
            cout << "T-Rex's Health: " << TRexHealth << endl;
            turn == false;
            TRex();
        }
    }
}


void DinosaurRandomize()
{
    time_t T;
    time(&T);
    srand(T);

    int A;

    for(int R = 0; R < 3; ++R)
    {
        A = rand() % 2;
    }

    switch(A)
    {
        case 0:
            TRex();
            break;
        case 1:
            VRaptor();
            break;
    }
}


void TRex()
{
    int X;

    time_t T;
    time(&T);
    srand(T);

    for(int R = 0; R < 1; ++R)
    {
        X = rand() % 5;
    }

    int Bite = 5;
    int Slash = 6;
    int Pounce = 4;
    int Quick Attack = 5;

    if(TRexHealth <= 0)
        {
            cout << "\n";
            cout << "You have defeated the T-Rex!!!" << endl;
            cout << "\n";
            cin.get();
            Entrance();
        }

        switch(X)
        {
            case 0:
                cout << "\n";
                cout << "T-Rex used Bite!" << endl;
                cout << "\n";
                PlayerHealth -= 5;
                cout << "Your Health: " << PlayerHealth << endl;
                cout << "\n";
                Player();
                break;
            case 1:
                cout << "\n";
                cout << "T-Rex used Stomp!" << endl;
                cout << "\n";
                PlayerHealth -= 4;
                cout << "Your Health: " << PlayerHealth << endl;
                cout << "\n";
                Player();
                break;
            case 2:
                cout << "\n";
                cout << "T-Rex used Charge!" << endl;
                cout << "\n";
                PlayerHealth -= 5;
                cout << "Your Health: " << PlayerHealth << endl;
                cout << "\n";
                Player();
                break;
            case 3:
                cout << "\n";
                cout << "T-Rex used Chew!" << endl;
                cout << "\n";
                PlayerHealth -= 7;
                cout << "Your Health: " << PlayerHealth << endl;
                cout << "\n";
                Player();
                break;
            case 4:
                cout << "\n";
                cout << "T-Rex's attack missed!" << endl;
                cout << "\n";
                PlayerHealth -= 0;
                cout << "Your Health: " << PlayerHealth << endl;
                cout << "\n";
                Player();
                break;
    }
}


void VRaptor()
{
    int X;

    time_t T;
    time(&T);
    srand(T);

    for(int R = 0; R < 1; ++R)
    {
        X = rand() % 5;
    }

    int Bite = 5;
    int Stomp = 4;
    int Charge = 5;
    int Chew = 7;

    if(VRaptorHealth <= 0)
        {
            cout << "\n";
            cout << "You have defeated the Veloci Raptor!!!" << endl;
            cout << "\n";
            cin.get();
            Entrance();
        }

        switch(X)
        {
            case 0:
                cout << "\n";
                cout << "Raptor used Bite!" << endl;
                cout << "\n";
                PlayerHealth -= 5;
                cout << "Your Health: " << PlayerHealth << endl;
                cout << "\n";
                Player();
                break;
            case 1:
                cout << "\n";
                cout << "Raptor used Stomp!" << endl;
                cout << "\n";
                PlayerHealth -= 4;
                cout << "Your Health: " << PlayerHealth << endl;
                cout << "\n";
                Player();
                break;
            case 2:
                cout << "\n";
                cout << "Raptor used Charge!" << endl;
                cout << "\n";
                PlayerHealth -= 5;
                cout << "Your Health: " << PlayerHealth << endl;
                cout << "\n";
                Player();
                break;
            case 3:
                cout << "\n";
                cout << "Raptor used Chew!" << endl;
                cout << "\n";
                PlayerHealth -= 7;
                cout << "Your Health: " << PlayerHealth << endl;
                cout << "\n";
                Player();
                break;
            case 4:
                cout << "\n";
                cout << "Raptor's attack missed!" << endl;
                cout << "\n";
                PlayerHealth -= 0;
                cout << "Your Health: " << PlayerHealth << endl;
                cout << "\n";
                Player();
                break;
    }
}

void Hospital()
{

}
Last edited on
You can't use a variable from one function in another. That is why they are called local.
There are two things that you can do:
1) Return the value of the variable you want from the function via the return statement. You need to make sure that you call the generating function from the function that needs the value.
2) Assign the value you need to a global variable. Then you can use the value anywhere you like. Although if you have to do this, your program is probably structured poorly. You should prefer 1).

By the way, what do you expect
1
2
3
4
for(int R = 0; R < 3; ++R)
{
    A = rand() % 2;
}
to do? A is just one variable. Reconsider it.
Topic archived. No new replies allowed.