473,498 Members | 703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Menu handle program

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

}

Nov 7 '06 #1
2 3039
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();

}
Nov 7 '06 #2
"JoShCrUz" <jo******@yahoo.comwrites:
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();

}
Nov 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
3515
by: Andromeda | last post by:
I've been trying to add a tree menu to my website with some luck (more or less). I came across a script on simplythebest.net, but I seem to be having a problem with it... and since they have no...
3
34205
by: dapernia | last post by:
Hi friends, I want to make menu in my C program For example: Main Menu: (1) Enter Data, (2) Control Variables (3) Exit I want to make this menu to go through the options just pressing...
3
2577
by: Yuri O. | last post by:
Hi I'm wondering whether is existing any working way of getting real metrics about the active ContextMenu. For example ContextMenu(.net) expose window handler which I tried to use in...
1
2456
by: news.microsoft.com | last post by:
Hi everyone, Please I'm creating a Context Menu and doing a manual Show from C#, on a different thread I need to know the window size of the Context Menu already opened, I have the object...
0
1541
by: Will Pittenger | last post by:
I have a C# .NET program which does without a normal titlebar for its main window. Trouble is, I still want the user to be able to access select commands while the program is minimized. (I...
1
2753
by: Ignacio X. Domínguez | last post by:
Hi everyone. Hope you can help me out on this one. I'm developing a Component which has a property ContextMenu. This component, when placed on a form with a working ContextMenu (events assigned...
2
1815
by: Peter Row | last post by:
Hi, I am trying to draw on top of the main menu area of a form (this is just a test at the moment). I'm handling the forms paint event and using the following code: private void...
0
898
by: iwdu15 | last post by:
hi, how (if my program is currently running) can i add a menu item to the system menu, for instane, when a user right-clicks on a program in the taskbar, my menu shows up? also how can i get the...
1
2492
by: Kayvine | last post by:
Hi guys, this is a question I have for an assignment, it is pretty long, but I am not asking for the code(well if someone wants to write I'll be really happy, lol), but I just want to know how to...
0
6998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5460
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.