C++ 为什么每次单击所需按钮时,此应用程序都会关闭? int main{ SetConsoleTitleAMenu试验; cout>lmao; cin.get{ 开关lmao{ 案例1: cout

C++ 为什么每次单击所需按钮时,此应用程序都会关闭? int main{ SetConsoleTitleAMenu试验; cout>lmao; cin.get{ 开关lmao{ 案例1: cout,c++,C++,您的switch语句除了显示文本外,什么也不做。 因为您要求用户按1继续进入主菜单。 1是switch语句中的一个例子,它运行switch语句中的内容,然后关闭。这一切发生得非常快 博士 您需要有一个用户输入来保持终端打开 int main() { SetConsoleTitleA("Menu Test"); cout << "Press 1 To Continue To The Main Menu\n\n"; int

您的switch语句除了显示文本外,什么也不做。 因为您要求用户按1继续进入主菜单。 1是switch语句中的一个例子,它运行switch语句中的内容,然后关闭。这一切发生得非常快

博士 您需要有一个用户输入来保持终端打开

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;
    }
}

让我们浏览一下您的代码,并解释为什么它会完全按照您在按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

这应该给你一个明确的提示,告诉你应该向代码中添加什么来阻止它执行。

程序完全按照你告诉它的去做——等待输入,然后打印菜单,然后终止。你期望发生什么不同的情况,为什么会这样?这些回答了你的问题吗?你需要一个额外的cin>>lmao;之后显示菜单。否则,程序将在打印菜单后结束。执行其他操作的代码在哪里?程序正在执行它应该执行的操作-读取输入并打印菜单后终止。如果在IDE中运行程序,通常IDE会提供一个窗口,以I/O定向方式运行程序,并在程序终止后立即关闭窗口。您需要在程序终止后不会立即关闭的窗口内运行程序,例如手动启动窗口,然后在该窗口的命令行启动程序,或者让程序停止,例如在程序终止前再次等待输入锿。
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