how can i make the program depend on time

Reply

Join Date: May 2004
Posts: 15
Reputation: dooda man is an unknown quantity at this point 
Solved Threads: 0
dooda man dooda man is offline Offline
Newbie Poster

how can i make the program depend on time

 
0
  #1
Jun 1st, 2004
hi , i am using simple console application and i am asking about the way to make the program depend on time insead of dependng on pressing keys for example, i want siplay the word "Hello.." and after 5 seconds it changes to "World" , to elaborate .. i want system("pause") but depending on time not on keys.
thnx
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: how can i make the program depend on time

 
0
  #2
Jun 1st, 2004
Something like this?
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. int main(void)
  5. {
  6. time_t now, then = time(&now) + 5;
  7. if ( now != (time_t)(-1) )
  8. {
  9. const char *text[] = {"Hello","World"};
  10. size_t i = 0;
  11. for ( ;; )
  12. {
  13. printf("\r%s", text[i]);
  14. fflush(stdout);
  15. if ( ++i >= sizeof text / sizeof *text )
  16. {
  17. i = 0;
  18. }
  19. while ( then > time(&now) );
  20. then = now + 5;
  21. }
  22. }
  23. return 0;
  24. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 51
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: how can i make the program depend on time

 
0
  #3
Jun 4th, 2004
Hello,

Unfortunately, that code above is unfriendly to other processes on the computer, because of the infinite loop that is run. Granted, in this close example, you are looking at 5 seconds, and that won't cause something to croke.

But, if I had to do a delay in the minutes range, that would peg the CPU, and cause a system admin to come and shut you down. That might not matter today, but back in the day when CPU seconds = money on the mainframe, a loop like that could run you out of money.

There has to be a better way without doing the raw iteration!

(I had a program that needed the delay, and I ended up using a part C++ program and part of a BASH shell script so that I could do the wait() command that did not use CPU power during the delay)

Christian
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: how can i make the program depend on time

 
0
  #4
Jun 5th, 2004
You could use GetTickCount().

  1. double next_time = GetTickCount();
  2.  
  3. while (true)
  4. {
  5. if(GetTickCount() >=next_time + 100)
  6. {
  7. cout<<"Bong\n";
  8. next_time = GetTickCount();
  9. }
  10. }
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC