2011-10-22 66 views
2

我不知道,如果在Linux是使任何不同,但我已经在网上发现这一点:按任意键继续在Linux下C++

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; 

} 
如果你想看到我的代码的任何其他位

,随时让我知道。

在此先感谢。

+1

Rob's Rule#47:当你​​的意思是''\ n''时,千万不要说'endl'。请参阅http://stackoverflow.com/questions/5492380/what-is-the-c-iostream-endl-fiasco –

回答

5

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

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

+0

虽然这是很好的建议,但它不回答他的问题:为什么不'cin.ignore'块? (不回答他的问题是可以理解的 - 我现在看到他实际上没有问过。) –

4

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

+3

我从来没有见过这种技术*不会被使用。 – tylerl

+1

我试图用cin.get替换cin.ignore,但它似乎没有任何区别。你认为这可能是我在其他地方犯了一个错误吗? – Synia

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; 
} 
相关问题