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:
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";
}
elseif (damage[rand()%3] = 1) {
cout << "Xemnas took out " << xdmg << " of your health!\n";
}
elseif (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.
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.
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();
}
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.