Thread: Date Class

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    8

    Date Class

    I found this code that pretty much does exactly what I want. (minus forcing the change of a date) but I don't understand it to be able to produce a similar one.

    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    //Date class
    class Date
    {
    public:
          Date(int DDD, int YYYY);                    //constructor for DDD YYYY overloaded Dateone()
          Date(int MM, int DD, int YY);               //constructor for MM/DD/YY overloaded Datetwo)
          Date(char data[20]);
    private:
         int day;
         int month;
         int year;
         char mon;
    
    };
    Date::Date(int DDD, int YYYY)
    {
         day = DDD;
         year = YYYY;
         cout << day << " " << year << "\n";
    }
    
    Date::Date(int MM, int DD, int YY)
    {
         day = DD;
         month = MM;
         year = YY;
         cout << month << "/" << day << "/" << year << "\n";
    
    }
    
    
    
    Date::Date(char data[20])
    {
    
             
    
         cout << data << "\n";
    }
    int main(void)
    {
    
            struct tm *t;
            int day, mon, year;
            char *month[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
            char date[100];
            time_t rawtime;
    
            time ( &rawtime );
            cout << ( "Current date and time are: ", ctime (&rawtime) );
    
           t = localtime(&rawtime);
    
            day = t->tm_mday;
            mon = t->tm_mon + 1;
            year = t->tm_year + 1900;
            sprintf(date, "%s %d, %d", mon[mon - 1], day, year);
    
            Date One(day, year);
            Date Two(mon, day, year);
            Date Three(date);
    
            return 0;
    }
    I don't understand the purpose of this 'data' function tho it doesnt work without it.
    Also its my first time using ctime so I dont understand the main much.. please correct me if I'm wrong
    so from
    Code:
    time_t rawtime;
    
            time ( &rawtime );
            cout << ( "Current date and time are: ", ctime (&rawtime) );
    
           t = localtime(&rawtime);
    
            day = t->tm_mday;
            mon = t->tm_mon + 1;
            year = t->tm_year + 1900;
            sprintf(date, "%s %d, %d", mon[mon - 1], day, year);
    is all necessary to get the pc time and display it? if I just want the pc to know I would delete after the t= part cus the rest is basically just setting it up to print right?

    I dont understand the purpose of the struct in the main nore the char date either..

    currently my code i'm developing is
    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    class Date {
    
    private:
      int day;
      int month;  
      int year;  
    public:
      Date();
      Date( int,int,int); 
      void FormatTime();
      void print(); 
    }; 
    Date::Date( int m, int d, int yr )
    {
        if ( m > 0 && m <= 12 )  // validate the month (Jan-Dec)
            month = m;
        else {                     
           month = 1;
           cout << "Month " << m << " invalid. Set to month 1.\n";
              }
    
        if ( d > 0 && d <= 31 )  // validate the day (0-31)
            day = d;
        else {                     
           day = 1;
           cout << "day " << d << " invalid. Set to day 1.\n";
              }
         year = yr;  
             
    }
    
    void Date::FormatTime()
    {
        //in hear change number into an actual month (1-january)
    }
    
    
    void Date::print() 
    {
        cout << month<<"/"<<day<<"/"<<year <<endl; // mm/dd/year
    	cout << day <<", "<< year << endl;  //dd year
    
    } 
    
    
    int main()
    {
        
    }
    I was doing it so that the user puts in the date but then I realized the question says "read about <ctime> and implement it. Which would make my code all useless cus I'm suppose to be using the pc time...

    Any tips/help is appreciated

    Thanks!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Eh, where to start...
    OK, first off, the Date(char data[20]) constructor is incomplete it seems. My guess it that it takes in a date in string form (similar to the constructor that takes year, month and date).
    As for the main function, here's a breakdown:
    Code:
    int main(void)
    {
    
            struct tm *t; // Struct to hold date
            int day, mon, year;
            char *month[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
            char date[100];
            time_t rawtime;
    
            time ( &rawtime ); // Gets the current time and stores it in rawtime.
            cout << ( "Current date and time are: ", ctime (&rawtime) );
    
           t = localtime(&rawtime); // Converts the raw time to local time (time zone thingys) and returns a tm struct with the date.
    
           // Takes the date and converts it to a "real" date.
           // The tm struct begins counting years from 1900 and days are relative to 0, so the first day in a month is 0, not 1.
           // Lastly prints the date.
            day = t->tm_mday;
            mon = t->tm_mon + 1;
            year = t->tm_year + 1900;
           // The -1 is to adjust the month value to an index for the month array and print the month name in that index.
            sprintf(date, "%s %d, %d", mon[mon - 1], day, year);
    
           // Construct date classes from the year.
            Date One(day, year);
            Date Two(mon, day, year);
            Date Three(date);
    
            return 0;
    }
    Also, the date is printed twice. The first two lines should get the date.
    The second is a little more verbose and allows you to format it a little better, not to mention it's in a much easier format.
    And also, this:
    Date( int,int,int);
    Don't remove the names there!
    http://sourceforge.net/apps/mediawik...arameter_names

    Boost also has a date class you can look into.
    Last edited by Elysia; 02-10-2010 at 02:26 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    There is a very nice Date (or maybe Date/Time) class available for free on snippets.org...it is a "kitchen sink" class so you may want to trim it down for your purposes but I have used it in the past and found it quite nice...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Boost is known quality and portable, so that's where I would look first for reusable code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  3. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM