user8915618
user8915618

Reputation:

Exiting the Game from the Menu is not working

So I have a Menu that I used to run my text based game. The problem is that I can't seem to exit my game.Every time I run it, I can do option 1 and go to my game, and options 2 and 3 work just fine. But For option 4, I am unable to exit my game. All it does is print out what I ask it to print out, before giving the menu options again (as if it was just looping). I have googled a lot, and tried to figure out why but I am not sure. If someone can advise me what to do or tell me where my mistake is, it would be greatly appreciated. Please let me know if you want to see more code. All I have displayed here is the Menu Function.

void menu() {
    char choice = -1;
    while(choice != '1')
    {
        cout << "\n*    *   *   *   *" << endl;
        cout << "     The Dark Maze\n";
        cout << "\n*    *   *   *   *" << endl;
        cout << "\n=====================";
        cout << "\n     Main Menu       |";
        cout << "\n=====================";
        cout << "\n 1 - Start Game      |";
        cout << "\n 2 - Instructions    |";
        cout << "\n 3 - Storyline       |";
        cout << "\n 4 - Exit        |";
        cout << "\n=====================";
        cout << "\n";
        cout << "\n Enter choice: ";
        cout << "\n";
        cin >> choice;
        switch (choice)
        {
        case '1':
            cout << "\n" << endl;
            cout << "\n     But we can't start the game just yet..." << endl;
            break; //heads to game
        case '2':
            Instructions();
            break;
        case '3':
            Storyline();
            break;
        case '4':
            cout << "\n     Well, if you really don't want to play... you don't have to." << endl;
            break;                                          //just say exit?? break isnt making it stop 
        default:
            cout << "Invalid Character entered\n";
            cout << "\n";
            cout << "Press Space to continue\n";
        }// end of switches 
        cin.get();
    } // end of while
}// end of menu 

Upvotes: 0

Views: 157

Answers (3)

Azeem
Azeem

Reputation: 14637

You shouldn't be using while loop for this. Try do-while loop for this kind of menus like this:

do
{
    // your menu here...

    cin >> choice; 

    switch ( choice )
    {
    case ...
    ...
    }

    // cin.get();
    // ^^^^^^^^^^ You don't need this...
} while ( choice != '4' );

Some points to help you:

  • Use an enum to define your menu choices. (OR an enum class).

  • You can simply write a printMenu() function to print the menu in the main loop. Another function to process the choice.

For example:

void startGame()
{
    char choice = INVALID_OPTION;    // INVALID_OPTION => default invalid value

    do
    {
        printMenu();
        cin >> choice;
        processChoice( choice );
    } while ( choice != EXIT );      // EXIT => #define or an enum
}

Upvotes: 1

iBug
iBug

Reputation: 37287

You can use exit() to terminate your program with a given return value:

case '4':
    std::cout << "blahblahblah";
    std::exit(0);
    break; // No longer necessary

It's prerequisite and prototype is

#include <cstdlib>

namespace std{
    void exit(int status);
}

Upvotes: 0

Binghe Zhai
Binghe Zhai

Reputation: 93

just use return instead break blow case '4' . not perfect but can work.

Upvotes: 0

Related Questions