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