Passing variables through void?

i need to pass the variables x and y from function 1 to function 2 so its the same number i also need the variables in another function as well
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 void Function1(){
 
    srand(time(NULL));
    int x = rand() % 2;
    int y = rand() % 5 ;
}

 void Function2(){
 
std::string NPC[2][5] = {
        {"Ogre","Goblin","Salamander","Moss Giant","Ghost"},
        {"Troll","Demon","Imp","Cow","skeleton"},

cout<< NPC[x][y];
}

Thanks to anyone that can help :)
do not decare 2 ints inside your function. instead pass two int references into your functions.


wait a second. what exactly do you mean by "Passing variables through void"?
Last edited on
basically i need x and y to be randomized once then i need it in many different functions
it can't be a global variable though
i cant think how i would do it

so if x = 1 and y = 4 in function1 then i want "Moss Giant to appear in function2

but for function 3 its going to get the stats of moss giants

i hope this helps you understand what im taking about,

also this is in a .h file that will be loaded to main
Illustration of concept:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void Function1(int &x, int &y)
{
    ...
    x = rand() % 2;
    y = rand() % 5 ;
    ...
}

void Function2(int x, int y);

int main()
{
    ...
    int x, y;
    Function1(x, y);
    Function2(x, y);
    ...
    return 0;
}
Topic archived. No new replies allowed.