Menu handle program

G

Gary Wessle

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();

}
 
J

JoShCrUz

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,
 
G

Gary Wessle

JoShCrUz said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top