Search code examples
c++menudos

Creating main menu for DOS program


I'm writing a short text-adventure game and that game is ready, but now I want to do something with a main menu. Note that the whole game is in DOS.

This is what I want to achieve:

Create a main menu in the following way, but using while and switch loops. The switch loop will contain cases (case 1:, case 2:, case 3:, etc.) with the following options (which will cout above the loops)

cout << "[1] Play\n";
cout << "[2] Credits\n";
cout << "[3] Exit\n";

Now, the text-adventure game is too big to just put in this loop as it becomes increasingly hard to read because of nesting. There are loops in the game itself too, also while and switch loops. What I want to do now is something like the following, but I don't know how to do this. I will write it in psuedocode:

*open file game_name.cpp*
If player presses 1
    Insert actual_game.cpp
    Play game until over or user presses escape
else if player presses 2
    Show credits
    Return to main menu
else if player presses 3
    Terminate the session
else
    Shows the player that an invalid option has been chosen

The point is that I want to include multiple .cpp files instead of putting all the code in one single file (the actual_game.cpp). What is the best way to do this?


Solution

  • This question and answer is very similar to yours, take a look:

    Link

    Let me know if something is unclear in there. The bottom line is that you don't insert code files - those don't exist anyway after you compile your program. You call functions in response to each condition - those individual functions will, in turn, execute the relevant logic. You need to organize your game into a set of functions, where each function performs one particular job in the game (and likely call ever more specialized functions that handle the various locations in your game, etc.)

    Here's an example:

    // In GameFunctions.h:
    bool StartGame ();
    bool ShowCredits ();
    bool ExitGame ();
    
    // Add all function definitions here or create multiple header files to hold
    // groups of function definitions. Include these headers files in your CPP files.
    
    // In GameFunctions.cpp:
    #include <iostream>
    #include "GameFunctions.h"
    
    using namespace std;
    
    int main ( int argc, const char* argv[] )
    {
        int nKeyPress; // this holds the key pressed by the user
        bool bContinue = true;
    
        while ( bContinue )
        {
            ... // read a key here into nKeyPress
    
            switch ( nKeyPress )
            {
                case 1:
                    bContinue = StartGame ();
                    break;
    
                case 2:
                    bContinue = ShowCredits ();
                    break;
    
                case 3:
                    bContinue = ExitGame ();
                    break;
            }
        }
    }
    
    ...
    
    bool StartGame ()
    {
        // initialize your game here
        InitGame ();
    
        // Show the first room of your game and start  waiting for
        // user input (the user making various selections in the game).
        // You'll invoke other rooms from this function as you respond to
        // user selections.
        ShowRoom ( 1 );
    
        return ( true );
    }
    
    bool ShowCredits ()
    {
        ... // show credits here
    
        return ( true );
    }
    
    bool ExitGame ()
    {
        // do cleanup (if you have any to do) here
        return ( false );
    }
    

    You can also break up your game code into multiple .cpp and .h files to group your functions into logical groups. Even if you don't use classes, having all your code in a single .cpp file is usually a bad idea, unless your game is very, very short. So you can create multiple .cpp files, for example, one for each room: each .cpp file will hold the code to handle a particular room in your game. You'll need the function definitions in a header file and you need to include all header files in a particular .cpp file that you intend to use. (You don't need to include every .h in every .cpp, though - you only need those headers that contain definitions that you intend to use in a single .cpp.)

    At the end, your game will be made up of several .cpp and .h files and will have a number of functions: some will read user input, some will display messages on the screen, some may keep track of the user's score, some will initialize a room before the player first enters it, etc.

    You'll likely need other header files from the standard C or C++ library, depending on what standard features you'll try to use.