c++在while循环中获得一次输出

标签 c++ time while-loop

你好,我想在 while 循环中获取一次输出

While(1){
    if( current->tm_hour == 10 && current->tm_min == 0  ){
        Start_Function();
        std::cout <<  "Started" << std::endl;
    }

    if( current->tm_hour == 12 && current->tm_min == 0  ){
        End_Function();
        std::cout <<  "Ended" << std::endl;
    }

    Sleep(5000);
}

我使用 sleep 每 5 秒刷新一次

所以我想要当前的小时和分钟 = 10 和 00

它给我输出 Started 并且它只调用一次该函数并且它继续刷新

最佳答案

怎么样:

bool start_called = false, end_called = false;
While(1){
    if( current->tm_hour == 10 && current->tm_min == 0 && !start_called  ){
        Start_Function();
        std::cout <<  "Started" << std::endl;
        start_called = true;
    } else
        start_called = false;

    if( current->tm_hour == 12 && current->tm_min == 0 && !end_called ){
        End_Function();
        std::cout <<  "Ended" << std::endl;
        end_called = true;
    } else
        end_called = false;

    Sleep(5000);
}

你可以用仿函数做得更好,但那更高级一些。

关于c++在while循环中获得一次输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13505172/

相关文章:

Java while循环永远不会进入循环

java - Python 与 Java UTC?

c++ - 使用 MFC 对话框编辑器添加带有纯资源 DLL 的事件处理程序

c++ - 如何使用 C++ 更改 Window 的事件记录设备?

c++ - 使用C++访问通过网卡传递的数据

javascript - 使用 Javascript 更新时间元素

javascript - 更新 JavaScript

php - do-while/while 中的分配没有按预期工作

c - 用其他字符的框架在 C 中编程矩形

c++ - 微软 SQL 服务器 : Access a local temporary table in a prepared statement