Timer in a C++ program

truegenius

Distinguished
Oct 22, 2011
113
0
18,660
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).
 
Solution
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.

theDanijel

Distinguished
May 4, 2011
74
0
18,590


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.
 

truegenius

Distinguished
Oct 22, 2011
113
0
18,660
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.
 

Ijack

Distinguished
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.
 

Ijack

Distinguished
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.
 
Solution