-1
int main() {
    SetConsoleTitleA("Menu Test");
    cout << "Press 1 To Continue To The Main Menu\n\n";
    int lmao;
    cin >> lmao;
    cin.get(); {
        switch (lmao) {
        case 1:
            cout << "Main Menu n";
            cout << endl
                << " 1 - Main.\n"
                << " 2 - Story.\n"
                << " 4 - Help.\n"
                << " 5 - Exit.\n"
                << " Enter your choice and press return: ";
            break;
        }
    }
}

So I'm trying to make it a menu type thing in the windows console, so when ever you run the application it would open up the console and ask you to press 1 to continue which bring you to another menu. The problem is that its closing when I enter the digit 1.

scurkz
  • 1
  • 2
    The program does exactly what you told it to do - waits for input, then prints the menu, then terminates. What did you expect to happen differently, and why did you expect that? – Igor Tandetnik Nov 26 '20 at 02:54
  • 1
    Does these answer your question? [How to stop C++ console application from exiting immediately?](https://stackoverflow.com/questions/2529617/how-to-stop-c-console-application-from-exiting-immediately) [How to keep the console window open in Visual C++?](https://stackoverflow.com/questions/454681/how-to-keep-the-console-window-open-in-visual-c) – Sprite Nov 26 '20 at 02:59
  • You need an additional `cin >> lmao;` after displaying the menu. Otherwise the program just ends after printing the menu. – drescherjm Nov 26 '20 at 03:15
  • Where is your code to do something other than that? – Asteroids With Wings Nov 26 '20 at 03:16
  • 1
    The program is doing exactly what it should - terminating after reading input and printing the menu. If you are running the program within an IDE, typically the IDE provides a window, runs the program with I/O directed to that window, and closes the window immediately after the program terminates. You either need to run the program within a window that won't close immediately on program termination (e.g. manually start the window and, at the command line in that window, start your program) or have your program stop (e.g. wait for input again) before it terminates. – Peter Nov 26 '20 at 03:25

2 Answers2

1

Lets walk through your code, and explain why it does exactly what you have written after pressing 1:

cin >> lmao; 
cin.get(); // Here we are waiting for the enter key
switch (lmao) { // Now we have the number that the user pressed
case 1: // It was 1! great
    cout << "Main Menu n"; // We print our things
    ...
    break; // We break out of our switch statement
}
// Thats it, we reached the end of our program, what is there left to do, but 
// clean up and exit

So if you want the user to navigate through your menus, you need to introduce a method of keeping the user in some sort of loop. Here is some psuedo code:

exit_now = false
while not exit_now
    print menu options
    get user menu input
    if user input is exit
        exit_now = true
    else 
        act on user input

This should give you a firm hint about what to add to your code to keep it from executing.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
0

Your switch statement doesn't do anything besides display text. since you are asking user to continue to main menu by pressing 1. and 1 is a case in the switch statement its runs what is in the switch statement then closes. This all happens extremely fast.

TL;DR You need to have a user input to keep terminal open.

int main()
{

    SetConsoleTitleA("Menu Test");
    cout << "Press 1 To Continue To The Main Menu\n\n";
    int lmao;
    cin >> lmao;
    cin.get();
    {
        switch (lmao) {
        case 1:
            cout << "Main Menu n";
            cout << endl
                 << " 1 - Main.\n"
                 << " 2 - Story.\n"
                 << " 4 - Help.\n"
                 << " 5 - Exit.\n"
                 << " Enter your choice and press return: ";
            break;
        }
        cout << endl
             << "Please Enter A Num To Exit" << endl;
        int num;
        std::cin >> num;
    }
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64