menu-based console application

Discussion in 'C++' started by Roberto Dias, Jul 21, 2004.

  1. Roberto Dias

    Roberto Dias Guest

    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
    Roberto Dias, Jul 21, 2004
    #1
    1. Advertising

  2. Roberto Dias

    lallous Guest

    "Roberto Dias" <> wrote in message
    news:...
    > 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:
    lallous, Jul 21, 2004
    #2
    1. Advertising

  3. 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
    Thomas Matthews, Jul 21, 2004
    #3
  4. Roberto Dias

    rossum Guest

    On 21 Jul 2004 06:10:20 -0700, (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
    rossum, Jul 22, 2004
    #4
  5. Roberto Dias

    Old Wolf Guest

    "lallous" <> 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.
    Old Wolf, Jul 23, 2004
    #5
  6. rossum wrote:

    > On 21 Jul 2004 06:10:20 -0700, (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
    Thomas Matthews, Jul 23, 2004
    #6
  7. Old Wolf wrote:

    > "lallous" <> 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
    Thomas Matthews, Jul 23, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Finney
    Replies:
    2
    Views:
    976
    Egor Bolonev
    Jun 30, 2003
  2. Egor Bolonev
    Replies:
    0
    Views:
    465
    Egor Bolonev
    Jun 30, 2003
  3. Serge Terekhoff
    Replies:
    0
    Views:
    631
    Serge Terekhoff
    Jun 30, 2003
  4. Miktor
    Replies:
    7
    Views:
    11,996
    Joe Bacigalupa
    Aug 9, 2005
  5. Joe Martin
    Replies:
    2
    Views:
    193
    ghorner
    Feb 22, 2010
Loading...

Share This Page