Menu handle program

Discussion in 'C++' started by Gary Wessle, Nov 7, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    Hi

    I need help organizing this program in the right way.
    I included the code below which compiles and runs and gives the
    desired effect to a certain point, but I don't know what the next step
    is to finish this program.

    the program presents Main menu to the user and gives a prompt, the
    user types the number corresponding to an item from the menu, the
    program then
    1) acts on this selection by calling a handle-function which
    spawns a thread and runs a corresponding function,
    2) or presents another menu for more specific selection by the user, a
    prompt for selection and a repeat of (1) above.

    I am thinking to use a base class "Menu" which holds common attributes
    and operations and derived classes "Main" "Edit" "View" ...
    the items for each menu are stored in a file named after the menu, i.e
    menus/main.txt, menus/edit.txt ...

    my problem is, presented here so that you keep it in mind while
    reading the below; Menu::prompt() fulfills number (1) need above but
    how to do number (2) in a creative way?

    thanks a bunch

    // menu.h ****************
    #ifndef MENU_H
    #define MENU_H

    #include <string>

    class Menu {
    protected:
    std::string menus_dir;
    short opt;
    bool go_on;
    std::string ask;

    public:
    Menu();
    virtual void print() = 0; // print a menu
    void prompt(); // give a prompt
    virtual void handle() = 0; // call a handle
    };


    class Main: public Menu {
    std::string m_itms;

    public:
    Main();
    void print();
    void handle();
    };


    #endif


    //menu.cpp ****************
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    #include <fstream>
    using std::ifstream;
    #include <string>
    using std::string;

    #include "menu.h"


    Menu::Menu()
    : menus_dir( "menus/" ),
    ask( "Please choose an option from the menu:\n > " ),
    opt( true )
    {}

    void Menu::prompt(){ // in question ????????????????
    while( go_on ) {
    cout << ask;
    std::cin >> opt;

    switch( opt ){
    case ( 1 ): cout << "handle 1 thread"; break;
    default: cout << ask;
    }
    }
    }


    Main::Main()
    : Menu(),
    m_itms( "main.txt" )
    {}

    void Main::print(){
    string x = menus_dir+m_itms;
    ifstream in( x.c_str() );
    string line;
    while( getline(in, line) ) {
    cout << line << endl;
    }
    Menu::prompt();
    }

    void Main::handle(){
    cout << "handle thread started\n";
    }


    //main.cpp ****************
    #include "menu.h"

    int main() {
    Main mm;
    mm.print();

    }
    Gary Wessle, Nov 7, 2006
    #1
    1. Advertising

  2. Gary Wessle

    JoShCrUz Guest

    Would be interesting to understand what is the overall problem you are
    trying to solve !

    Managing threads sounds like over complicated (i.e. user chooses menu
    option 1.A ... thread X1A is then created ... shall the option 1.A. be
    unavailable until thread X1A dies ? what happens if two threads of the
    class are running ? Will they be competing for a specific resource
    (i.e. keyboard input?)

    Maybe that part of the solution be better left as asynchronous calls or
    just synchronous callbacks, but then again, there is no light about the
    problem you are trying to solve.

    If you are building a generic library for menus, the file mechanism can
    be of use, but introduces the problem of how to assign actions to the
    items in the menu :) which opens another pandora's box.

    Maybe creating a class that receives as parameters arrays of entries {
    ParentID, ID, Display Text, pointer to function() } gives you a generic
    structure, ParentID = 0 could mean main menu, so you can create the
    hierarchy, then you can pass pointers to functions for the actions
    (maybe the function has as parameter the callback to be called when it
    has completed !)

    Hope this helps,

    Gary Wessle wrote:
    > Hi
    >
    > I need help organizing this program in the right way.
    > I included the code below which compiles and runs and gives the
    > desired effect to a certain point, but I don't know what the next step
    > is to finish this program.
    >
    > the program presents Main menu to the user and gives a prompt, the
    > user types the number corresponding to an item from the menu, the
    > program then
    > 1) acts on this selection by calling a handle-function which
    > spawns a thread and runs a corresponding function,
    > 2) or presents another menu for more specific selection by the user, a
    > prompt for selection and a repeat of (1) above.
    >
    > I am thinking to use a base class "Menu" which holds common attributes
    > and operations and derived classes "Main" "Edit" "View" ...
    > the items for each menu are stored in a file named after the menu, i.e
    > menus/main.txt, menus/edit.txt ...
    >
    > my problem is, presented here so that you keep it in mind while
    > reading the below; Menu::prompt() fulfills number (1) need above but
    > how to do number (2) in a creative way?
    >
    > thanks a bunch
    >
    > // menu.h ****************
    > #ifndef MENU_H
    > #define MENU_H
    >
    > #include <string>
    >
    > class Menu {
    > protected:
    > std::string menus_dir;
    > short opt;
    > bool go_on;
    > std::string ask;
    >
    > public:
    > Menu();
    > virtual void print() = 0; // print a menu
    > void prompt(); // give a prompt
    > virtual void handle() = 0; // call a handle
    > };
    >
    >
    > class Main: public Menu {
    > std::string m_itms;
    >
    > public:
    > Main();
    > void print();
    > void handle();
    > };
    >
    >
    > #endif
    >
    >
    > //menu.cpp ****************
    > #include <iostream>
    > using std::cout;
    > using std::cin;
    > using std::endl;
    > #include <fstream>
    > using std::ifstream;
    > #include <string>
    > using std::string;
    >
    > #include "menu.h"
    >
    >
    > Menu::Menu()
    > : menus_dir( "menus/" ),
    > ask( "Please choose an option from the menu:\n > " ),
    > opt( true )
    > {}
    >
    > void Menu::prompt(){ // in question ????????????????
    > while( go_on ) {
    > cout << ask;
    > std::cin >> opt;
    >
    > switch( opt ){
    > case ( 1 ): cout << "handle 1 thread"; break;
    > default: cout << ask;
    > }
    > }
    > }
    >
    >
    > Main::Main()
    > : Menu(),
    > m_itms( "main.txt" )
    > {}
    >
    > void Main::print(){
    > string x = menus_dir+m_itms;
    > ifstream in( x.c_str() );
    > string line;
    > while( getline(in, line) ) {
    > cout << line << endl;
    > }
    > Menu::prompt();
    > }
    >
    > void Main::handle(){
    > cout << "handle thread started\n";
    > }
    >
    >
    > //main.cpp ****************
    > #include "menu.h"
    >
    > int main() {
    > Main mm;
    > mm.print();
    >
    > }
    JoShCrUz, Nov 7, 2006
    #2
    1. Advertising

  3. Gary Wessle

    Gary Wessle Guest

    "JoShCrUz" <> writes:

    > Would be interesting to understand what is the overall problem you are
    > trying to solve !


    presenting the user with menu/submenu to choose from, run a function
    according to the choice, such function is run while the user gets a
    different menu to choose from and the prompt, such "second" different
    menu will not have an option to run the same function again but other
    options plus quit to go back to the parent menu.

    >
    > Managing threads sounds like over complicated (i.e. user chooses menu
    > option 1.A ... thread X1A is then created ... shall the option 1.A. be
    > unavailable until thread X1A dies ?


    will be unavailable by virtue of "no option to rerun it" from the
    submenu.


    > what happens if two threads of the
    > class are running ? Will they be competing for a specific resource
    > (i.e. keyboard input?)
    >

    this problem will not exist, not above.

    > Maybe that part of the solution be better left as asynchronous calls or
    > just synchronous callbacks, but then again, there is no light about the
    > problem you are trying to solve.
    >
    > If you are building a generic library for menus, the file mechanism can
    > be of use, but introduces the problem of how to assign actions to the
    > items in the menu :) which opens another pandora's box.


    that should not be a problem

    if I make the prompt function in the Menu pure virtual.
    virtual void prompt() = 0;
    and let derived class like "Edit" "View" define it like

    void Edit::prompt(){
    while( go_on ) {
    cout << ask;
    std::cin >> opt;

    switch( opt ){
    case( 1 ): cout << "handle another thread"; break; //action assigned here
    case( 2 ): Parent.print(); break; // <<< this is the problem I am
    trying to solve.
    default: cout << "This is not an option\n";
    }
    }
    }



    >
    > Maybe creating a class that receives as parameters arrays of entries {
    > ParentID, ID, Display Text, pointer to function() } gives you a generic
    > structure, ParentID = 0 could mean main menu, so you can create the
    > hierarchy, then you can pass pointers to functions for the actions
    > (maybe the function has as parameter the callback to be called when it
    > has completed !)


    not sure exactly about all of that but will chow on it.

    >
    > Hope this helps,
    >
    > Gary Wessle wrote:
    > > Hi
    > >
    > > I need help organizing this program in the right way.
    > > I included the code below which compiles and runs and gives the
    > > desired effect to a certain point, but I don't know what the next step
    > > is to finish this program.
    > >
    > > the program presents Main menu to the user and gives a prompt, the
    > > user types the number corresponding to an item from the menu, the
    > > program then
    > > 1) acts on this selection by calling a handle-function which
    > > spawns a thread and runs a corresponding function,
    > > 2) or presents another menu for more specific selection by the user, a
    > > prompt for selection and a repeat of (1) above.
    > >
    > > I am thinking to use a base class "Menu" which holds common attributes
    > > and operations and derived classes "Main" "Edit" "View" ...
    > > the items for each menu are stored in a file named after the menu, i.e
    > > menus/main.txt, menus/edit.txt ...
    > >
    > > my problem is, presented here so that you keep it in mind while
    > > reading the below; Menu::prompt() fulfills number (1) need above but
    > > how to do number (2) in a creative way?
    > >
    > > thanks a bunch
    > >
    > > // menu.h ****************
    > > #ifndef MENU_H
    > > #define MENU_H
    > >
    > > #include <string>
    > >
    > > class Menu {
    > > protected:
    > > std::string menus_dir;
    > > short opt;
    > > bool go_on;
    > > std::string ask;
    > >
    > > public:
    > > Menu();
    > > virtual void print() = 0; // print a menu
    > > void prompt(); // give a prompt
    > > virtual void handle() = 0; // call a handle
    > > };
    > >
    > >
    > > class Main: public Menu {
    > > std::string m_itms;
    > >
    > > public:
    > > Main();
    > > void print();
    > > void handle();
    > > };
    > >
    > >
    > > #endif
    > >
    > >
    > > //menu.cpp ****************
    > > #include <iostream>
    > > using std::cout;
    > > using std::cin;
    > > using std::endl;
    > > #include <fstream>
    > > using std::ifstream;
    > > #include <string>
    > > using std::string;
    > >
    > > #include "menu.h"
    > >
    > >
    > > Menu::Menu()
    > > : menus_dir( "menus/" ),
    > > ask( "Please choose an option from the menu:\n > " ),
    > > opt( true )
    > > {}
    > >
    > > void Menu::prompt(){ // in question ????????????????
    > > while( go_on ) {
    > > cout << ask;
    > > std::cin >> opt;
    > >
    > > switch( opt ){
    > > case ( 1 ): cout << "handle 1 thread"; break;
    > > default: cout << ask;
    > > }
    > > }
    > > }
    > >
    > >
    > > Main::Main()
    > > : Menu(),
    > > m_itms( "main.txt" )
    > > {}
    > >
    > > void Main::print(){
    > > string x = menus_dir+m_itms;
    > > ifstream in( x.c_str() );
    > > string line;
    > > while( getline(in, line) ) {
    > > cout << line << endl;
    > > }
    > > Menu::prompt();
    > > }
    > >
    > > void Main::handle(){
    > > cout << "handle thread started\n";
    > > }
    > >
    > >
    > > //main.cpp ****************
    > > #include "menu.h"
    > >
    > > int main() {
    > > Main mm;
    > > mm.print();
    > >
    > > }
    Gary Wessle, Nov 7, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dietrich
    Replies:
    1
    Views:
    642
    Joe Smith
    Jul 22, 2004
  2. Leon
    Replies:
    2
    Views:
    526
  3. John Smith
    Replies:
    0
    Views:
    2,163
    John Smith
    Aug 9, 2006
  4. tomix
    Replies:
    1
    Views:
    475
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tp?=
    Oct 29, 2006
  5. =?ISO-8859-1?Q?KLEIN_St=E9phane?=
    Replies:
    3
    Views:
    446
    hanumizzle
    Oct 6, 2006
Loading...

Share This Page