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!