How to create a timer using the time.h library?

This is not homework, it is rather a challenge to our professor, but I still mark it as.

The task is to create a typing game with three levels with the corresponding complexity, different time limits (in seconds) and a lot of points. Now I have no problems with the program itself, I have a problem with using a timer that should be OS independent (I assume, as the only hint says time.h about this). What I did is wrong, because this is only an approximate guess from what I read about .h time, this is also ugly code:

time_t start;
int timer = time(&start);
...
time_t current;
for(ctr=0;ctr<10;ctr++)
{
    ...
    if(time(&current) == (timer+40))
      {    
           break;
      }
    ...
}

It works sometimes, but not in most cases, because this is just an approximate guess. Any suggestions would be appreciated.

+3
1

-, time_t* time. , ( ) time(NULL). , start current, .

-, time time_t, int, timer time_t.

,

if(time(&current) == (timer+40))

, , 40 , - , , , 43 . ? , == ?

+2

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


All Articles