我正在尝试运行一个while循环,直到在终端上按下enter,但据我所知,该循环在cin.get()处停止,直到接收到某些东西。有没有办法使来自终端的输入成为可选的并重新运行while循环?这是代码中的循环,如果我去掉cin.get()部分,它工作得很好,我就是不能停止它。
while (true) {
// In each iteration of our main loop, we run the Myo event loop for a set number of milliseconds.
hub.run(1);
// Extract first timestamp from Myo (string casted as a number)
if (tstart == 0){
stringstream myStream(collector.stampTime);
myStream >> tstart;
}
// Extracting samples from DataCollector
std::array<float, 3> acceData = collector.acceSamples;
std::array<float, 3> gyroData = collector.gyroSamples;
std::array<float, 3> oriData = collector.oriSamples;
std::array<int8_t, 8> emgData = collector.emgSamples;
for (int i = 0; i < emgData.size(); i++){
if (i < 3) {
// Accelerometer samples
acce[i] = acceData[i];
pAcce[i] = acce[i];
// Gyroscope samples
gyro[i] = gyroData[i];
pGyro[i] = gyro[i];
// Orientation samples
ori[i] = oriData[i];
pOri[i] = ori[i];
}
// EMG samples
emg[i] = emgData[i];
pEMG[i] = emg[i];
}
/*
* Plot the result
*/
engPutVariable(ep, "Acce", Acce);
engPutVariable(ep, "Gyro", Gyro);
engPutVariable(ep, "Ori", Ori);
engPutVariable(ep, "EMG", EMG);
engEvalString(ep,"EMG_gather");
// Extract timestamps from Myo (string casted as a number) and compute elapsed time
stringstream myStream(collector.stampTime);
myStream >> tend;
elapsedTime = (tend - tstart)/1000000;
// Keep track of how many runs Myo has performed
x++;
if (x % 30 == 0){
std::cout << x << endl;
}
if (cin.get() == '\n')
break;
else if (cin.get() == '')
continue;
}发布于 2016-12-04 05:50:50
这取决于您希望输入的复杂程度,但根据您发布的内容,您也可以使用std::signal函数,如下所示:
#include <iostream>
#include <csignal>
namespace {
volatile std::sig_atomic_t m_stop;
}
static void app_msg_pump(int sig)
{
if (sig == SIGINT || sig == SIGTERM) {
m_stop = sig;
}
}
int main(int argc, char* argv[])
{
m_stop = 0;
std::signal(SIGINT, &app_msg_pump); // catch signal interrupt request
std::signal(SIGTERM, &app_msg_pump); // catch signal terminate request
while (m_stop == 0) {
// your loop code here
}
std::cout << "Stopping.." << std::endl;
// your clean up code here
return 0;
}它将一直运行到用户按下CTRL+C (通常的控制台信号中断键处理程序),您可以修改上面的代码来忽略按键,只处理用户请求中断的事实。
希望这能有所帮助。
https://stackoverflow.com/questions/40952039
复制相似问题