Choosing random subfunction from array

Hey I would like to ask how can I get a random subfunction to open when I click a button.
Like using subfunctions to display different text everytime you press that button
Sorry for the capitals in the code im using my phone
What I have for now

It always shows all the functions
1
2
3
4
5
6
7
void ExploreFiore()
{
system("cls");
int randf[4] = {AngryErza(),MeetNatsu(),MeetGray(),MeetLucy()}
}
Int randindex = rand() % 4;
Cout<<randf[randindex];
Last edited on
are you wanting to call a random function from a set?
you can do this with function pointers, which it looks like you are already attempting. Is that what you want to see, such that

randf[index] actually calls a function?

I am not sure what you are saying it is doing. It should just write out one integer from this code snip.

Last edited on
What im trying to do is a text adventure game.
Lets say like you have 2 options:
1: do the mission
2: explore the city

And when you type 2 another function open and a random text appears everytime you press keep exploring.
Like:
MeetNatsu()
{
Cout<<"conversation";
}

Next time you press explore the city a different function starts
MeetGray() {}
ok. I dislike function pointers, so personally I would do this:

int rndsel = rand()% 4; //consider making 4 a constant somewhere.
switch (rndsel)
{
case 1: MeetGray(); break;
case 2: MeetNatsu(); break;
... etc
default:
; //this can't actually happen, because math, but you always want a default. Maybe put an error message here just for posterity.
}

there are several other ways to do this, depending on what you have. As I said, function pointer can do it, but that is kind of c-ish and avoidable. If the functions are simple and just print a few canned strings and read a response, you can drop the strings into a container and have a single function handle them in a reusable manner. But if there are different logical constructs etc this may not work. You can also do it with object oriented coding in various ways, such as a top class that inherits a conversation (this is like the container/function approach, but you can make a conversation as complex as needed with some effort). Just some ideas depending on where your game is going and what design fits your goals.




Last edited on
Thanks alot!
I'll repair my code asap. For now on my mobile app it works just fine.
Topic archived. No new replies allowed.