按任意键在Linux C ++中继续 [英] Press anykey to continue in Linux C++
问题描述
我不知道如果在linux造成任何不同,但我已经在网上发现这:
I am not sure if being in linux makes any different, but i have found online that this:
cout << "Press Enter to Continue...";
cin.ignore(numeric_limits<streamsize>::max(),'\n');
应该足够了,#include< limits> $ c $
Should be sufficient, with #include<limits>
in the header of course.
但是,它似乎在我的程序中不起作用。
However, it does not seem to work in my program.
它编译,它运行,但它不等待。
It compiles, it runs, but it does not wait.
基本上,我有一个菜单,导致方法调用显示屏幕上的人的列表。
Basically, i have a menu, which lead to a method call to display a list of people on the screen. I wish to pause that list before the system goes back to the menu.
这是我的菜单中的代码:
Here is my code from the menu:
//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;
}
<请随时让我知道。
if you want to see any other bit of my code, feel free to let me know.
提前感谢。
推荐答案
In * nix,终端通常在向程序发送任何内容之前等待一整行输入。这是为什么你发布的示例代码说:按Enter键继续...;
,然后丢弃所有内容,直到下一个换行符。
In *nix, terminals usually wait for a whole line of input before sending anything to the program. Which is why the example code you posted said "Press Enter to Continue...";
, and then discarded everything until the next newline.
为了避免这种情况,你应该把你的终端放在非规范模式,这可以使用POSIX termios(3)
检查是否按下了键。
To avoid that, you should put your terminal in non-canonical mode, which can be done using the POSIX termios(3)
functions, as explained in checking if a key was pressed.
这篇关于按任意键在Linux C ++中继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!