How do you randomly choose a function?

I'm new to C++, and not very good in it, but I'm trying to create this mini game and I need the system to choose a set of functions randomly. I've seen posts about choosing regular values so I tried the same with functions. Here's what I tried:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int tkdmg() {
	int ansemhit(); {
		hp = hp - admg;
	}
	int xemnashit(); {
		hp = hp - xdmg;
	}
	int bothatk(); {
		hp = hp - (admg + xdmg);
	}
	int *damage[3] = {{ansemhit()},{xemnashit()},{bothatk()}};
	if (damage[rand()%3] = 0) {
		cout << "Ansem took out " << admg << " of your health!\n";
	}
	else if (damage[rand()%3] = 1) {
		cout << "Xemnas took out " << xdmg << " of your health!\n";
	}
	else if (damage[rand()%3] = 2) {
		cout << "Xemnas and Ansem both took out " << admg + xdmg << " of your health!\n";
	}
}


It didn't work, so can somebody explain to me if it's possible? Thanks in advance.
int (*damage[3])() = {ansemhit, xemnashit, bothatk};
Alright then, that helped, but now it says that there's an error:

1>------ Build started: Project: cmd game, Configuration: Debug Win32 ------
1>  cmdgame.cpp
1>c:\users\roni\c++ stuff\cmd game\cmd game\cmdgame.cpp(20): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
1>c:\users\roni\c++ stuff\cmd game\cmd game\cmdgame.cpp(103): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\roni\c++ stuff\cmd game\cmd game\cmdgame.cpp(148): error C2556: 'int tkdmg(void)' : overloaded function differs only by return type from 'void tkdmg(void)'
1>          c:\users\roni\c++ stuff\cmd game\cmd game\cmdgame.cpp(33) : see declaration of 'tkdmg'
1>c:\users\roni\c++ stuff\cmd game\cmd game\cmdgame.cpp(148): error C2371: 'tkdmg' : redefinition; different basic types
1>          c:\users\roni\c++ stuff\cmd game\cmd game\cmdgame.cpp(33) : see declaration of 'tkdmg'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


So, I tried changing it to void, but void can't hold arrays. I'm lost now.
Have you declare tkdmg() to return void somewhere? It should be int.
Hi,

I would do some thing like following

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

void ansemhit(){
	// do some thing
	cout << "Ansem took out " << admg << " of your health!\n";
}

void xemnashit(){
	// do some thing
	cout << "Xemnas took out " << xdmg << " of your health!\n";;
}

void bothatk(){
	// do some thing
	cout << "Xemnas and Ansem both took out " << admg + xdmg << " of your health!\n";
}

struct funcs {
	void(*func)();
	funcs(void(*func)()) :	func (func ){}
};

void tkdmg() {
	
	srand ( time(NULL) );
	funcs ranFunc[] = {ansemhit, xemnashit, bothatk};
	ranFunc[rand()%3].func();

}


hope it helps
Last edited on
Yep, that script fixed it. Before the output was claiming there was a double to int conversion, but now it's gone. Also, I did declare void somewhere, fixed that also.

Thanks everyone!
Topic archived. No new replies allowed.