Sign in with
Sign up | Sign in
Solved

Timer in a C++ program

Last response: in Applications
Share

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

More about : timer program

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.
Related ressources
Programming Authority

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.

Best solution

Programming Authority

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