Original Post
i'm working on a console game, and am trying to implement a menu system, what i have so far is a function that will draw out the menu and take the input and return a value based on the menu choice, and i wanna use that return value as the parameter of another function which will perform actions based on what the choice was (play, load, credits, quit). my first attempt was to chuck the function as a normal function call in the parameter assuming that the return would be used as the parameter, but what actually happens is that the return of the functions used as the parameter will cause the calling function to return with the same value as well.
i'm assuming, therefore that i will need to use function pointers to pass the function in as a parameter and get the return value that way, however, for some reason my function parameters aren't working. any help would be much appreciated.
the error i am getting:
error: a value of type int (*)() cannot be used to initialize an entity of type int
thanks
i'm assuming, therefore that i will need to use function pointers to pass the function in as a parameter and get the return value that way, however, for some reason my function parameters aren't working. any help would be much appreciated.
//prototypes
int menu();
int DoAction(int (*choice)());
//function implementation
int menu()
{
int x = 14;
int y = 0;
char input = '\0';
cout << "1: new game" << endl
<< "2: load game" << endl
<< "3: credits" << endl
<< "4: quit" << endl;
gotoxy(x, y);
cout << "<-";
while(true)
{
if(_kbhit())
{
input = _getch();
switch(input)
{
case UP_ARROW:
if(y > 0 && y < 4)
{
gotoxy(x, y);
cout << " ";
y--;
gotoxy(x, y);
cout << "<-";
break;
}
break;
case DOWN_ARROW:
if(y > -1 && y < 3)
{
gotoxy(x, y);
cout << " ";
y++;
gotoxy(x, y);
cout << "<-";
break;
}
break;
case 13: //enter was pressed
switch(y)
{
case 0:
return 0;
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
default:
break;
}
default:
break;
}
}
}
return 0;
}
int DoAction(int (*choice)())
{
int ch = (*choice)();
switch(ch)
{
case 0:
case 1:
case 2:
case 3:
exit(0);
default:
break;
}
return 0;
}
//function use (in main.cpp) - causes error: a value of type int (*)() cannot be used to initialize an entity of type int
int DoAction(&menu);
the error i am getting:
error: a value of type int (*)() cannot be used to initialize an entity of type int
thanks