Button & Timer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Button & Timer

How do I create a script capable to read a char in a limited time, e. g., 3 seconds? if any char has been taken in this period, it assumed a default value and prosecute the rest of the code. C++ or C#

10th Oct 2018, 10:44 AM
Daniel Bandeira
Daniel Bandeira - avatar
3 Answers
+ 1
idk if its possible, but you can try to create a timed thread with cin in it, just a idea not sure if it will work
10th Oct 2018, 8:41 PM
Taste
Taste - avatar
0
you can record the time when the user need to type the input and after the input, then compare both the time and see if the different less than 3 second or not here an example //untested char input; clock_t start_time = clock(); int wait_time = 3; cin >> input; if((clock() - start_time) <= (wait_time*1000)){ //user complete to input in less than or equal 3 second }else{ //user fail to input }
10th Oct 2018, 5:24 PM
Taste
Taste - avatar
0
I will try it... but I think it will stop the line "cin>> input;" eternally, until someone press the keyboard enter. I want this, but, if the function wait for 3 seconds or more, a default value is given to input
10th Oct 2018, 7:51 PM
Daniel Bandeira
Daniel Bandeira - avatar