calling class functions by random?

Hi,
this is my first post so I apologise if I'm doing something wrong.
I got a problem calling a function in a class from lots of functions by generating a random number. What my idea of doing it is to generate a random number and then callin the function in like this: object.'function'+random();
but it doesn't work.
Please help me with this.
I made a simple code to show and ask what i need and what I mean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "px.h"
#include <iostream>
using namespace std

void betkas::betkoks1(){
	cout<<"something"<<endl;
}

void main(){
	betkas pradedam; // we create an object here
	int random = 1; //lets pretend this is a random number
	pradedam. + 'betkoks' + random(); // this is the part I'm trying to call the function betkoks1
	system("PAUSE");             // what am I doing wrong?
}


P.S. using Microsoft Visual Studio 2008
Last edited on
You can't create symbols during runtime in C++ because it is a compiled language.
that was only my edit mistake, but how about my real problem?
how to call the functio like that ? is it possible? or is there another way to do it?
1
2
3
4
5
6
7
8
if(random==1)
{
pradedam.betkoks1();
}
else if(random==2)
{
pradedam.betkoks2(); // betkoks2 must exist
};
Last edited on
thats not what I need, maby I didn't specify what do I need corectly, I'll try it again :
this is something like a quiz program that gives you random questions and you need to state correct answers, of course it's only a fraction of the program
1
2
3
4
5
6
buvo:
	Sleep(1000); //for random generation..
	nr=pradedam.betkoks(); //calls a function that generates random
	if ('kl'+nr == 1) goto buvo; // checks if the question was already asked, if it was then
	//it goes to the buvo part to generate another random till it gets the random question that wasn't asked
	pradedam.'klausimas'+nr (); //when we know the question wasn't asked we try to ask it but it doesn't work 
Last edited on
Why don't you store the questions in a container and then show them without a specific function for each question?
After storing them, you can use the algorithm random_shuffle to have them at random position in the container.
http://www.cplusplus.com/reference/stl/
http://www.cplusplus.com/reference/algorithm/random_shuffle/
becouse my assignment is to make it as simple as possible, and I think that my way is quite simple (primitive) but there's that problem - i don't know how to merge a word with a variable into a function name and then call that function succsessfuly. Is it possible ?
It is not possible.

In your case "simple as possible" is probably a switch statement.

yah, maby you'r right
thnx 4 replies
Topic archived. No new replies allowed.