重新启动或退出菜单

时间:2018-10-22 00:23:24

标签: c++ screen clear

所以我第一次按下它时由于某种原因我无法使其清除整个屏幕,这又创建了2次重启并退出,然后只有第一个改变。添加了cin.get()以便我可以查看结果

#include <iostream>
#include <Windows.h>
#include <conio.h>
using namespace std;
int main()
{
    int n = 0;
    bool restart;
    while (n == 0)
    {
        cout << ">Restart<\n";
        cout << "Exit\n";
        restart = true;
        switch (_getch())
        {
        case 's':
            system("cls");
            cout << "Restart\n";
            cout << ">Exit<\n";
            restart = false;
            break;
        case 'w':
            system("cls");
            cout << ">Restart<\n";
            cout << "Exit\n";
            restart = true;
            break;
        case '\r':
            if (restart == true)
            {
                n = 1;
                cout << "You restarted";
                cin.get();
            }
            else
            {
                n = 1;
                cout << "You exited";
                cin.get();
            }
            break;
        }
    }
}

我现在也看到它只能重新启动而不退出,我真的看不到为什么。 那我该如何选择2种东西?

0 个答案:

没有答案