heyy i am a beginner in c++. i am trying out to develop a code to display the date in dd/mm/yy format .
also i am interested in validating the date( like for eg a month feb only 31 is invalid) and it should display "date invalid" if the date in invalid
the points i have noticed are
1. the days of each month are varying
2. the leap year problem

i also want to develop the code in such a way that we can add and subtract dates. And develop a Concept calender type class where we can see which date refers to which day ( Ex: 12/jan/1989 is a monday).. something like that.
I need help right from the scratch for this one. please help me as soon as possible
thanks in advance

Recommended Answers

All 7 Replies

Break the problem down into manageable pieces, then solve each one individually. That should minimize the smothering effect of a complicated problem and you'll end up with specific questions that we can answer and specific problems that we can help with.

Asking for "help right from the scratch" suggests that you want us to do the design grunt work for you, which doesn't teach you anything.

#include<iostream.h>
#include<conio.h>
int i=1;
class date
{
private : int day,mont,year,month[12];

public : void read();
void operator <<(date d);
void operator +(int d);
void operator -(date d);

date()
{
month[1]=month[3]=month[5]=month[7]=month[8]=month[10]=month[12] =31;
month[4]=month[6]=month[9]=month[11] = 30;
month[2] =28;
}
};

void date :: read()
{
cout<<"entr the "<<i<<"date :\n";
i++;
cin>>day>>mont>>year;
}
void date :: operator<<(date d)
{
cout<<d.day<<"/"<<d.mont<<"/"<<d.year;
}
void date :: operator-(date d2)
{

if((year<d2.year)||(year==d2.year && mont<d2.mon)||(year==d2.year && mont==d2.mont &&day<d2.day))
{
cout<<"Subtraction not possible ";
getch();
exit(0);
}
while(year!=d2.year || mon!=d2.mon || day!=d2.day)
{
d2.day++;

if((d2.year%4==0 && d2.mon==2) ? d2.day>29 : d2.day > month[d2.mon])
{
d2.day=1;
d2.mon++;

if(d2.mon>12)
{
d2.mon=1;
d2.year++;
}
}
}
cout<<"The difference is :";
}

void date :: operator +(int d)
{
for(int i=0;i<d;i++)
{
day++;

if((year%4==0 && mon==2) ? day>29 : day > month[mon])
{
day=1;
mon++;
if(mon>12)
{
mon=1;
year++;
}
}
}
void main()
{

clrscr();
date d1;
date d2;

d1.read();
d2.read();

d1-d2;
int days;
cout<<"ADD date :";
cin>>days;
d2+days;
getch();
}

this is what i tried.... but i got stuck up somewhere... could you please help me resolve this problem

this is wad i could do after working on it for several hours.. but i couldnt really solve it.

please help
thanks in advance

Well, you can "write your own", use code that's already written, or do a combination. A lot has been written. One potential drawback of some of it is that it only works with dates after 1900 or 1970, but it's worth looking at:


http://www.cplusplus.com/reference/clibrary/ctime/ctime.html
http://www.cplusplus.com/reference/clibrary/ctime/tm.html
http://www.cplusplus.com/reference/clibrary/ctime/difftime.html
http://www.boost.org/doc/libs/1_35_0/doc/html/date_time.html

That last link is a Boost library. I haven't much experience with Boost, but it's got a lot of functions that the C++ standard doesn't. A search of the web can also bring up a lot of stuff that other people have written and made public. Food for thought, but assuming that you want to write it all yourself, I would pick some base year and call that year 0. Everything is relative to that. In the struct tm structure, it's the year 1900. In the ctime library, it's 1970. Pick a year/day 0 (might as well be the actual year January 1, 0000. I guess actually there was no year 0, but rather time started at year 1, but I'd make it 0 myself) and have some function that calculates the number of days that have elapsed from that year 0. You'll be using that function a lot.

So I would make this the function that everything revolves around:

int NumDaysElapsed (date aDate);
// returns number of days since January 1, 0000

From that, it's easy to get the number of days between two dates:

int NumDaysElapsed (date date1, date date2);
// returns number of days between date1 and date2

This function will be calculated by calling NumDaysElapsed(date) for date1 and date2, then subtracting.

You'll also need a function that takes the number of days elapsed from January 1, 0000 and returns the date.

date GetDate (int numDaysElapsed);

For the day of the week, you'll need to figure out the day of the week of January 1, 0000, which you can do from knowing today's day of the week and the number of days since January 1, 0000, and doing a modulus 7 operation on that number and perhaps another calculation. If you know the day of the week of any date called date1 and you know the number of days between date1 and date2, you can figure out the day of the week of date2.

Hopefully this'll get you going It all revolves around writing this function. You'll likely need to write some helper functions to write this one.

int NumDaysElapsed (date aDate);
// returns number of days since January 1, 0000

Again, I don't know whether January 1, 0000 actually was a date. If you want to use January 1, 0001 or some other date, that's fine, but the point is to pick a day 0, when "time starts" and have everything revolve around that. Having it be January 1 makes the math easier since there is a guarantee that you won't have to "borrow" when you subtract.

tht was really helpful... thanks...

i also wanted to know if declaring month[12] is ryt???

arrar defining in c++ s from 0-n-1 ryt ??

could u help me with tht aspect ???

tht was really helpful... thanks...

i also wanted to know if declaring month[12] is ryt???

arrar defining in c++ s from 0-n-1 ryt ??

could u help me with tht aspect ???

I assume you mean "right"? Please don't use text-speak. Yes, arrays in C++ start at 0 so if you have an array int month[12] , you want to use array indexes 0 through 11, not 1 through 12, Also, ditto what Salem said on the code tags. It makes it much easier to read.

[code=cplusplus] // paste code here

[/code]

or

[code]

// paste code here

[/code]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.