Comparing two time programs

This is a discussion on Comparing two time programs within the C++ Programming forums, part of the General Programming Boards category; Okay, this simple program works just fine: Code: #include <iostrem> #include <ctime> using namespace std; int main() { time_t myTime; ...

  1. #1
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53

    Comparing two time programs

    Okay, this simple program works just fine:
    Code:
    #include <iostrem>
    #include <ctime>
    using namespace std;
    
    int main()
    {
    
      time_t myTime;
      time ( &myTime );
      printf ( "Current date and time are: %s", ctime (&myTime) );
    
     return 0;
    }
    Yet this one doesn't (it compiles but doesn't return anything):
    Code:
    #include <iostream>
    #include <ctime> 
    using namespace std;
    
    class Date 
    {
       public:
          Date();  
          ~Date();  
    	
       private:
          int myTime;
    
    
    }; // end class Date
    
    Date::Date()
    {
      time_t myTime;
      time ( &myTime );
      printf ( "Current date and time are: %s", ctime (&myTime) );
    }
    
    int main(){
    
       Date h();
      
       return 0;
    }
    As you can see, I'm trying to create a constructor to do the same thing that the first program does. Any info is greatly appreciated!
    Last edited by Kayoss; 04-10-2006 at 02:10 PM.
    THE redheaded stepchild.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    http://www.parashift.com/c++-faq-lit....html#faq-10.2
    Code:
    #include <cstdio>
    #include <ctime> 
    using namespace std;
    
    class Date 
    {
       time_t myTime;
    public:
       Date() : myTime ( time ( NULL ) )
       {
          printf ( "Current date and time are: %s\n", ctime (&myTime) );
       }
    };
    
    int main()
    {
       Date h;
       return 0;
    }
    Last edited by Dave_Sinkula; 04-10-2006 at 02:07 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Great link, thanks a lot!
    THE redheaded stepchild.

  4. #4
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Another question - is it possible to use strcpy to copy Date h into a string? I'm getting conversion errors ie "cannot convert parameter from class Date to char *"

    Code:
       strcpy ( timeToConvert, h ); // copy system time into timeToConvert
    THE redheaded stepchild.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Context is wonderful.
    Code:
    #include <cstdio>
    #include <ctime> 
    #include <string>
    
    class Date 
    {
       std::time_t myTime;
       std::string text;
    public:
       Date() : myTime ( std::time ( 0 ) ), text ( std::ctime ( &myTime ) )
       {
          std::printf ( "Current date and time are: %s\n", text.c_str() );
       }
    };
    
    int main()
    {
       Date h;
       return 0;
    }
    
    /* my output
    Current date and time are: Mon Apr 10 16:58:56 2006
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Oops, here is an example, since we haven't covered <string> just yet:

    Code:
    #include <iostream>
    #include <ctime> // for time
    #include <cstring> // for strings
    
    using namespace std;
    
    class Date 
    {
       time_t myTime;
    
    public:
       Date() : myTime ( time ( NULL ) )
       {
          printf ( "Current date and time are: %s\n", ctime (&myTime) );
       }
    
    }; // end class Date
    
    int main()
    {
       Date h;
    
       string timeToConvert[30];
       char *tokenPtr;
       strcpy ( timeToConvert, h ); // copy system time into timeToConvert
       cout << timeToConvert;
       tokenPtr = strtok( timeToConvert, " ");
       while (tokenPtr !=NULL) {
          cout << tokenPtr << '\n';
    	  tokenPtr = strtok(NULL, " "); // get next token
    
       } // end while
    
       cout << "\nAfter tokenizing, timeToConvert is: " << timeToConvert << endl;
      
      
      return 0;
    }
    The error is "Cannot convert parameter 1 from class to char *".
    THE redheaded stepchild.

  7. #7
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    >> string timeToConvert[30];

    c++ strings do not work like c-strings. This statement is declaring an array of 30 string objects. You must now learn how the c++ strings work. they work differently from c-strings so all the strcpy, strcmp and those functions no longer apply. Check this link http://www.cprogramming.com/tutorial/string.html

    [edit]
    Sorry, had inserted a wrong link, please check the new one
    [/edit]
    Last edited by LinuxCoder; 04-10-2006 at 05:33 PM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You'd need some public method to get at the private myTime value in the class Date. Something along this line.
    Code:
    char *Date::foo()
    {
       return asctime(localtime(&myTime));
    }
    Then you'd need to copy the text pointed to by the pointer returned by this function to a char array.
    Code:
       char timeToConvert[30];
       strcpy ( timeToConvert, h.foo() ); // copy system time into timeToConvert
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  3. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 05:00 PM

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