menu-based console application

R

Roberto Dias

Could anyone give me some tips about how to start a menu-based console
application using C++ function oriented programming techniques? I'm a
FORTRAN-based electrical engineer and a newbie on C++ style, for this,
I'm not ready for OOP techniques yet.

Thanks a lot,

Roberto Dias
Recife/PE - Brazil
 
L

lallous

Roberto Dias said:
Could anyone give me some tips about how to start a menu-based console
application using C++ function oriented programming techniques? I'm a
FORTRAN-based electrical engineer and a newbie on C++ style, for this,
I'm not ready for OOP techniques yet.

Thanks a lot,

Roberto Dias
Recife/PE - Brazil

Hello

A simple menu system would look like:

int choice = 0;

while (choice != 4)
{
cout <<"Enter choice:"<< endl <<
"1)add user" << endl <<
"2)delete user" << endl<<
"3)edit user" << endl <<
"4)quit" << endl;

cin >> choice;

switch(choice)
{
case 1:
// add user
break;
case 2:
 
T

Thomas Matthews

Roberto said:
Could anyone give me some tips about how to start a menu-based console
application using C++ function oriented programming techniques? I'm a
FORTRAN-based electrical engineer and a newbie on C++ style, for this,
I'm not ready for OOP techniques yet.

Thanks a lot,

Roberto Dias
Recife/PE - Brazil

Search the newsgroup for the keyword "menu" and maybe also
"table driven". I've posted many replies about table driven
menu systems here.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
R

rossum

Could anyone give me some tips about how to start a menu-based console
application using C++ function oriented programming techniques? I'm a
FORTRAN-based electrical engineer and a newbie on C++ style, for this,
I'm not ready for OOP techniques yet.

Thanks a lot,

Roberto Dias
Recife/PE - Brazil

// Very simple menu based console application

#include <iostream>
#include <string>
#include <cstdlib>

//-------------------------------------

char display_menu();
void sneer_at_007();
void release_bats();
void take_over_world();
void get_the_girl();

//-------------------------------------

int main() {
bool running = true;
do {
switch (display_menu()) {
case '1': case 's': case 'S':
sneer_at_007();
break;
case '2': case 'r': case 'R':
release_bats();
break;
case '3': case 't': case 'T':
take_over_world();
break;
case '4': case 'g': case 'G':
get_the_girl();
break;
case '5': case 'q': case 'Q':
running = false;
break;
default:
std::cout << "I do not recognise that selection.\n";
} // end switch
} while (running);
return EXIT_SUCCESS;
} // end main()

//-------------------------------------

char display_menu() {
using std::cout;

const char NULL_SELECTION = '\0';

cout << '\n';
cout << "Main Menu\n";
cout << "=========\n";
cout << "1 Sneer at 007\n";
cout << "2 Release the Bats\n";
cout << "3 Take over the world\n";
cout << "4 Get the girl\n";
cout << "5 Quit\n";
cout << "\n";
cout << "Enter selection: ";

std::string selection;
if (!getline(std::cin, selection)) { exit(EXIT_FAILURE); }

if (selection.length() == 0) {
return NULL_SELECTION;
}
else {
return selection[0];
} // end if
} // end display_menu()

//-------------------------------------

void sneer_at_007() {
std::cout << "That tie clashes with those socks Mr Bond.\n";
return;
} // end sneer_at_007()

//-------------------------------------

void release_bats() {
std::cout << "The bats have been released sir.\n";
return;
} // end release_bats()

//-------------------------------------

void take_over_world() {
std::cout << "The world is now yours, your Imperial Majesty.\n";
return;
} // end take_over_world()

//-------------------------------------

void get_the_girl() {
std::cout << "What's a girl like you doing in a nice place like
this?\n";
return;
} // end get_the_girl()

//-------------------------------------
 
O

Old Wolf

lallous said:
cout <<"Enter choice:"<< endl <<
"1)add user" << endl <<
"2)delete user" << endl<<
"3)edit user" << endl <<
"4)quit" << endl;

Why flush the stream 5 times? "\n" is more intuitive anyway.
Also, many style guides recommend putting 'dangling' operators
at the start of the next line, rather than the end of the
previous:

cout << "Enter choice:\n"
<< "1) add user\n"
<< "2) delete user\n"
<< "3) edit user\n"
<< "4) quit"
<< endl;

I'd rather maintain this version.
 
T

Thomas Matthews

rossum said:
// Very simple menu based console application
[snip]

The case selections in the following code
could be simplified by using the std::toupper
or std::tolower functions.

Another simplification is to change the
display_menu() function to only return
values '1' through '5', see below.
int main() {
bool running = true;
do {
switch (display_menu()) {
case '1': case 's': case 'S':
sneer_at_007();
break;
case '2': case 'r': case 'R':
release_bats();
break;
case '3': case 't': case 'T':
take_over_world();
break;
case '4': case 'g': case 'G':
get_the_girl();
break;
case '5': case 'q': case 'Q':
running = false;
break;
default:
std::cout << "I do not recognise that selection.\n";
} // end switch
} while (running);
return EXIT_SUCCESS;
} // end main()

//-------------------------------------

char display_menu() {
using std::cout;

const char NULL_SELECTION = '\0';

cout << '\n';
cout << "Main Menu\n";
cout << "=========\n";
cout << "1 Sneer at 007\n";
cout << "2 Release the Bats\n";
cout << "3 Take over the world\n";
cout << "4 Get the girl\n";
cout << "5 Quit\n";
cout << "\n";
cout << "Enter selection: ";

std::string selection;
if (!getline(std::cin, selection)) { exit(EXIT_FAILURE); }

if (selection.length() == 0) {
return NULL_SELECTION;
}
else {
return selection[0];
One could eliminate the checking for upper or lower case
by converting first:
return std::toupper(selection[0]);
or
return std::tolower(selection[0]);
or
const char letter_selections[] = "SRTGQ"
const char numeric_equiv[] = "12345";
char * p = strchr(letter_selections, selection[0]);
if (!p)
{
return numeric_equiv[p - letter_selections];
}
else
{
return NULL_SELECTION;
}
} // end if
} // end display_menu()

Many people would have the display menu function display
the menu over again if the input was not within range.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
T

Thomas Matthews

Old said:
Why flush the stream 5 times? "\n" is more intuitive anyway.
Also, many style guides recommend putting 'dangling' operators
at the start of the next line, rather than the end of the
previous:

cout << "Enter choice:\n"
<< "1) add user\n"
<< "2) delete user\n"
<< "3) edit user\n"
<< "4) quit"
<< endl;

I'd rather maintain this version.

Why bother using stream insertion operators for text that
doesn't require formatting?

I rather maintain this version:
const char Menu_Text[] =
"Enter choice:\n"
"1) add user\n"
"2) delete user\n" // Hmmm, deleting people.
"3) edit user\n"
"4) quit\n";

cout.write(Menu_Text, sizeof(Menu_Text);
cout.flush(); // Is this necessary after a write()?

One time consuming aspect of the I/O streams is the formatting.
If the text doesn't need formatting, consider using the
write() method and declaring the text as constant. The above
version is faster since it bypasses the formatting aspects
of the I/O streams.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top