How to insert a countdown timer into a quiz program that is created using C ++?

All 10 questions with 5 characters must be answered over time. therefore, the time taken for each question n the remaining time should be displayed. can anyone help?

+1
source share
2 answers

A portable C ++ solution would be to use chrono::steady_clock to measure time. This is available in C ++ 11 under the <chrono> header, but may be available to older compilers in TR1 in <tr1/chrono> or boost.chrono .

Sustainable watches always advance at the speed of "as uniform as possible", which is an important factor on a multi-tasking multi-threaded platform. A stable clock is also independent of any β€œwall clock”, for example, a system clock (which can be arbitrarily manipulated at any time).

(Note: if steady_clock not part of your implementation, find monotonic_clock .)


The <chrono> types are a little distorted, so here is an example of a code fragment that returns a stable timestamp (more precisely, a timestamp from what you like, for example, high_resolution_clock ):

 template <typename Clock> long long int clockTick(int multiple = 1000) { typedef typename Clock::period period; return (Clock::now().time_since_epoch().count() * period::num * multiple) / period::den; } typedef std::chrono::monotonic_clock myclock; // old typedef std::chrono::steady_clock yourclock; // C++11 

Using:

 long long int timestamp_ms = clockTick<myclock>(); // milliseconds by default long long int timestamp_s = clockTick<yourclock>(1); // seconds long long int timestamp_us = clockTick<myclock>(1000000); // microseconds 
+4
source

Use time() .

It has a limitation that Kerrek indicated in his answer. But it is also very easy to use.

+2
source

Source: https://habr.com/ru/post/915219/


All Articles