c++ timer and enter key localising

so i wanna make a timer which counts up to when the user presses enter key. any idea on how that could happen if i'm using

int wait(int seconds)
{
clock_t endwait=clock()+seconds*CLOCKS_PER_SEC;
while(clock()<endwait){}
}

and a for loop with call to wait(1) within to count up to that time?
Last edited on
Is your function returning an integer or not?

The function "clock()" in 'ctime.h' queries and returns a value that can be thought of as running constantly in parallel with your program so you could eliminate that "while(...)" loop. Just sit there with std::cin.ignore(256, '\n'); and return the result of "clock()" from your function after the user presses enter.
my idea is that i wanna show how much time has elapsed after every second
i've done a countdown like this
int wait(int seconds)
{
clock_t endwait=clock()+seconds*CLOCKS_PER_SEC;
while(clock()<endwait){}
}
int countdown(int hours,int minutes)
{
long long seconds=(hours*3600)+minutes*60;
for(seconds;seconds>=0;seconds--)
{
wait(1);
if(seconds<=5){cout<<"\a";}
if(seconds/3600<10){cout<<"0"<<seconds/3600<<":";}
else{cout<<seconds/3600<<":";}
if((seconds%3600)/60<10){cout<<"0"<<(seconds%3600)/60<<":";}
else{cout<<(seconds%3600)/60<<":";}
if((seconds%3600)%60<10){cout<<"0"<<(seconds%3600)%60<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n";}
else{cout<<(seconds%3600)%60<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n";}

}
cout<<"\a\a\a";wait(2);cout<<"\a\a\a";wait(2);cout<<"\a\a\a";wait(2);
}

it refreshes the seconds every second i want the timer to refresh the seconds every time like this but i cant think how can i implement the enter-stopping :D
You need a second thread to either update the display or watch for input from the user. Other then that when the application expects input from a stream it stops processing until it recieves it, we call C++ an "Event Driven" language.
so what you're saying is i can move the display update to wait function and then make the countdown func wait for input from the user? :D
Yes this is exactly what I'm saying. However what you want is for both functions to run at the same time and this requires at least two threads.

EDIT: SFML has a quick to learn and easy to use Multi threading component: http://www.sfml-dev.org/tutorials/1.6/system-threads.php
This would be my suggestion if you aren't already comfortable with another lib.
Last edited on
hmm i kinda have a restriction not to use external libs :D
Would that be a personal restriction? Scholastically impossed? Or is this part of a bet? Because depending on how strict the restrictions are, we may be able to cheat a little bit.
well i'll be sending this program as a part of my candidature for a grant so ... less cheating :D
k leave this, have you got any idea on the alarm clock? i need the alarm clock to go off at a given time but I can't seem to get the difference between the NOW time and alarm ring time? i dont think time() or clock() helps me any and am out of ideas
Wait, hold on there. You don't need extra threads - the OS does timed/wait stuff on input for you, with the same efficiency as separate threads can.

On Windows, you'll want to use WaitForSingleObject() to wait for user input.
http://www.cplusplus.com/forum/beginner/5619/#msg25047

On Linux, use poll() or select().
http://www.cplusplus.com/forum/general/5304/#msg23940

For an alarm clock, you'll need to periodically check time() to see whether or not the time is equal to or later than the alarm time. Do this in your wait/poll loop, and break if the time is done.

Hope this helps.
The Win32 and the Linux API would still be external libs. This is still do-able but it won't be nice and neat.

For your alarm clock you'll also have to launch the process as a service which brings us back into 3rd party libs. This is because the alarm can't go off if it isn't running and for some reason I doubt they'll except having to execute it themself everytime. You'll also want to take measures to ensure you don't saturate the CPU with NOOP commands.
You are saying that using the OS API is an external libray?
You do realize that every program must link to the OS to run, right?

The functions I mentioned are specifically designed to yield until something happens...
well time() just returns the time elapsed since 1970 doesn't it, how does that help me? :D I mean isnt there an easier way than calculating the time we are now? :D
Last edited on
Well, most of us in the Western Hemisphere measure time since the year Christ was born. So how does that help me tell what the date is?

Read through the documentation for very simple examples about using time.
http://www.cplusplus.com/reference/clibrary/ctime/
In particular, you are interested in taking a user-supplied time and converting that to a computer-supplied time so that you can compare them.

Good luck!
Topic archived. No new replies allowed.