Is there any simpler way to move one step in loop after some time? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is there any simpler way to move one step in loop after some time?

I will ask a user a question and if he is unable to answer the question within 30 second,I have to display another question and continue the timer for every question till file reads all the questions.

11th Dec 2018, 3:12 PM
Maleeha Khalid
Maleeha Khalid  - avatar
12 Answers
+ 1
Before start the input store current time in a variable "startTime". And after the input, just before register it. Also get a current time, then do a simple calculation (currentTime-startTime) to get how much time user takes to finish an input. If its more than specified time then dont process the input
11th Dec 2018, 3:58 PM
Taste
Taste - avatar
+ 1
unsigned long startTime = time(NULL); //input unsigned long endTime = time(NULL); if((endTime-startTime)>(second*1000))cout<<"no good";
11th Dec 2018, 4:21 PM
Taste
Taste - avatar
0
Idk if you can cancel console input in c++. But the timer need to run in different thread, so it wont intercept the input. Here's a good resource about thread http://www.cplusplus.com/reference/thread/thread/ It has an example, dont worry. As for the countdown maybe you can call sleep inside the timer thread, to make it stop for cetain amount of time
11th Dec 2018, 3:31 PM
Taste
Taste - avatar
0
Taste I've not studied tread yet and i tried sleep function but it nust block the whole program for certain amount of time..I don't want a program to block but want to continue without the user input after sometime
11th Dec 2018, 3:45 PM
Maleeha Khalid
Maleeha Khalid  - avatar
0
Thats why you need to put sleep inside another thread, so it wont block the main thread for the user input there's a trick, create a variable lets call it "timeout" set it to false. after the timer runs out set it to true. and after user input check "timeout" first before register the input
11th Dec 2018, 3:49 PM
Taste
Taste - avatar
0
Taste can we do it without thread as I'm not allowed to do the project out of my course outline :/
11th Dec 2018, 3:50 PM
Maleeha Khalid
Maleeha Khalid  - avatar
0
Okay, what about storing current time ? Is it allowed ?
11th Dec 2018, 3:52 PM
Taste
Taste - avatar
0
Taste yes..can you elaborate how can I do it?
11th Dec 2018, 3:54 PM
Maleeha Khalid
Maleeha Khalid  - avatar
0
ok I'll try this out Taste thank you
11th Dec 2018, 4:04 PM
Maleeha Khalid
Maleeha Khalid  - avatar
0
Taste do we not have to register the input to get the "current time"? or in other words, how will it calculate the time before the user input ?
11th Dec 2018, 4:05 PM
Maleeha Khalid
Maleeha Khalid  - avatar
0
Save it, before cin
11th Dec 2018, 4:06 PM
Taste
Taste - avatar
0
Taste can you give me a sample code..I am still not getting the point
11th Dec 2018, 4:07 PM
Maleeha Khalid
Maleeha Khalid  - avatar