c++ - 判断是否过了 5 秒

标签 c++

我正在尝试确定自上次检查以来控制台应用程序是否过去了五秒钟。我觉得我的逻辑有点不对劲,不知道怎么解决。

程序开始时,我的lastCheck 变量首先为 0。它负责承载“旧时光”。

LastCheckCheckSeconds() 更新,这给了它一个新的“旧时间”

如果 LastCheck 等于 1232323,而 now 变量当前等于 1227323,那么我会知道已经过去了 5000 毫秒。 (实际上,数字远比这个大)

否则,我不想发生任何事情,我想等到这五秒真正过去。

后端

inline std::vector<int> CheckSeconds(int previous, int timeinseconds)
{
    //check if a certain amount of seconds have passed.
    int now = GetTickCount();
    int timepassed = 0;
    std::vector<int> trueandnewtime;
    //if the current time minus the old time is greater than 5000, then that means more than 5000 milliseoncds passed.
    //therefore the timepassed is true.
    if (now - previous > 5000)
        timepassed = 1;

    trueandnewtime.push_back(timepassed);
    trueandnewtime.push_back(now);

    return trueandnewtime;

}

前端

storage = CheckSeconds(LastCheck, 5);
        LastCheck = storage.at(1);

        if (storage.at(0) == 1)
        {
            ....blahblahblah.....
        }

有人知道我做错了什么吗?我一定是在某个地方有逻辑错误,或者我太笨了。

同样值得注意的是,这段代码处于一个 while 循环中,在 Sleep(60); 持续运行,目前它是一个控制台应用程序。

感谢任何帮助。

最佳答案

通过将 Lastcheck 集放入循环中修复了它。

关于c++ - 判断是否过了 5 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33863594/

相关文章:

c++ - 限制嵌套结构中的构造函数范围

c++ - 使用-std = c++ 0x时未声明nullptr

c++ - 在 Qt Gui 中使用 std::cout

C++ - 类方法不会从 dll 中导出(VS - win)

C++:对象键的 multimap 自定义比较器

c++ - 通过基类虚函数获取派生类型

c++ - 使用索引缓冲区对象时出现奇怪的段错误

c++ - 对本地类的 undefined reference

c++ - G++ 显示关于未定义命名空间的错误

c++ - 如何在C++中返回二维数组