首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行while循环,直到收到特定输入,但如果没有收到c++,则重新运行

运行while循环,直到收到特定输入,但如果没有收到c++,则重新运行
EN

Stack Overflow用户
提问于 2016-12-04 04:12:59
回答 1查看 100关注 0票数 3

我正在尝试运行一个while循环,直到在终端上按下enter,但据我所知,该循环在cin.get()处停止,直到接收到某些东西。有没有办法使来自终端的输入成为可选的并重新运行while循环?这是代码中的循环,如果我去掉cin.get()部分,它工作得很好,我就是不能停止它。

代码语言:javascript
复制
        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;
    }
EN

回答 1

Stack Overflow用户

发布于 2016-12-04 05:50:50

这取决于您希望输入的复杂程度,但根据您发布的内容,您也可以使用std::signal函数,如下所示:

代码语言:javascript
复制
#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 (通常的控制台信号中断键处理程序),您可以修改上面的代码来忽略按键,只处理用户请求中断的事实。

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40952039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档