I am have been self teaching C++ for a few years(off and on). I do this by attempting to create simple programs. It has just occured to me that every program I write has a menu. I am sooooo tired of re-coding menus for every app.
So the question starts:
I want to be able to write a menu for any app like so...
1 2 3 4 5 6 7 8 9 10 11 12
#include "menu_maker.h"
int main()
{
menu_maker MyMenu;
MyMenu.title("Main Menu");//Displays name of menu
MyMenu.addOption(1,"Create player",Player.create_character());//Add option addOption(int position, string option, class or function to call)
MyMenu.addOption(2,"Choice two",player.foo2());
MyMenu.addOption(3,"Choice three",player.foo3());
MyMenu.addOption(4,"Choice four",player.foo4);
MyMenu.Display();
}
I want it to display something like this:
****Main Menu****
1) Create player
2) Choice two
3) Choice three
4) Choice four
Choose an option (1-4)
>1
[The program calls player.createcharacter() here]
I could rough a header file out. But I need to know how to be able to call a class member function and setup the addOption() member function in my menu_maker class to pass in the required info.
I hope this question isn't too involved for one post. Thanks ahead of time.
You write this by adding a collection of some sort to your menu maker class. Since you have several data items per option, the best is to also create a MenuOption class, and then make addOption() receive instances of the MenuOption class. Then the addOption() method would take that MenuOption object and add it to the collection.
You have a choice of several collections using the STL, but in this particular case, and assuming you provide operator<() for MenuOption, I'd recommend that you use a std::vector<MenuOption> to hold all items. You can use std::sort() (along with MenuOption::operator<()) to sort the menu options by position (or whatever order you like).
Get something simple like this working to start with. Once you have done that much, you could address the problems with the inflexibility of this design - all operations are member functions which take no arguments, without cv-qualifiers, invoked on the same object.
There, we could sink out teeth into some interesting aspects of C++ - polymorphic call-wrappers with lazy evaluation (std::function<>) and generic binders (std::bind<>()).
I compiled the class you made to see how it works so I could adapt it. I came up with some errors:
||=== LittleRPG, Debug ===|
F:\CodeBlocks-Programs\LittleRPG\LRPG_Functions.h||In member function 'void menu_maker<T>::display()':|
F:\CodeBlocks-Programs\LittleRPG\LRPG_Functions.h|24|warning: 'auto' will change meaning in C++0x; please remove it [-Wc++0x-compat]|
F:\CodeBlocks-Programs\LittleRPG\LRPG_Functions.h|24|error: ISO C++ forbids declaration of 'p' with no type [-fpermissive]|
F:\CodeBlocks-Programs\LittleRPG\LRPG_Functions.h|24|error: range-based-for loops are not allowed in C++98 mode|
F:\CodeBlocks-Programs\LittleRPG\LRPG_Functions.h|25|error: request for member 'first' in 'p', which is of non-class type 'const int'|
||=== Build finished: 3 errors, 1 warnings (0 minutes, 1 seconds) ===|