0

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 
j08691
  • 204,283
  • 31
  • 260
  • 272
  • 1
    hi Candy, break will break the `switch (){ case : ....; break; }`statement stopping fallthrough of statement. It wont break the while loop. Add `return` when user want to stop playing. `exit` will work too – NishanthSpShetty Dec 11 '17 at 06:13
  • A generic `bool running` variable could be used to determine whether the while loop should continue – rosengrenen Dec 11 '17 at 08:40

3 Answers3

1

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
}
Azeem
  • 11,148
  • 4
  • 27
  • 40
  • Okay, the problem with saying choice!=4 is that when I say that, my first option does not work. So then how would I solve that? –  Dec 11 '17 at 16:34
  • @Candy: Technically and by convention, we use `do-while` loop for this kind of menu stuff. I'll update my answer to reflect that. – Azeem Dec 12 '17 at 05:13
0

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

0

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);
}
iBug
  • 35,554
  • 7
  • 89
  • 134
  • Thank you!! This worked, except my program didn't need the namespace. All I needed was the #include part. –  Dec 11 '17 at 23:49
  • @Candy It is obvious that you've written `using namespace std`, which [is a bad pratice](https://stackoverflow.com/q/1452721/5958455). – iBug Dec 12 '17 at 01:51