   Timer question
  Hello everyone!I just started learning C++ and have gone through FOR & WHILE loops aswell as if statements.In order to make sure I got it all down I decided to create a program with my newly gained knowledge, but I'm having a problem.In my program I'm asking the user a question and want them to answer it within ten seconds, or the program shuts down.Problem is, I have no idea how to create a timer.What I did first was to create a for loop and have Sleep(1000); in the code block, so that it wouldn't count to ten instantly, but then I figured out that Sleep most likely pauses my whole program, correct?And even if it didn't, it doesn't work as c++ code seems to execute sequently.. that is, if I make a for loop and then a while loop, the while loop won't execute until the for loop is done.Anyway, I don't have too much knowledge yet so, is there an easy way to create a timer in a console application?Preferrably using the knowledge that I already have.Thanks a bunch!   
  I don't know if in C++ exist something like a timer in some library...But you can use time function, or whatever function that gives you the current time. Time is usualy precise to ms. So you remeber the time before you ask the question. In the loop you do what you have to (processing eventual user answer, etc) and at the end of the loop you call the time function again, compare the times (acctual time and the one you remeberd before you asked the question), and you if 10 s passed you exit.The only problem is that hadling somethnig like this with time is a little trick, because you can have problems when the first time (before asking) is 10:55:11 (min:sec:milisec). Then the time after 10 s is 11:05:11, and you must pay attention to the minutes.   ;  Actually, the timer really isn't the tricky part:Quote:Original post by chazzyzI just started learning C++ and have gone through FOR & WHILE loops aswell as if statements.In order to make sure I got it all down I decided to create a program with my newly gained knowledge, but I'm having a problem.In my program I'm asking the user a question and want them to answer it within ten seconds, or the program shuts down.Problem is, I have no idea how to create a timer.This stuff is usually pretty much platform dependent, but you may want to check out the time.h or ctime headers. As was already pointed out, a timer is not much more than a function that takes the time at two points and compares the resulting delta to trigger an event.However, you'll find that timers themselves do not solve the actual problem you have described, that is terminating a running function upon timer expiration-this stuff is even more platform-dependent and is usually accomplished using threads (i.e. pthreads).Quote:Original post by chazzyzWhat I did first was to create a for loop and have Sleep(1000); in the code block, so that it wouldn't count to ten instantly, but then I figured out that Sleep most likely pauses my whole program, correct?And even if it didn't, it doesn't work as c++ code seems to execute sequently.. that is, if I make a for loop and then a while loop, the while loop won't execute until the for loop is done.That's correct: your program is generally SEQUENTIAL, that is any timers or input functions would normally be run sequentially if not designed properly-so in a simple C program, there's no built-in way to terminate a running function after a certain event has occured from within another function, that's when threads usually come in-or alternatively, that's when you start implementing all the required low level stuff manually.Quote:Original post by chazzyzAnyway, I don't have too much knowledge yet so, is there an easy way to create a timer in a console application?Preferrably using the knowledge that I already have.As I said, timers should really not be a problem at all, however as soon as you want to have multiple independent execution paths running in parallel (the timer and the input routine in this case, that is), things become more complicated and you'll have to resort to platform specific stuff and/or threads or manual implementation of all the low level details.However, from a low-level point of view it is of course possible to implement this, I'm simply afraid it may be more confusing than helpful for someone who's just started learning to program. You explicitly said you would want to know of a way within your current knowledge-I don't think, that's possible for the scenario you outlined.Furthermore, the task may become more complicated depending on the type of input you want to process-if you are really interested in getting this to work for you, i.e. as some sort of challenge-and don't want to use too much high level stuff, you may want to look into coroutine design-basically you would still be running (calling) two functions sequentially, however these would be run at a fast speed and would maintain state between calls using static variables, so that the input routine is no longer called as soon as the timer has expired.However, the input routine itself must not be doing any blocking system calls when running.;  Thanks a lot for your replies!EDIT: After reading these first senteces here again I felt that it might have been unclear, so here goes; I understood everything you wrote Everyman and I agree completely.And checking and then comparing time won't work either, as the code works like this;question -> waiting for answer -> while loop (while answer is NOT correct, ask the question again and then check if it's correct using an IF statement)So even if I put Sleep(1000) at the end or if I before the question take the time and save it and then check the time again at the end of that loop, it will be after each time the user has input something, which means if the user is just sitting there and thinking about what the answer may be, nothing happens and they have all the time in the world.At least that's my conclusion from what I've learned so far.So I guess I'm going to have to go with "you have three tries" or something similiar instead.Thanks again fellas![Edited by - chazzyz on September 24, 2006 9:39:32 AM];  You could just check the time before and after they enter their answer, and if the difference is too big you print "sorry, out of time" or something. It's not exactly what you want, but it has the same basic effect. It would sit there waiting for their answer as long as they want, but would tell them afterwards if they were too slow.   ;Quote:Original post by chazzyzAnd checking and then comparing time won't work either, as the code works like this;question -> waiting for answer -> while loop (while answer is NOT correct, ask the question again and then check if it's correct using an IF statement)So even if I put Sleep(1000) at the end or if I before the question take the time and save it and then check the time again at the end of that loop, it will be after each time the user has input something, which means if the user is just sitting there and thinking about what the answer may be, nothing happens and they have all the time in the world.Okay. It's important how the user answers. If it's via keyboard, you can just check if key is pressed then you process the pressed key (in C I think there's a keypressed() function). If a key is not pressed, you just check the time, and countinue the loop.If it's mouse, you can update mouse coordinates in every loopstep.And as stated before, you can resolve this problem with threads.One thread that listen to input, and the other that is timer.Sorry, I can't help you with implementing threads, I know only some theory about it.   ;  Hi!I've solve my "time problems" on console programsusing this useful library: <time.h>On this linkyou can find an example that shows you how to manage and solvethe problem I guess you currently have!I also suggest you to check out all the librariesshowed on the site, they are very useful!   ;  Thanks for your posts!I went with three tries though, since threads surely is way ahead of where I am right now when it comes to knowledge.Clawer: Doesn't that do exactly what Sleep() does? Looks like it at least.Either way, still won't work unless I can run the timer and the while loop at the same time, unfortunately.;  whoops!...it seems I didn't understand very well you problem.I read "just started learning C++" and"I have no idea how to create a timer"so I didn't realize your Sleep() functionhave worked since the beginning.Sorry for the mistake :)