Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2008
    Location
    41° 28′ 56″ N, 81° 40′ 11″ W
    Posts
    2,399

    Console-based menu system

    I can tell that it's been quite some time since I had to learn how to develop a menu system in console/DOS format, but thought I'd get back into the practice of this. However, I have to wonder if this is really the best code to use to make all this happen. I have all the functions broken up into parts, to save on code space:

    Code:
    // Menu.cpp - Vintage DOS/Console Menu System
    // (c)2010, Robert "WedgeBob" Morin
    // --------
    
    // Header files
    #include <iostream>
    #include <stdlib.h>
    
    // namespaces
    using namespace std;
    
    // menu function classes
    void AboutScreen();
    void SubMenuOne();
    void SubMenuTwo();
    void SubMenuThree();
    
    
    // main function
    void main()
    {
    		int quit;
            int clear;
            int option = 0;
    
    		while (option != 4)
    		{
    
            system("CLS");
            cout << "ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»" << endl;
            cout << "º        V I N T A G E   D O S   M E N U   S Y S T E M   E N G I N E      º" << endl;
            cout << "º                ( c ) 1 9 9 8,   2 0 1 0   W e d g e B o b               º" << endl;
            cout << "ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹" << endl;
            cout << "º                                                                         º" << endl;
            cout << "º        1)  GO TO SUBMENU ONE                                            º" << endl;
            cout << "º        2)  GO TO SUBMENU TWO                                            º" << endl;
            cout << "º        3)  GO TO SUBMENU THREE                                          º" << endl;
            cout << "º                                                                         º" << endl;
            cout << "×ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄĹ" << endl;
            cout << "º  4) QUIT TO DOS/WIN             |     5)  ABOUT MENU                    º" << endl;
            cout << "ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ" << endl;
            cout << endl;
            cout << "Please select option (1 - 5):  ";
            cin >> option;
    
            switch (option)
            {
                    case 1:
                         SubMenuOne();
                         break;
                    case 2:
                         SubMenuTwo();
                         break;
                    case 3:
                         SubMenuThree();
                         break;
                    case 4:
                         system("CLS");
                         cout << "Press <0 + ENTER> to Return to DOS:  " << endl;
                         cin >> quit;
                         break;
                    case 5:
                         system("CLS");
                         AboutScreen();
                         break;
                    default:
                         system("CLS");
                         cout << "Sorry, but that is an invalid option." << endl;
                         cout << "Please press <0 + ENTER> to return to main menu." << endl;
                         cin >> clear;
             }
    		}
    }
    Code:
    void SubMenuOne()
    {
    
    		int option = 0;
            int quit;
            int clear;
    
    		while (option != 4)
    		{
            system("CLS");
            cout << "ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»" << endl;
            cout << "º            S U B - M E N U   O N E   T I T L E   H E A D E R            º" << endl;
            cout << "ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹" << endl;
            cout << "º                                                                         º" << endl;
            cout << "º            1)  OPTION ONE                                               º" << endl;
            cout << "º            2)  OPTION TWO                                               º" << endl;
            cout << "º            3)  OPTION THREE                                             º" << endl;
            cout << "ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÑÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹" << endl;
            cout << "º        4)  QUIT TO DOS         |        5)  BACK TO MAIN MENU           º" << endl;
            cout << "ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ" << endl;
            cout << endl;
            cout << "Please select option (1 - 5):  ";
            cin >> option;
    
            switch (option)
            {
                    case 1:
                            system("CLS");
                            cout << "You have selected option one." << endl;
                            cout << "Please press <0 + ENTER> to return to main menu." << endl;
                            cin >> clear;
                            break;
                    case 2:
                            system("CLS");
                            cout << "You have selected option two." << endl;
                            cout << "Please press <0 + ENTER> to return to main menu." << endl;
                            cin >> clear;
                            break;
                    case 3:
                            system("CLS");
                            cout << "You have selected option three." << endl;
                            cout << "Please press <0 + ENTER> to return to main menu." << endl;
                            cin >> clear;
                            break;
                    case 4:
                            system("CLS");
                            cout << "Please press <0 + ENTER> to return to DOS:  " << endl;
                            cin >> quit;
                            break;
                    case 5:
                            system("CLS");
                            AboutScreen();
                            break;
                    default:
                            system("CLS");
                            cout << "Sorry, but that is not a valid option." << endl;
                            cout << "Please press <0 + ENTER> to return to main menu." << endl;
                            break;
            }
    	}
    }
    Code:
    void SubMenuTwo()
    {
    
            int option = 0;
            int quit;
            int clear;
    
    		while (option != 4)
    		{
            system("CLS");
            cout << "ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»" << endl;
            cout << "º            S U B - M E N U   T W O   T I T L E   H E A D E R            º" << endl;
            cout << "ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹" << endl;
            cout << "º                                                                         º" << endl;
            cout << "º            1)  OPTION ONE                                               º" << endl;
            cout << "º            2)  OPTION TWO                                               º" << endl;
            cout << "º            3)  OPTION THREE                                             º" << endl;
            cout << "ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÑÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹" << endl;
            cout << "º        4)  QUIT TO DOS         |        5)  BACK TO MAIN MENU           º" << endl;
            cout << "ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ" << endl;
            cout << endl;
            cout << "Please select option (1 - 5):  ";
            cin >> option;
    
            switch (option)
            {
                    case 1:
                            system("CLS");
                            cout << "You have selected option one." << endl;
                            cout << "Please press <0 + ENTER> to return to main menu." << endl;
                            cin >> clear;
                            break;
                    case 2:
                            system("CLS");
                            cout << "You have selected option two." << endl;
                            cout << "Please press <0 + ENTER> to return to main menu." << endl;
                            cin >> clear;
                            break;
                    case 3:
                            system("CLS");
                            cout << "You have selected option three." << endl;
                            cout << "Please press <0 + ENTER> to return to main menu." << endl;
                            cin >> clear;
                            break;
                    case 4:
                            system("CLS");
                            cout << "Please press <0 + ENTER> to return to DOS:  " << endl;
                            cin >> quit;
                            break;
                    case 5:
                            system("CLS");
                            AboutScreen();
                            break;
                    default:
                            system("CLS");
                            cout << "Sorry, but that is not a valid option." << endl;
                            cout << "Please press <0 + ENTER> to return to main menu." << endl;
                            break;
            }
    	}
    }
    Code:
    void SubMenuThree()
    {
    
            int option = 0;
            int quit;
            int clear;
    
    		while (option != 4)
    		{
            system("CLS");
            cout << "ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»" << endl;
            cout << "º          S U B - M E N U   T H R E E   T I T L E   H E A D E R          º" << endl;
            cout << "ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹" << endl;
            cout << "º                                                                         º" << endl;
            cout << "º            1)  OPTION ONE                                               º" << endl;
            cout << "º            2)  OPTION TWO                                               º" << endl;
            cout << "º            3)  OPTION THREE                                             º" << endl;
            cout << "ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÑÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹" << endl;
            cout << "º        4)  QUIT TO DOS         |        5)  BACK TO MAIN MENU           º" << endl;
            cout << "ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ" << endl;
            cout << endl;
            cout << "Please select option (1 - 5):  ";
            cin >> option;
    
            switch (option)
            {
                    case 1:
                            system("CLS");
                            cout << "You have selected option one." << endl;
                            cout << "Please press <0 + ENTER> to return to main menu." << endl;
                            cin >> clear;
                            break;
                    case 2:
                            system("CLS");
                            cout << "You have selected option two." << endl;
                            cout << "Please press <0 + ENTER> to return to main menu." << endl;
                            cin >> clear;
                            break;
                    case 3:
                            system("CLS");
                            cout << "You have selected option three." << endl;
                            cout << "Please press <0 + ENTER> to return to main menu." << endl;
                            cin >> clear;
                            break;
                    case 4:
                            system("CLS");
                            cout << "Please press <0 + ENTER> to return to DOS:  " << endl;
                            cin >> quit;
                            break;
                    case 5:
                            system("CLS");
                            AboutScreen();
                            break;
                    default:
                            system("CLS");
                            cout << "Sorry, but that is not a valid option." << endl;
                            cout << "Please press <0 + ENTER> to return to main menu." << endl;
                            break;
            }
    	}
    }
    Code:
    void AboutScreen()
    {
    
            int menu;
            int quit;
            int option = 0;
    		while (option != 2)
    		{
            system("CLS");
            cout << "ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»" << endl;
            cout << "º            A B O U T   V I N T A G E   M E N U   S Y S T E M           º" << endl;
            cout << "ÇÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄĹ" << endl;
            cout << "º                                                                        º" << endl;
            cout << "º   (c)1998, (c)2010, Robert 'WedgeBob' Morin.  This is a base program   º" << endl;
            cout << "º   used for either educational purposes, or for other developers and    º" << endl;
            cout << "º   menu designers for their own programs, and is only available on an   º" << endl;
            cout << "º   original software developer basis.  Not for personal or everyday use.º" << endl;
            cout << "ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÑÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹" << endl;
            cout << "º    1) Return to Main Menu      |        2) Quit to DOS/Console         º" << endl;
            cout << "ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ" << endl;
            cout << endl;
            cout << "Please press <R + ENTER> to return to menu, or <Q + ENTER> to quit program:  " << endl;
            cin >>  option;
    
            switch (option)
            {
                    case 1:
                            system("CLS");
                            cout << "Press <0 + ENTER> to Return to Main Menu:  " << endl;
                            cin >> menu;
                            break;
    
                    case 2:
    						system("CLS");
                            cout << "Press <0 + ENTER> to Quit to DOS:  " << endl;
                            cin >> quit;
                            break;
    
                    default:
                            system("CLS");
                            cout << "Sorry, that is not a valid option." << endl;
                            cout << "Please press <0 + ENTER> to return to main menu." << endl;
                            cin >> menu;
    
            }
    		}
    
    }
    Yeah, I know the code looks a bit messy, but trust me, it was well formatted when I was working with this code in Windows XP Mode in the EDIT.COM editor before bringing it back into Windows 7 and in VC++ 2010. Tried to go for that old navigation menu-type program that I used to use before the Win 95 days. Yes, it may be ancient history, but dammit, I want my old navigation menu back!
    PC Specs: Antec 900 ATX Case, Gigabyte 790GX Mobo, AMD Phenom II x4 955, Corsair HX620W PSU, Corsair Vengeance 16GB (4x4GB) DDR3-1600, EVGA GTX 680 2GB GDDR5, 3D Vision Kit 2, LG 8x Blu-ray Burner, Intel X25-M SSD, 500GB+640GB WD Caviar Blacks, Win 7 Pro x64, DAS Pro K/B, MX518 Mouse, Logitech Z-2300 Speakers, Toshiba 32TL515U 32" 3DTV, Wacom Bamboo Fun Large Tablet.

  2. #2
    Join Date
    Mar 2006
    Location
    South Australia
    Posts
    4,521
    Have you looked into using something like ncurses?

  3. #3
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    Quote Originally Posted by WedgeBob View Post
    However, I have to wonder if this is really the best code to use to make all this happen.
    here's how i'd do it:
    your main problem now is that you have to write a lot of code that looks exactly the same. e.g., every sub-menu consists of more or less the same switch/case loop with more or less the same functionality included in the labels. whenever you spot such redundancies, that's where you will want to start changing things.
    a much better approach would be to write a generic 'menu-engine', that allows you to build menus in a less error-prone form. for example, your menus are always organized like trees, so it should not be hard to write a menu-engine that allows you to build menus from tree-like data structures. maybe even read menus from an external xml-file?
    the great thing about this approach is that it forces you to think in structures: what are common properties that all menus share? how can i abstract the behavior triggered by a menu item into a data structure? how would an interface for such a menu-engine look like, that is elegant and easy to use and still powerful enough to allow everything that was possible with the original approach?

    take your time and think about this thoroughly. preferably in a comfy chair with a nice warm cup of tea in one hand and a sharp pencil in the other. don't worry too much about messing up (remember: the first draft of everything is crap ), just get a basic layout on the page and then start coding.

    i'm looking forward to see what you came up with

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •