按任意键继续Linux C ++

问题描述 投票:2回答:3

我不确定在Linux中是否有任何不同,但我在网上发现这个:

    cout << "Press Enter to Continue...";
    cin.ignore(numeric_limits<streamsize>::max(),'\n');

应该足够了,当然在标题中加上#include<limits>

但是,它似乎在我的程序中不起作用。

它编译,运行,但它不等待。

基本上,我有一个菜单,导致方法调用显示屏幕上的人员列表。我希望在系统返回菜单之前暂停该列表。

这是菜单中的代码:

//Manager's Menu
void SelectionPage::showManagerMenu(){
    char option;
    while(true)
    {
        system("clear");                                                //Clears the terminal
        cout<<"             Flat Manager's Menu"<<endl<<endl;           //Display manager's menu
        cout << "Select Manager option" << endl;
        cout << "a) Add a new Flat Member" << endl;
        cout << "b) Delete an existing Flat Member" << endl;
        cout << "c) List Flat Members" << endl;
        cout << "d) Duties" <<endl;
        cout << "e) Resources" <<endl;
        cout << "f) Reset System" <<endl;
        cout << "q) Exit" << endl;
        cout << "make selection: ";
        cin >> option;

        switch(option) {                                                //Takes the user to the corresponding menu or method
            case 'a':   system("clear");
                        memberList.addNewFlatMember(points);
                    break;
            case 'b':   system("clear");
                        memberList.deleteFlatMember();
                    break;
            case 'c':   system("clear");
                        memberList.listFlatMembers();
                    break;
            case 'd':   system("clear");
                        showDutiesMenu();
                    break;
            case 'e':   system("clear");
                        showResourcesMenu();
                    break;
            case 'f':   //reset();
                    break;
            case 'q':   exit(0);
            default:    cout << "Option not recognised: " << option << endl;
                        showManagerMenu();  
        }
    }
}

我想选择的选项是c)导致:

//Show the current flat population
void MemberManagement::listFlatMembers(){
    cout<<"             Member List"<<endl<<endl;

    importFlatMembers();                                                //get flat member info from file

    for( int count = 0; count<flatMemberList.size(); count++){
        cout << count+1<<". "<<flatMemberList[count].getName() << endl;
    }

    cout << "Press any key to Continue...";
    cin.ignore(numeric_limits<streamsize>::max(),'\n');

    return;

}

如果你想看到我的代码中的任何其他部分,请随时告诉我。

提前致谢。

c++ linux key cin any
3个回答
5
投票

在* nix中,终端通常在向程序发送任何内容之前等待整行输入。这就是为什么你发布的示例代码说"Press Enter to Continue...";,然后丢弃所有内容直到下一个换行符。

为避免这种情况,您应该将终端设置为非规范模式,这可以使用POSIX termios(3)函数完成,如How to check if a key was pressed in Linux?中所述。


5
投票

难道你不能只使用cin.get()(获得一个字符)?


1
投票

这是我的代码中的一个片段。它适用于Windows和Linux。

#include <iostream>

using std::cout;
using std::cin;

// Clear and pause methods
#ifdef _WIN32
// For windows
void clearConsole() {
    system("cls");
}

void waitForAnyKey() {
    system("pause");
}
#elif __linux__
// For linux
void clearConsole() {
    system("clear");
}

void waitForAnyKey() {
    cout << "Press any key to continue...";
    system("read -s -N 1"); // Continues when pressed a key like windows
}

#endif

int main() {
    cout << "Hello World!\n";
    waitForAnyKey();
    clearConsole();
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.