【问题标题】:Press anykey to continue in Linux C++在 Linux C++ 中按任意键继续
【发布时间】:2011-12-13 08:06:16
【问题描述】:

我不确定在 linux 中是否有什么不同,但我在网上发现:

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

应该足够了,当然在标题中有#include&lt;limits&gt;

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

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

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

这是我的菜单代码:

//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


【解决方案1】:

您不能只使用cin.get()(获得一个字符)吗?

【讨论】:

  • 我从未见过这种技术被使用过。
  • 我尝试将 cin.ignore 替换为 cin.get,但似乎没有任何区别。你认为这可能是我在其他地方搞错了吗?
【解决方案2】:

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

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

【讨论】:

  • 虽然这是很好的建议,但它并没有回答他的问题:为什么cin.ignore 不阻止? (不回答他的问题是可以理解的——我现在知道他实际上并没有问。)
【解决方案3】:

这是我的代码中的一个 sn-p。它适用于 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;
}

【讨论】:

    猜你喜欢
    • 2013-11-10
    • 2015-01-23
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多