0

I have written a guess a number game in C++. The game has 20 tries. If the user doesn't guess the correct number with in 30 seconds I want the game to end and display "time has expired". How do I use a timer in C++ that counts down from 30 seconds to 0 seconds while the user is playing and hasn't guessed the correct number yet? Where in my program should I put that timer code in order to control the timing of my game?

  • one question at a time. ( where / how ) ... What have you tried so fare? Do you have a gui? nail down your problem to a single most specific question. The progress of finding the question may also be the way how you can find the solution your self. Read the forum Guidelines in how asking a question. Provide a code example how fare you have come, and where the issue arises. – Cutton Eye Apr 17 '20 at 10:00
  • How are you getting player input? This might just be as simple as adding a timeout to something or as complicated as having a gui element serviced every frame to update remaining time. Depending on your needs you could also store time at start and time when user responds. – Abel Apr 17 '20 at 10:01
  • https://en.cppreference.com/w/cpp/chrono/high_resolution_clock/now – pptaszni Apr 17 '20 at 10:09
  • 1
    This is quite easy to achieve in a GUI application where the whole application is event driven and user input and timer can be processed "concurrently" (and even without multi-threading). In pure C++, this is not that simple to achieve nor portable because the (console) input is usually blocking. Once you wait for input it's hard to abort this without killing the process (AFAIK). – Scheff's Cat Apr 17 '20 at 10:23
  • FYI: [SO: I/O in concurrent program](https://stackoverflow.com/a/48097134/7478597) – Scheff's Cat Apr 17 '20 at 10:25

0 Answers0