void inline pause() //a simple puase function that can be called anywhere
{
gotoxy(20,23);
cout << "Press any key to continue";
getche();
}
struct menuitem //create a struct of 2 variables,
{
char name[20]; //selection name for the screen
void (*function)(); //the associated function for the user's selection
};
class menu
{
menuitem *option;
//declares a pointer variable named *OPTION, of the datatype MENUITEM.
int numoptions; //Number of options available
int selected; //boolean flag
void drawmenu(); //declaration of DRAWMENU function
void selectoption(); //declaration of SELECTOPTION function
public:
int validexit; //is TRUE if user wants to exit
char selection; // User's keyboard input.
menu() {numoptions = 0;} //default constructor, that's overloaded
menu (int, menuitem *); //setup for the real constructor
//INT will be sizeofarray, and a pointer variable of the type MENUITEM
void runmenu(); //function declaration that will control program flow
};
void menu::drawmenu()
{
textcolor(YELLOW); textbackground(BLACK); //set the main screen colors
_setcursortype(_NOCURSOR); //turn off the cursor
clrscr();
gotoxy(25,10); cputs("Pick your poison...");
for (int x=0; x<=numoptions; x++)
{
gotoxy(25,12+x); //screen position for option x
if(selection==x)
{
textcolor(WHITE); textbackground(LIGHTRED);
//makes user's selection white on red
}
else
{
textcolor(LIGHTGRAY); textbackground(BLACK);
//returns unselected items back to normal colors
}
cputs (option[x].name); //place selection on screen
}
}//END menu:drawmenu
void menu::selectoption()
{
char k;
k=getche(); //trap input from keyboard
switch(k)
{
case 72: selection--; break; //UP arrow
case 80: selection++; break; //DOWN arrow
case 13: selected=1; //ENTER key
}
if (selection<0) selection=numoptions;
//if lightbar on FIRST option and user hits UP key,
//move the lightbar to the LAST option on the screen
if (selection>numoptions) selection=0;
//if lightbar on LAST option and user hits DOWN key,
//move the lightbar to the FIRST option on the screen
if (selection==numoptions&&selected==1)
{
validexit=1; //tells prog that user wants to quit
}
else
{
(*option[selection].function)(); //execute user's selection
selected=0; //makes selected FALSE again
}
}//END menu::menuselection
void menu::runmenu()
{
do
{
drawmenu();
selectoption();
}
while (!validexit);
}//END menu::runmenu
menu::menu (int sizeofarray, menuitem menudata[])
{
numoptions = sizeofarray / sizeof(menuitem);
option = menudata;
strcpy(option[numoptions].name,"Quit");
}//END menu::menu