Solved

Timer in a C++ program

i want to make a program of timed quiz (just like KBC).
I want to make a timer in this program.

Working of program
program will ask a question from the user and will show 4 options and will wait for user to enter a option, if user is not able to answer or is not providing any input then in 30 seconds program will display a message of 'gameover'.

So how to introduce this timer in a C++ program?
Please give me the codding of this timer (only need coding of timer).
10 answers Last reply Best Answer
More about timer program
  1. truegenius said:
    i want to wake a program of timed quiz (just like KBC).
    I want to make a timer in this program.

    Working of program
    program will ask a question from the user and will show 4 options and will wait for user to enter a option, if user is not able to answer or is not providing any input then in 30 seconds program will display a message of 'gameover'.

    So how to introduce this timer in a C++ program?
    Please give me the codding of this timer (only need coding of timer).


    All it takes i a quick reference search:

    http://www.cplusplus.com/reference/clibrary/ctime/clock/
    http://www.cplusplus.com/reference/clibrary/ctime/time/

    Because this looks like some sort of homework, I will not code it for you, just give you pointers and references so you can learn.
  2. those links are explaining how to use clock and time.
    but i need to know that how to give input in-between that countdown.

    it is not my homework so you can freely provide me some codding on how to accept input from user in-between that countdown.
  3. Timers tend to be dependent on the OS. What OS are you targetting and which C++ compiler are you using?
  4. borland turboc v3.0 on windows 7 ultimate 32bit
  5. I'm afraid that, as far as I know, there is no way to do this using Turbo C. (well, never say never, but it would certainly be extremely difficult if it is possible). It is a very old compiler and cannot produce multithreaded code, or call the Windows APIs, which is what you really need.

    This would be trivial if you were using a compiler that could access the Windows API, such as Visual C++. Turbo C was a fine language in its day, but that day is long past. I am constantly amazed by the number of people still using Turbo C when there are so many more modern compilers available.

    Edit - on second thoughts, I believe that Turbo C had a delay() function. So write a loop that checks for a keypress, does a one-second delay and increments a counter. Exit the loop when a key is pressed or the router reaches, say, 30. It's not very clean or accurate, but should work. I'll leave you to fill in the details.
  6. "checks for a keypress"+ 'in between that countdown' thats the exact thing that i want to do but don't know how to.

    i too have visual c++ and borland turbo c v4.5 windows.
  7. Best answer
    I believe there is a function kbhit() in Turbo C that checks if a key has been pressed.

    If you want to use Visual C++ then Googling "Windows Timer" should give you plenty of hits explaining how to use timers in Windows.
  8. kbhit() seems close to the function that i want, i tried it to use it but unable to make it work as i don't know much, so i am still waiting for more hint or program.
  9. hey guys, you forget about me
    it is not my home work
    it is a part of a series of many games that i want to make, and i want to know how to do this thing as it is common in my games.
  10. Best answer selected by truegenius.
Ask a new question

Read More

Programming Apps