📄 menu.cpp
字号:
//***********************************************
//* Menu.cpp: implementation of the Menu class. *
//***********************************************
#include <iostream.h>
#include "Menu.h"
void EatKeys() // To clean up the input stream func
{
while ( cin.rdbuf()->in_avail() > 0 ) cin.ignore(1);
}
Menu::Menu() // Default constructor
{
mTitle[0] = 0; // Set Title to zero
mItemNumber = 0; // Set Items to zero
for ( int i = 0; i < MAX_MENU_NUMBER; i++ ) // set Max Menu up to 20
mMenuItems[i] = NULL; // set the array to null
}
Menu::Menu( char * Title, char * CharItems[], int NoOfItems )
{
SetTitle ( Title ); // Call SetTitle function
for ( int i = 0; i < NoOfItems; i++ ) // The Items and # of Items
{
mMenuItems[i] = new char [strlen(CharItems[i])+1];
strcpy ( mMenuItems[i], CharItems[i]);
}
mItemNumber = NoOfItems;
}
Menu::~Menu() // Destructor
{
for ( int i = 0; i < mItemNumber; i++ )
{
if ( mMenuItems[i] != NULL )
{
delete [] mMenuItems[i]; // To delete MenuItems from the memory
mMenuItems[i] = NULL;
}
}
mItemNumber = 0;
}
void Menu::Display() // Display Menu info func
{
cout<<"\t\t\t"<<mTitle<<endl<<endl; // Display the Menu Title
for ( int i = 0; i < mItemNumber; i++ ) // The Menu Items
cout<<"\t\t\t"<<i+1<<' '<<mMenuItems[i]<<endl;// number + items
cout<<"\n\t\t\tSelect An Option\n\t\t\tOr Press 0 To Return: ";// user options
}
int Menu::MakeChoice()
{
int c;
for(;;)
{
EatKeys(); // Clean any junks!!!
clrscr(); // Clear screen function
Display(); // Diplay function
cin>>c;
cin.get(); //'Enter' key
if ( 0 <= c && c <= mItemNumber ) // Selected item starting from 0
return c-1 ; // Returns -1 if cancel
cout << "\n\t\t\tWrong Selection !\n\t\t\tPress ENTER To Continue\n";
while ( cin.get() != '\n' );
}
}
//************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -