So I'm making a simple console project using data structures but the thing here I want to know which way will be the best for implementing a console Menu, I want to know what is the best practice for it, this is my actual menu
void MainMenu(){ while(1)
{ int opt; cout << endl << "----------------------------" << endl; cout << endl << " Main Menu " << endl; cout << endl << "----------------------------" << endl; cin >> opt; cout << "\n" << endl; switch (opt) { case 1: SubMenu(); break; case 2: //Code break; case 0: exit(1); default: break; }
}And a submenu example would be this one
void Submenu()
{
bool exit=true;
while(exit)
{ int opt; cout << endl << "----------------------------" << endl; cout << endl << " SubMenu " << endl; cout << endl << "----------------------------" << endl; cin >> opt; switch (opt) { case 1: //Code break; case 0: exit = false; }
}I want to know if this is a good way of implementing a menu or if there's a better way to do it, also I would like to know where can I find books or documentation in order to code in a proper way to make my program not to use a lot of resources from the PC.
42 Answers
IMHO, table driven console menus are very good.
They allow for constant data (which can be stored in Read-Only Memory).
They are data driven (you don't need to add code to add more options).
The "engine" or driver can be tested once; if you use the same structure, you can use one function to process many menus.
typedef void (*Menu_Processing_Function_Pointer)(void); struct Menu_Option { char choice; char const * p_selection_text; Menu_Processing_Function_Pointer p_processing_function; }; void Process_Selection_One(); void Process_Selection_Two(); static const Menu_Option main_menu[] = { {'1', "Option 1", Process_Selection_One}, {'2', "Option 2", Process_Selection_Two}, }; static const size_t quantity_selections = sizeof(main_menu) / sizeof(main_menu[0]); int main() { static const char menu_title = "\n" "------------------------------\n" " Main Menu\n" "------------------------------\n" ; cout.write(menu_title, sizeof(menu_title) - 1); for (size_t i = 0; i < quantity_selections; ++i) { std::cout << main_menu[i].p_selection_text << "\n"; } cout << "Enter selection, 0 to quit: "; char choice; cin >> choice; for (size_t i = 0; i < quantity_selections; ++i) { if (choice == main_menu[i].choice) { Menu_Processing_Function_Pointer p_function = main_menu[i].p_processing_function; (p_function)(); break; } } return EXIT_SUCCESS; }You'll have to implement looping for when the selection is invalid.
The above is the gist of the technique.
i Would use other loops then switch case:
void MainMenu(){ char coice=''; while(coice !='#') { int opt; cout << endl << "----------------------------" << endl; cout << endl << " Main Menu " << endl; cout << endl << "----------------------------" << endl; cin >> opt; cout << "\n" << endl; if(coice =='1') { SubMenu(); } if(coice =='2') { //Code } } exit(1);
}As far as you dont chose to end die Programm aktively it wont be end and its more clean.
The same way you can make your submenu, if you want.
4