Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Menu handle program

Reply
Thread Tools

Menu handle program

 
 
Gary Wessle
Guest
Posts: n/a
 
      11-07-2006
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:rompt() 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:rompt(){ // 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:rint(){
string x = menus_dir+m_itms;
ifstream in( x.c_str() );
string line;
while( getline(in, line) ) {
cout << line << endl;
}
Menu:rompt();
}

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


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

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

}

 
Reply With Quote
 
 
 
 
JoShCrUz
Guest
Posts: n/a
 
      11-07-2006
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:rompt() 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:rompt(){ // 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:rint(){
> string x = menus_dir+m_itms;
> ifstream in( x.c_str() );
> string line;
> while( getline(in, line) ) {
> cout << line << endl;
> }
> Menu:rompt();
> }
>
> void Main::handle(){
> cout << "handle thread started\n";
> }
>
>
> //main.cpp ****************
> #include "menu.h"
>
> int main() {
> Main mm;
> mm.print();
>
> }


 
Reply With Quote
 
 
 
 
Gary Wessle
Guest
Posts: n/a
 
      11-07-2006
"JoShCrUz" <(E-Mail Removed)> 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:rompt(){
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:rompt() 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:rompt(){ // 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:rint(){
> > string x = menus_dir+m_itms;
> > ifstream in( x.c_str() );
> > string line;
> > while( getline(in, line) ) {
> > cout << line << endl;
> > }
> > Menu:rompt();
> > }
> >
> > void Main::handle(){
> > cout << "handle thread started\n";
> > }
> >
> >
> > //main.cpp ****************
> > #include "menu.h"
> >
> > int main() {
> > Main mm;
> > mm.print();
> >
> > }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Package to handle table text render (handle space or tab betweenthe columns) ? =?ISO-8859-1?Q?KLEIN_St=E9phane?= Python 3 10-06-2006 08:46 AM
how to handle the menu control, gridview, and postback...this is driving me nuts! John Smith ASP .Net 0 08-09-2006 08:50 PM
Possible to handle web requests without an ASPX page? i.e. have DLL handle request. jdlwright@shaw.ca ASP .Net 2 05-31-2005 05:42 PM
how to handle command line output(not terminal handle) Leon Python 2 11-04-2004 05:16 AM
File Handle Reading Blues: Rereading a File Handle for Input Dietrich Perl 1 07-22-2004 10:02 AM



Advertisments