简体   繁体   中英

C++ displaying a timer for the program

So I have been given a first semester project to make hangman, tic tac toe and snakes and ladders game (on command line) and also display a timer when the game is being played and as a beginner I have no idea how to show the timer and what to use....Kindly help in this regard.

you can use std::chrono for displaying time. Given below is example code for measuring time:

#include <chrono>
#include <stdio.h>
#include <unistd.h>

int main()
{
    auto CurrentFrameStartTime = std::chrono::steady_clock::now();
    auto CurrentFrameStopTime = std::chrono::steady_clock::now();
    std::chrono::duration<double> currentFrameTime = CurrentFrameStopTime - CurrentFrameStartTime;
    while(1)
    {
        CurrentFrameStopTime = std::chrono::steady_clock::now();
        std::chrono::duration<double> currentFrameTime = CurrentFrameStopTime - CurrentFrameStartTime;
        auto hr = std::chrono::duration_cast<std::chrono::hours>(currentFrameTime);
        auto min = std::chrono::duration_cast<std::chrono::minutes>(currentFrameTime);
        auto sec = std::chrono::duration_cast<std::chrono::seconds>(currentFrameTime);
        auto msec = std::chrono::duration_cast<std::chrono::milliseconds>(currentFrameTime);
        printf("%d:%d:%d.%d have passed\n", hr, min%60, sec%60, msec%1000);
        usleep(1000);
    }
    return 1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM