-3

I'm making a simple typing game with c++, but I got stuck at the end where I need to create a timer. The timer will be used so that the player have a little amount of time (5s) to type what's on the screen cause the game have no meaning without it.

  • 3
    [sleep_for](https://en.cppreference.com/w/cpp/thread/sleep_for) – PaulMcKenzie Jun 01 '22 at 17:19
  • 1
    What do you mean by "a timer"? Are you asking how to make your program do nothing for 5 seconds? Are you asking how to allow your program to do other work and change what it's doing after 5 seconds? Are you asking how to launch another thread that does work without interrupting your main thread? A "timer" can be very simple or very complex, depending on _what it actually does_. – Drew Dormann Jun 01 '22 at 17:33
  • i want the game to gave the player 5s to type what is on the screen, so i can't use Sleep function. any thoughts? – nour husein Jun 01 '22 at 20:23
  • So you want to get the [start time](https://en.cppreference.com/w/cpp/chrono/system_clock/now), add 5 [seconds](https://en.cppreference.com/w/cpp/chrono/operator%22%22s) to it, and then keep comparing that to the current time, right? – Useless Jun 01 '22 at 20:36
  • I think so...... – nour husein Jun 01 '22 at 21:03
  • You want the input prompt to time out and choose a default value after 5 seconds. So you don't want buffered input because that will wait until something is typed. If you make unbuffered input you get what you want but that depends on the OS you are using. Are you using Windows or Linux or something else? – Jerry Jeremiah Jun 01 '22 at 21:22
  • Some possibly helpful answers: https://stackoverflow.com/questions/28944972/how-to-use-a-timer-in-c-to-force-input-within-a-given-time https://stackoverflow.com/questions/9053175/is-it-possible-to-set-timeout-for-stdcin https://stackoverflow.com/questions/40811438/input-with-a-timeout-in-c – Jerry Jeremiah Jun 01 '22 at 21:24

0 Answers0