C time limit - quiz program

I have a code as shown below for a Quiz program for which I need to add a time limit. The code is currently evaluating the quiz based on the time used. But I want it to go out after 5 minutes exactly from the moment the program starts.

Header used

#include<time.h> 

Initialized values ​​as follows

 time_t initialtime,finaltime; 

I am starting the countdown to the quiz using this code

 initialtime=time(NULL); 

When the program ends, I get time using this code

 finaltime=time(NULL); 

Now, how do I end a program before a final time of up to 10 minutes? What code should be used in the main function?

PS: I can post the full code if necessary.

+4
source share
3 answers

Something like the following:

 int main() { //.... time_t initial_time = time(NULL); float time_limit = 600.0f; //... while ( (time(NULL) - initial_time) < time_limit ) { // ... do stuff } // clean up etc. } 

This will be done until a certain duration has passed.

+3
source

There are many ways to do this:

  • write a loop that checks if 10 minutes are included at each iteration
  • create a thread that sleeps for 10 minutes then terminates the program
  • implement an alarm that signals after 10 minutes

See also:

What's good about stack overflows, you can search for “C Timeout” , and also “Google c function timeout” gives good results

0
source

You have two options: 1. During the process / program, continue to check whether the time has reached 10 minutes or not. This is pretty easy. 2. Run the program as a simple timer that automatically exits at the end of 10 minutes. Now in this program, before starting the timer, start the required operation as a separate thread. This is more accurate.

0
source

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


All Articles