How to limit the time entering a number in C++ in console. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to limit the time entering a number in C++ in console.

The programm should wait for certain time and autofill it by itself if the user didn't enter "cin >> i; "

2nd Dec 2019, 9:42 AM
MIRZAEV KHAMIDULLO
MIRZAEV KHAMIDULLO - avatar
2 Answers
+ 1
You'll probably have to look into threading for this. https://en.cppreference.com/w/cpp/thread 1. Create a function that takes the input. 2. Create a thread and let it run the input function. 3. Terminate the thread after n seconds in the main thread. 4. Check the thread's error code to determine a timeout or not. If for some reason you don't have access to the threading library and are on windows then these functions are probably the important ones from the windows API: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createthread https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobject
2nd Dec 2019, 1:01 PM
Dennis
Dennis - avatar
+ 1
using threads unfortunately sololearn compiler ain't good with handling thread one of the reason maybe because Sololearn compilers arent the real compilers they uses compiler from SERVER.
2nd Dec 2019, 5:15 PM
Rohit