uses for time.h

This is a discussion on uses for time.h within the C++ Programming forums, part of the General Programming Boards category; I was just wondering about the header file "time.h" i havent used it really and what information can you get ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    uses for time.h

    I was just wondering about the header file "time.h" i havent used it really and what information can you get from it and how (date, hour, year, etc.)

    Thanks in Advance!

  2. #2
    Banned
    Join Date
    Oct 2004
    Posts
    250
    You can use it to get the current time

    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    int main()
    {
    	time_t TheTime;
    	time(&TheTime);
    
    	cout <<ctime(&TheTime)<<endl;
    	cin.get();
    }
    and you can use it to generate a random number

    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    int main()
    {
    	srand ((unsigned)time(0));
    	int a_random_number = rand()%10; // generates a random number between 1-10
    
    	cout <<"The number is "<<a_random_number<<endl;
    	cin.get();
    }
    and im sure there are plenty of other uses for it

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >what information can you get from it
    References are good.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb See the FAQ...

    There's a good introduction to <ctime> / <time.h> in the Programming FAQ. (The examples in the FAQ use the "old" dot-h headers.)

    Note that <ctime> uses pointers and structures. So, if you don't know about these yet, you won't fully understand <ctime>.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    cgod,
    Your second example requires <cstdlib> for the srand/rand functions (granted, it may compile without it because the header may happen to be included elsewhere, or automatically by the compiler, it is still a good idea to have the #include there for portability and predictability).

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21