Thread: is there a timer function?

  1. #1
    Unregistered
    Guest

    is there a timer function?

    if so, does it have to do with time.h?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    2 things.

    First, If you're talking about microsoft C, you can get the source code from the site in the source code section. If not, you can find out by looking in the file for time.h They will have all the function declarations in there. It'll probably be called timer() or something really obvious. Hope you find it!

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    What operating system are you using, and what compiler?

  4. #4
    Unregistered
    Guest
    I'm using codewarrior. And it's on win 2000. But I'll look at the source code.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One option is to use GetTickCount(), part of the win32 api. Include <windows.h> to use.
    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main(void)
    {
       DWORD start, end;
    
       cout << "Hello." << endl;
       start = GetTickCount();
       Sleep(1000);  //Sleep 1 second
       end = GetTickCount();
       cout << "start:" << start << "  end:" << end << endl;
       cout << "millisec elapsed:" << end-start << endl;
       cout << "Good bye." << endl;
       return 0;
    }
    I think time.h also has some functions for timing, but may not be as accurate. For more accuracy than milliseconds, use the search function. There was a good post about this a couple of months ago on the c programming board.
    Last edited by swoopy; 12-14-2001 at 01:29 AM.

  6. #6
    Unregistered
    Guest
    Thanx!!! That helps out alot. I was looking to find milliseconds. However, do I have to use namespace? I don't really enjoy using that.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >However, do I have to use namespace?
    No, in fact I'm not real fond of it myself. Just:

    #include <iostream.h>
    #include <windows.h>

    and leave out the namespace.

  8. #8
    Unregistered
    Guest
    i've written a simple little function that takes a "Timeout" for x number of seconds ( a double variable ):

    include <time>

    void Timeout(double length)
    {
    int starttime = time(0);

    while(time(0) - starttime < length)
    {
    }
    }


    so Timeout(.5) would wait for a half second.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM