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";}
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.
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.
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
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.
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.
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
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.