1

I have a console application that is intended to only run on windows. It is written in C++. Is there any way to wait 60 seconds (and show remaining time on screen) and then continue code flow?

I've tried different solutions from the internet, but none of them worked. Either they don't work, or they don't display the time correctly.

YSC
  • 38,212
  • 9
  • 96
  • 149
H4ZE
  • 183
  • 1
  • 1
  • 9
  • 1
    A for loop that sleeps 1 second 60 times? – Tobias Wilfert Jan 03 '19 at 09:36
  • 2
    What *have* you tried? Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [mcve]. – Some programmer dude Jan 03 '19 at 09:39
  • Exactly. But I need to display the remaining time on screen, and I can't figure out how to do that. I can't figure out a way to clear only specific parts of the screen. – H4ZE Jan 03 '19 at 09:41
  • How about using the carriage-return to overwrite what you previously wrote? Like e.g. `std::cout << "\rTime remaining: " << std::setw(5) << counter << std::flush;` – Some programmer dude Jan 03 '19 at 09:46
  • @H4ZE So your question is actually "How to clear only part of the console?", right? If you search for that, you'll land at [this](https://stackoverflow.com/questions/15063076/clearing-only-part-of-the-console-output) answer. That's assuming you mean the console with "the screen", that part is not clear. – Max Langhof Jan 03 '19 at 10:06
  • @Someprogrammerdude 's comment actually worked, but I feel the countdown is faster than 60 seconds. Like the actual seconds are faster. Here's my code: `int counter = 60; while (counter >= 1) { cout << "\rTime remaining: " << setw(5) << counter << flush; _sleep(1000); counter--; }` – H4ZE Jan 03 '19 at 10:19
  • use `SetWaitableTimer` with `CompletionRoutine` and period==1 sec and `SleepEx(INFINITE, TRUE);`- print remaing time from callback – RbMm Jan 03 '19 at 10:21
  • 1
    There is no need to use Windows-specific code if you have access to a C++11 compiler. You can replace the `sleep()` function with `std::this_thread::sleep_for()`. Have a look at [this page](https://en.cppreference.com/w/cpp/thread/sleep_for). – Krzysiek Karbowiak Jan 03 '19 at 10:45

5 Answers5

3
//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--;
    }
}
H4ZE
  • 183
  • 1
  • 1
  • 9
  • You can make it cross-platform if you use [`std::this_thread::sleep_for`](https://en.cppreference.com/w/cpp/thread/sleep_for). – Bartek Banachewicz Jan 03 '19 at 12:00
  • 1
    Unsurprisingly, you need to `#include `. And it's generally a good practice to avoid platform-specific code when you can. Right now it doesn't need to run on other OSes, but that might change in the future. – Bartek Banachewicz Jan 03 '19 at 14:34
0

You can use sleep() system call to sleep for 60 seconds.

You can follow this link for how to set 60 seconds timer using system call Timer in C++ using system calls.

mitesh7172
  • 666
  • 1
  • 11
  • 21
0

possible use Waitable Timer Objects with perion set to 1 second for this task. possible implementation

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);
RbMm
  • 31,280
  • 3
  • 35
  • 56
0

this might be of some help, it's not entirely clear what the question is but this is a countdown timer from 10 seconds, you can change the seconds and add minutes as well as hours.

#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;
    }
  }
-1

In c++ you can use countdown. please go through with the following logic which will allow you to show remaining time on the screen.

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;
}
}

if you need more help on this then please follow the link here

Hope it will work, please let me know that it is working in your case or not? or if you need any help.

Thanks!

Jayesh Vyas
  • 1,145
  • 3
  • 15
  • 35