C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-10-2010, 01:40 PM   #1
Registered User
 
Join Date: Nov 2009
Posts: 6
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!
kokoro is offline   Reply With Quote
Old 02-10-2010, 02:23 PM   #2
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,785
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.

Last edited by Elysia; 02-10-2010 at 02:26 PM.
Elysia is offline   Reply With Quote
Old 02-10-2010, 02:49 PM   #3
Registered User
 
jeffcobb's Avatar
 
Join Date: Dec 2009
Location: Henderson, NV
Posts: 541
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
jeffcobb is offline   Reply With Quote
Old 02-10-2010, 02:51 PM   #4
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,785
Boost is known quality and portable, so that's where I would look first for reusable code.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:26 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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