【问题标题】:Making a countdown timer in C++用 C++ 制作倒数计时器
【发布时间】:2019-01-03 09:34:12
【问题描述】:

我有一个旨在仅在 Windows 上运行的控制台应用程序。它是用 C++ 编写的。有什么方法可以等待 60 秒(并在屏幕上显示剩余时间)然后继续代码流?

我尝试了互联网上的不同解决方案,但都没有奏效。它们要么不工作,要么不能正确显示时间。

【问题讨论】:

  • 一个睡眠 1 秒 60 次的 for 循环?
  • 您尝试过什么?请阅读the help pages,获取the SO tour,了解how to ask good questions,以及this question checklist。最后学习如何创建minimal reproducible example
  • 没错。但我需要在屏幕上显示剩余时间,我不知道该怎么做。我想不出只清除屏幕特定部分的方法。
  • 用回车覆盖你之前写的怎么样?就像例如std::cout << "\rTime remaining: " << std::setw(5) << counter << std::flush;
  • 如果您可以访问 C++11 编译器,则无需使用特定于 Windows 的代码。您可以将sleep() 函数替换为std::this_thread::sleep_for()。看看this page

标签: c++ windows countdown


【解决方案1】:
//Please note that this is Windows specific code
#include <iostream>
#include <Windows.h>
using namespace std;

int main()
{
    int counter = 60; //amount of seconds
    Sleep(1000);
    while (counter >= 1)
    {
        cout << "\rTime remaining: " << counter << flush;
        Sleep(1000);
        counter--;
    }
}

【讨论】:

  • 使用std::this_thread::sleep_for可以跨平台。
  • 不出所料,您需要#include &lt;thread&gt;。尽可能避免使用特定于平台的代码通常是一个好习惯。目前它不需要在其他操作系统上运行,但将来可能会改变。
【解决方案2】:

您可以使用 sleep() 系统调用休眠 60 秒。

您可以点击此链接了解如何使用系统调用 Timer in C++ using system calls 设置 60 秒计时器。

【讨论】:

  • 我认为您没有理解我的问题。我想要一个倒计时,而不是计时器。
【解决方案3】:

可能使用 Waitable Timer Objects 并将 perion 设置为 1 秒来完成此任务。可能的实现

VOID CALLBACK TimerAPCProc(
                           __in_opt  LPVOID /*lpArgToCompletionRoutine*/,
                           __in      DWORD /*dwTimerLowValue*/,
                           __in      DWORD /*dwTimerHighValue*/
                           )
{
}

void CountDown(ULONG Seconds, COORD dwCursorPosition)
{
    if (HANDLE hTimer = CreateWaitableTimer(0, 0, 0))
    {
        static LARGE_INTEGER DueTime = { (ULONG)-1, -1};//just now
        ULONGLONG _t = GetTickCount64() + Seconds*1000, t;
        if (SetWaitableTimer(hTimer, &DueTime, 1000, TimerAPCProc, 0, FALSE))
        {
            HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
            do 
            {
                SleepEx(INFINITE, TRUE);
                t = GetTickCount64();
                if (t >= _t)
                {
                    break;
                }
                if (SetConsoleCursorPosition(hConsoleOutput, dwCursorPosition))
                {
                    WCHAR sz[8];
                    WriteConsoleW(hConsoleOutput, 
                        sz, swprintf(sz, L"%02u..", (ULONG)((_t - t)/1000)), 0, 0);
                }
            } while (TRUE);
        }
        CloseHandle(hTimer);
    }
}
    COORD dwCursorPosition = { };
    CountDown(60, dwCursorPosition);

【讨论】:

    【解决方案4】:

    这可能会有所帮助,目前尚不完全清楚问题是什么,但这是一个从 10 秒开始的倒数计时器,您可以更改秒数并添加分钟和小时。

    #include <iomanip>
    #include <iostream>
    using namespace std;
    #ifdef _WIN32
    #include <windows.h>
    #else
    #include <unistd.h>
      #endif
     int main()
     {
       for (int sec = 10; sec < 11; sec--)
     {
          cout << setw(2) << sec;
          cout.flush();
    
          sleep(1);
          cout << '\r';
          if (sec == 0)
          {
          cout << "boom" << endl;
          }
          if (sec <1)
          break;
        }
      }
    

    【讨论】:

      【解决方案5】:

      在 c++ 中,您可以使用倒计时。请完成以下逻辑,这将允许您在屏幕上显示剩余时间。

      for(int min=m;min>0;min--)  //here m is the total minits as per ur requirements
      {
      for(int sec=59;sec>=;sec--)
      {
      sleep(1);                   // here you can assign any value in sleep according to your requirements.
      cout<<"\r"<<min<<"\t"<<sec;
      }
      }
      

      如果您需要更多帮助,请关注link here

      希望它会起作用,请让我知道它是否适用于您的情况?或者如果您需要任何帮助。

      谢谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-19
        • 2012-05-22
        • 1970-01-01
        • 1970-01-01
        • 2021-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多