Jump to content

How to wait for input for a specific amount seconds and move on in C++ (C++ Console)

Peburu
Go to solution Solved by mariushm,

Then don't use that function anymore, use another function that monitors keys pressed or whatever.

 

See https://docs.python.org/2/library/msvcrt.html#console-i-o

 

It's a python documentation page, but the functions are in msvcrt.dll , c++ runtime...  see  kbhit , getch etc

some of those functions will allow you to get characters and not echo the character on console so you'll have to echo the character yourself.

Also, you'd have to handle the special keys like left right delete ... my advice would be to only accept backspace and enter besides alphanumeric keys and space which should be enough for your needs.

 

Or you can use API functions like ReadConsoleInput, GetNumberOfConsoleInputEvents to see if there's even some input to handle before calling ReadConsoleInput and so on...

See https://docs.microsoft.com/en-us/windows/console/readconsoleinput

and see Console-Docs/low-level-console-input-functions.md at master · MicrosoftDocs/Console-Docs · GitHub

 

Store the time or number of milliseconds since the OS started in a variable, print text on console, then go in a loop in which you see if there's a key pressed or not, and update the time or number of milliseconds since OS started and compare the new value with the original value and exit loop if the time expired.

 

 

I am trying to make a text based game as a assignment in c++ and I have wanted to know if anyone have any idea of how to wait for a specific amount of time for input from the user in console and if the time period is over the console app will keep moving on. I tried searching online but couldn't find a proper answer for this anywhere.

 

Link to comment
Share on other sites

Link to post
Share on other sites

There are functions either in the C runtime or part of the operating system which allow you to pause your application for an amount of time. search for functions named sleep or similar. See the answers to : https://stackoverflow.com/questions/4184468/sleep-for-milliseconds

 

Other methods would be to get the time of day (there's functions for that) and keep getting the time of day in a loop until the time of day is N seconds higher. 

Windows has some API functions that tell you the number of ticks or milliseconds since the operating system was launched (but with some rough granularity, like 10..16 ms increments) ... see for example GetTickCount64 : https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-gettickcount64

You can query that function, then do a loop and keep querying that function until the value returned is original value + 1000ms to delay for ~1s

 

There's high resolution timers you can use but that gets more complicated.

Link to comment
Share on other sites

Link to post
Share on other sites

19 minutes ago, mariushm said:

There are functions either in the C runtime or part of the operating system which allow you to pause your application for an amount of time. search for functions named sleep or similar. See the answers to : https://stackoverflow.com/questions/4184468/sleep-for-milliseconds

 

Other methods would be to get the time of day (there's functions for that) and keep getting the time of day in a loop until the time of day is N seconds higher. 

Windows has some API functions that tell you the number of ticks or milliseconds since the operating system was launched (but with some rough granularity, like 10..16 ms increments) ... see for example GetTickCount64 : https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-gettickcount64

You can query that function, then do a loop and keep querying that function until the value returned is original value + 1000ms to delay for ~1s

 

There's high resolution timers you can use but that gets more complicated.

This wont fix my issue ,my problem is that i am using an cin statement to get some input from the user and cin can pretty much stay as long as i dont give any inputs .i dont want that i want to wait for a specific time that i can wait for an input and if the time is over i will just move over the cin line and move on to the other parts of code.

Link to comment
Share on other sites

Link to post
Share on other sites

Then don't use that function anymore, use another function that monitors keys pressed or whatever.

 

See https://docs.python.org/2/library/msvcrt.html#console-i-o

 

It's a python documentation page, but the functions are in msvcrt.dll , c++ runtime...  see  kbhit , getch etc

some of those functions will allow you to get characters and not echo the character on console so you'll have to echo the character yourself.

Also, you'd have to handle the special keys like left right delete ... my advice would be to only accept backspace and enter besides alphanumeric keys and space which should be enough for your needs.

 

Or you can use API functions like ReadConsoleInput, GetNumberOfConsoleInputEvents to see if there's even some input to handle before calling ReadConsoleInput and so on...

See https://docs.microsoft.com/en-us/windows/console/readconsoleinput

and see Console-Docs/low-level-console-input-functions.md at master · MicrosoftDocs/Console-Docs · GitHub

 

Store the time or number of milliseconds since the OS started in a variable, print text on console, then go in a loop in which you see if there's a key pressed or not, and update the time or number of milliseconds since OS started and compare the new value with the original value and exit loop if the time expired.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×