-1

I have some knowledge about C++, but I stumbled upon an problem. I want the user to enter some text, but when it takes longer than X seconds, the program will go on. I guess, it is not possible, but maybe you know something about it.

It will be in the command line, without GUI. I am not certain how the programm will look like, but it will be something like a CMD-RPG. I wanted to use Quick Time Events to make it a little bit more exciting.

Not a real meerkat
  • 5,604
  • 1
  • 24
  • 55
Lars S
  • 13
  • 2
  • Your question is extremely unclear. What kind of program? – SLaks Jul 23 '18 at 20:44
  • 1
    Is this going to be command line or GUI? – NathanOliver Jul 23 '18 at 20:45
  • You'll need to set up some threading to get this done. A timer thread that will emit a TIMEOUT signal and another thread that will emit a RESPONSE_GIVEN signal. The main running thread will have to dispatch both of these and wait for either of these signals and respond accordingly. – MPops Jul 23 '18 at 20:55
  • This is not possible with standard C++ alone. You need some platform specific code (or library which does this). So you need to give more details about restrictions and programming environment than just "C++". – hyde Jul 23 '18 at 21:02
  • https://stackoverflow.com/questions/40811438/input-with-a-timeout-in-c seems seems exact duplicate? – hyde Jul 23 '18 at 21:23
  • Possible duplicate of [Input with a timeout in C++](https://stackoverflow.com/questions/40811438/input-with-a-timeout-in-c) – 1201ProgramAlarm Jul 23 '18 at 21:26

2 Answers2

1

I cant comment so I will just leave this here

Input with a timeout in C++

Diogo
  • 461
  • 1
  • 5
  • 7
0

Since I cannot comment, I will simply leave this as an answer.

One possible way to solve this problem is to have 2 threads:

Input capture thread: since receiving input is a thread-blocking action, you should have a thread that simply polls for user input, placing that input into a thread-safe buffer

Quick-time function on main thread: a function that will be responsible for executing the quick-time event. Something like the following could be done (pseudo code):

clear input buffer //the buffer provided by the input capture thread

bool success = false;

while(current time < ending time)
{
    if(input buffer isn't empty)
    {
        get contents of input buffer and send contents to cout
        if (user has finished input correctly)
        {
            success = true;
            break;
        }
        clear buffer
    }
}
return success;

For this to work, you would need to turn off echo on the command prompt (see this page for more info)

Although Diogo's reference is excellent (and informative), I believe this answer is more applicable than since the OP has indicated that this program will be ran on Windows.

Jack
  • 520
  • 2
  • 16