![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 6
| Date Class 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;
}
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);
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()
{
}
Any tips/help is appreciated Thanks! |
| kokoro is offline | |
| | #2 | |
| Mysterious C++ User 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;
}
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:
Last edited by Elysia; 02-10-2010 at 02:26 PM. | |
| Elysia is offline | |
| | #3 |
| Registered User 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... |
| jeffcobb is offline | |
| | #4 | |
| Mysterious C++ User 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:
| |
| Elysia is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |