943,704 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3519
  • C RSS
Jun 1st, 2004
0

how can i make the program depend on time

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dooda man is offline Offline
15 posts
since May 2004
Jun 1st, 2004
0

Re: how can i make the program depend on time

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. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 4th, 2004
0

Re: how can i make the program depend on time

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
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
Jun 5th, 2004
0

Re: how can i make the program depend on time

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. }
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to control a CD-Drive
Next Thread in C Forum Timeline: Quick Polymorphism Tutorial





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC