473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

menu-based console application

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
Jul 22 '05 #1
6 12133
"Roberto Dias" <di*****@br.inter.net> wrote in message
news:51**************************@posting.google.c om...
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:
Jul 22 '05 #2
Roberto Dias wrote:
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

Jul 22 '05 #3
On 21 Jul 2004 06:10:20 -0700, di*****@br.inter.net (Roberto Dias)
wrote:
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()

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


--

The ultimate truth is that there is no Ultimate Truth
Jul 22 '05 #4
"lallous" <la*****@lgwm.org> wrote:
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.
Jul 22 '05 #5
rossum wrote:
On 21 Jul 2004 06:10:20 -0700, di*****@br.inter.net (Roberto Dias)
wrote:

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

[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

Jul 22 '05 #6
Old Wolf wrote:
"lallous" <la*****@lgwm.org> wrote:

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.


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

Jul 22 '05 #7

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

Similar topics

1
by: Macamba | last post by:
Hi all, I am currently developing a website for a voluntary organisation. It is my first step in website development. The dynamic menu I developed has some bugs, which I addressed in another...
1
by: ajay | last post by:
I have following code for a slide menu but i twiked it to work for a single level menu. Open it in a Browser to get a clear picture. I have 2 Qs 1) How to make first entry as non-link. i.e i...
8
by: Dennis C. Drumm | last post by:
Is there a way to modify the standard context menu shown when someone right clicks in a windows text box and that would work for all open windows applications? The standard context menu for...
2
by: Gary Wessle | last post by:
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...
1
by: Anthony | last post by:
Below is a script I found at http://javascript.internet.com/ for a cascading menu. The script works great but there is one thing that I would like modified. BecauseI am just learning javascript,...
4
by: TycoonUK | last post by:
Hi, As I do not have IE7 on my computer, I was wondering if there is a fault in my CSS Menu when using IE7. Please can someone look at my site - http://www.worldofmonopoly.co.uk and tell me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...

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.