Thread: Stopwatch Function

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by bithub View Post
    Adak: Where is that delay() function coming from?
    Oh Gawd! I slipped into Turbo C mode, didn't I?

    Salem was asking "Why would anyone want to use Turbo C?" For me, it's because TC lets me do these kind of little programs with one hand behind my back, blindfolded.


    Code:
    /* Adak, Sept 23, '09.
    stopwatch type program for Turbo C. Adjust loss or delay(), 
    for increased accuracy.
    
    Status: ok
    */
    
    #include <stdio.h>
    #include <time.h>      //for clock()
    #include <dos.h>       //for delay
    #include <conio.h>     //for getch()
    #define ESC 27  
    
    //27 = 0x1B
    //VK_ESCAPE (0x1B) ESC key- trap the key press maybe for Windows ?
    
    
    int main(void) {
      clock_t start, stop, loss;
      int i, seconds1, seconds2, total_sec, keyPress;
    
      loss = 20;
      total_sec = 0;
      start = clock();
      seconds1 = start / CLK_TCK;               //divide by TICKS_PER_SEC for Visual C;
      for(i = 0; i < 36; ++i)
         putchar('\n');
      gotoxy(10, 8);
      printf("Press ESC to Quit");
      do {
      
        /*pause in milliseconds (the OS will "schedule" the actual delay),
          giving it at least 200, but usually more)
        */
        delay(200);  
        stop = clock();
        start = stop + loss;  //See note
        seconds2 = stop  / CLK_TCK;    //TICKS_PER_SEC;
        if(seconds2 > seconds1) {
          seconds1 = seconds2;
          total_sec++;
        
         /* SetConsoleCursorPosition for Windows (Visual C), (or gotoxy() if you have 
            conio.h), is good for this:
         */
          gotoxy(10, 10);
          printf( "Elapsed Time: %d seconds", total_sec);
          if(kbhit())
            keyPress = getch();
        }
      } while(keyPress != ESC);
    
      printf("\n\n\n\t\t\t     press enter when ready");
      i = getchar();
      return 0;
    }
    It's apparently accurate for 60 seconds, in an off-hand test. It would need adjusting to be even close to accurate.

    This version gives a more "stopwatch" look to it:
    Code:
    /* Adak, Sept 23, '09.
    stopwatch type program for Turbo C. Adjust loss or delay(), 
    for increased accuracy.
    
    Status: ok
    */
    
    #include <stdio.h>
    #include <time.h>
    #include <dos.h>
    #include <conio.h>
    
    #define ESC 27  
    
    //27 = 0x1B
    //VK_ESCAPE (0x1B) ESC key- trap the key press
    
    
    int main(void) {
      clock_t start, stop, loss;
      int i, seconds1, seconds2, total_sec, keyPress;
    
      loss = 20;
      total_sec = 0;
      start = clock();
      seconds1 = start / CLK_TCK;               //TICKS_PER_SEC;
      for(i = 0; i < 36; ++i)
         putchar('\n');
      gotoxy(10, 8);
      printf("Press ESC to Quit");
      do {
      
        /*pause in milliseconds (the OS will "schedule" the actual delay),
          giving it at least what you request, but usually more)
        */
        delay(100);  
        stop = clock();
    //    start = stop + loss;  //See note
    //    seconds2 = stop  / CLK_TCK;    //TICKS_PER_SEC;
        gotoxy(10,10);
        printf("Elapsed Time: %.2f ", (stop-start)/CLK_TCK);
    /*
        if(seconds2 > seconds1) {
          seconds1 = seconds2;
          total_sec++;
        
         // SetConsoleCursorPosition (or gotoxy() if you have conio.h), is good for this:
          gotoxy(10, 10);
          printf( "Elapsed Time: %d seconds", total_sec);
        }
    */
          if(kbhit())
            keyPress = getch();
    
      } while(keyPress != ESC);
    
      printf("\n\n\n\t\t\t     press enter when ready");
      seconds1 = getchar();
      return 0;
    }
    Last edited by Adak; 09-23-2009 at 02:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM