Question about making a text-based menu class

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 1
Reputation: rustyj110 is an unknown quantity at this point 
Solved Threads: 0
rustyj110 rustyj110 is offline Offline
Newbie Poster

Question about making a text-based menu class

 
0
  #1
Apr 15th, 2008
I am trying to create a text-based menu class to give myself a way to create more dynamic menus in programs. Basically the menu class has a vector of option classes that contain the menu-option's name, function to call etc.

The problem is that I want to be able to associate any function with an option. For example, say that option 1 calls func1( int, string ) and option 2 calls func2( string, ostream&, int ) and so on. Right now I am passing a function pointer but I don't think it is possible to have variable types of arguments with a function pointer.

Here is the code... Any ideas?

  1. /******************************************/
  2. #include "cstdlib"
  3. #include "string"
  4. #include "iostream"
  5.  
  6. using namespace std;
  7.  
  8. class Option
  9. {
  10. private:
  11. int option_number; // for display and vector purposes
  12. string name; // title for user to read
  13. void (*func_pointer)( void ); // function to be executed
  14.  
  15. public:
  16. Option( int option_number, string name, void (*func_name)() ); // constructor, fills option vars
  17.  
  18. void callFunction(); // calls function stored in option
  19.  
  20. // Getters
  21. int getOptionNumber();
  22. string getOptionName();
  23. void (*getFunctionPointer())();
  24. };
  25.  
  26. class MenuType
  27. {
  28.  
  29. private:
  30. vector<Option> options;
  31.  
  32. public:
  33. MenuType();
  34. void addOption( int option_number, string name, void (*func_name)() );
  35. void displayMenu();
  36.  
  37. };
  38.  
  39. /******class Option******/
  40.  
  41. Option::Option( int option_num, string option_name, void (*func_name)() )
  42. {
  43. option_number = option_num;
  44. name = option_name;
  45. func_pointer = func_name;
  46. }
  47.  
  48. void Option::callFunction()
  49. {
  50. if ( func_pointer != NULL )
  51. (*func_pointer)();
  52. }
  53.  
  54. // Getters
  55. int Option::getOptionNumber()
  56. {return option_number;}
  57.  
  58. string Option::getOptionName()
  59. {return name;}
  60.  
  61. void (*Option::getFunctionPointer())()
  62. {return func_pointer;}
  63.  
  64. /******class MenuType******/
  65.  
  66. MenuType::MenuType()
  67. {}
  68.  
  69. void MenuType::addOption( int option_number, string name, void (*func_name)() )
  70. {
  71. Option newoption( option_number, name, func_name );
  72. options.push_back( newoption );
  73. }
  74.  
  75. void MenuType::displayMenu()
  76. {
  77. for ( int i = 0 ; i < options.size() ; i++ )
  78. {
  79. cout<<options[i].getOptionNumber()<<". "<<options[i].getOptionName()<<endl;
  80. }
  81.  
  82. cout<<"Enter a menu option: ";
  83. int option_choice;
  84. cin>>option_choice;
  85. cin.ignore();
  86.  
  87. options[option_choice-1].callFunction();
  88. }
  89.  
  90. /***** Used in a program like this *****/
  91. int main()
  92. {
  93. MenuType first_menu;
  94. first_menu.addOption( 1, "option 1", function ); // but here i would like to be able to put function( arg1, arg2 ). how?
  95. first_menu.addOption( 2, "option 2", function2 ); // but be able to put function2( arg3, arg4, arg5 ) here
  96. first_menu.addOption( 3, "option 3", function3 );
  97. first_menu.displayMenu();
  98. }
Is there a better way to do this? I'm sure there is.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 365 | Replies: 0
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC