问题描述
我是初学者。实际上,我为洗衣机编写了代码。我有不同的过程。但是我的代码有一个简单的问题。我想在用户输入正在进行的过程中暂停和继续。我的意思是,如果我按Q,它将暂停,如果按ENTER,它将继续,但是如果我不按任何按钮,它将继续其程序。我尝试了cin.get(),但是没有用。因此,我尝试制作一个函数{在代码中为其char pause()},可以在特定过程中调用该函数,用户将输入Q来暂停,输入“ ENTER” /“ RETURN”来恢复程序,但是当我使用此函数时功能停止,直到我不按q再按Enter。这就是为什么我禁用该功能。如果您可以提供帮助,请告诉我。代码如下所示。预先感谢。
class WMachine {
public :
int waterlevel;
char q;
bool soak (int time) ;
bool wash (int time);
bool rinse (int time);
bool spin (int time);
void WL(int WL);
// char pause(char in);
void sleep(float seconds);
WMachine(){};
};
// char WMachine :: pause(char in){
// while(1){
// cin>>in;
// if(in == 'Q' || in == 'q') break;
// }}
void WMachine :: WL(int WL){
if(WL==4){
cout<<"\nWater Level is Now at High..."<<endl;
}
else if(WL == 3){
cout<<"\nWater Level is Now at Middle..."<<endl;
}
else if(WL==2){
cout<<"\nWater Level is Now at Low..."<<endl;
}
else if(WL==1){
cout<<"\nWater Level is Now at Little..."<<endl;
}
}
bool WMachine :: soak(int time){
int T = time;
if (true){
sleep(4.0);
cout<<"\nSoaking...."<<endl;
// pause(q);
sleep(3.5);
cout<<"\nTime Remaining.. "<<T<<" minutes"<<endl;
// pause(q);
sleep(3.0);
}
else {return false;};
}
bool WMachine :: wash(int time){
int T = time;
if (true){
sleep(3.0);
cout<<"\nWashing...."<<endl;
// pause(q);
sleep(2.5);
cout<<"\nTime Remaining.. "<<T<<" minutes"<<endl;
// pause(q);
sleep(2.0);
}
else return false;
}
bool WMachine :: rinse(int time){
int T = time;
if (true){
sleep(2.0);
cout<<"\nRinsing...."<<endl;
// pause(q);
sleep(1.5);
cout<<"\nTime Remaining.. "<<T<<" minutes"<<endl;
// pause(q);
sleep(1.0);
}
else return false;
}
bool WMachine :: spin(int time){
int T = time;
if (true){
sleep(2.5);
cout<<"\nSpining..."<<endl;
// pause(q);
sleep(1.5);
cout<<"\nTime Remaining.. "<<T<<" minutes"<<endl;
// pause(q);
sleep(1.0);
}
else return false;
}
void WMachine :: sleep (float seconds){
clock_t startClock = clock();
float secondsAhead = seconds * CLOCKS_PER_SEC;
while(clock() < startClock+secondsAhead);
return;
}
class ProgWM : public WMachine {
public :
ProgWM(){};
void setProgMode();
bool standard(string x);
bool strong(string y);
bool fast(string w);
bool soft(string z);
};
bool ProgWM :: standard(string x){
if (x == "sd") return true;
else {return false;}
}
bool ProgWM :: strong(string y){
if (y == "sg") return true;
else {return false;}
}
bool ProgWM :: fast(string w){
if (w == "fa") return true;
else {return false;}
}
bool ProgWM :: soft(string z){
if (z == "so") return true;
else {return false;}
}
void ProgWM :: setProgMode(){
string p;
string choice;
int no;
cout<<" \nChoose Mode :"<<endl;
cout<<"----------------------"<<endl;
cout<<"===> Default Mode (Press d) "<<endl;
cout<<"===> Manual Mode (Press m) "<<endl;
cin>>choice;
if(choice=="d"){
cout<<"Choose (Type) Your Program :"<<endl;
cout<<". Standard (sd)"<<endl;
cout<<". Strong (sg)"<<endl;
cout<<". Fast (fa)"<<endl;
cout<<". Soft (so)"<<endl;
cin>>p;
if(standard(p)){
WL(4);
soak(7);
WL(3);
wash(7);
WL(2);
rinse(4);
WL(4);
soak(7);
WL(3);
wash(7);
WL(2);
rinse(4);
WL(1);
spin(4);
cout<<"\n\nWASHING IS DONE.."<<endl;
cout<<"\nTHANK YOU !"<<endl;
}
else if(strong(p)){
WL(4);
soak(7);
WL(3);
wash(7);
WL(2);
rinse(4);
WL(1);
spin(1);
WL(4);
soak(7);
WL(3);
wash(7);
WL(2);
rinse(4);
WL(1);
spin(1);
WL(4);
soak(7);
WL(3);
wash(7);
WL(2);
rinse(4);
WL(1);
spin(4);
cout<<"\n\nWASHING IS DONE.."<<endl;
cout<<"\nTHANK YOU !"<<endl;
}
else if(fast(p)){
WL(4);
soak(3);
WL(3);
wash(3);
WL(2);
rinse(3);
WL(4);
soak(2);
WL(3);
wash(2);
WL(2);
rinse(2);
WL(1);
spin(2);
cout<<"\n\nWASHING IS DONE.."<<endl;
cout<<"\nTHANK YOU !"<<endl;
}
else if(soft(p)){
WL(4);
soak(7);
WL(3);
wash(7);
WL(2);
rinse(4);
WL(4);
soak(7);
WL(3);
wash(7);
WL(2);
rinse(4);
cout<<"\n\nWASHING IS DONE.."<<endl;
cout<<"\nTHANK YOU !"<<endl;
}
else{
cout<<"Invalid Input !"<<endl;
};
}
else if(choice=="m"){
cout<<"Select Process :"<<endl;
cout<<". Soak (1)"<<endl;
cout<<". Wash (2)"<<endl;
cout<<". Rinse (3)"<<endl;
cout<<". Spin (4)"<<endl;
cin>>no;
if(no==1) {
cout<<"Select Water Level :"<<endl;
cout<<". High (4)"<<endl;
cout<<". Middle (3)"<<endl;
cout<<". Low (2)"<<endl;
cout<<". Little (1)"<<endl;
cin>>waterlevel;
WL(waterlevel);
soak(7);
cout<<"\n\nWASHING IS DONE.."<<endl;
cout<<"\nTHANK YOU !"<<endl;}
else if (no == 2) {cout<<"Select Water Level :"<<endl;
cout<<". High (4)"<<endl;
cout<<". Middle (3)"<<endl;
cout<<". Low (2)"<<endl;
cout<<". Little (1)"<<endl;
cin>>waterlevel;
WL(waterlevel);
wash(7);
cout<<"\n\nWASHING IS DONE.."<<endl;
cout<<"\nTHANK YOU !"<<endl;}
else if (no == 3) {cout<<"Select Water Level :"<<endl;
cout<<". High (4)"<<endl;
cout<<". Middle (3)"<<endl;
cout<<". Low (2)"<<endl;
cout<<". Little (1)"<<endl;
cin>>waterlevel;
WL(waterlevel);
rinse(5);
cout<<"\n\nWASHING IS DONE.."<<endl;
cout<<"\nTHANK YOU !"<<endl;}
else if (no == 4) {cout<<"Select Water Level :"<<endl;
cout<<". High (4)"<<endl;
cout<<". Middle (3)"<<endl;
cout<<". Low (2)"<<endl;
cout<<". Little (1)"<<endl;
cin>>waterlevel;
WL(waterlevel);
spin(4);
cout<<"\n\nWASHING IS DONE.."<<endl;
cout<<"\nTHANK YOU !"<<endl;}
else {cout<<"Invalid !"<<endl;};
}
};
int main(){
string power;
ProgWM prog;
cout<<" WELCOME "<<endl;
cout<<"----------------------------------------"<<endl;
cout<<"\nSwitch Off / On (type on/off) : "<<endl;
cin>>power;
cout<<"Press Q/q After Every Process To Pause and Press ENTER To Start"<<endl;
if(power == "On" || power == "on"){
prog.setProgMode();
}
else if( power== "off" || power == "Off"){
cout<<"\nSee You On Your Next Washing !"<<endl;
}
else{
cout<<"Invalid Input !"<<endl;
};
return 0;
}
解决方法
我有点误解了你的问题。您的问题实际上是如何等待键盘输入时不暂停执行,除非处于特定状态。
有一个POSIX函数名称fctnl(文件控制),用于阻止或不阻止对文件(从这个意义上来说,stdin,stdout和stderr也是文件)上的读取操作。在我遇到过的所有系统上,stdin的文件号均为零,但是还有一个名为STDIN_FILENO的常量,所以让我们使用它。
将stdin设置为非阻塞:
flags = fcntl(STDIN_FILENO,F_GETFL,0);
flags |= O_NONBLOCK;
fcntl(STDIN_FILENO,F_SETFL,flags);
将标准输入设置为阻止:
flags = fcntl(STDIN_FILENO,0);
flags &= ~O_NONBLOCK;
fcntl(STDIN_FILENO,flags);
要读取键盘输入,请使用POSIX函数read
。
在非阻塞模式下,它将返回错误指示(-1),并且errno将设置为EAGAIN
或EWOULDBLOCK
。
更高级别的C ++ I / O API会将错误解释为实际错误,因此不应将其用于非阻塞文件的输入。
我的原始答案:
为暂停执行,有一个名为select
的POSIX函数。
它会暂停进程(或当前线程),直到告诉它要听的任何文件的输入为止。等待期间没有CPU周期。
Win32 API中存在一个类似的函数,称为WaitForMultipleObjects
。
这可以通过多线程实现。有一个线程在后台进行处理,而另一个线程在等待用户输入。
等待线程可以设置一个条件变量,表示需要暂停,而处理线程应定期等待此条件。条件检查的次数越多,界面的响应速度就越大。