2011-10-22 771 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; 
}