Search code examples
c++windowscountdown

Making a countdown timer in C++


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.


Solution

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