User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,225 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,233 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 208 | Replies: 7
Reply
Join Date: Jul 2008
Posts: 4
Reputation: dashisneo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dashisneo dashisneo is offline Offline
Newbie Poster

need help with this.

  #1  
Jul 25th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,333
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: need help with this.

  #2  
Jul 25th, 2008
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.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Jul 2008
Posts: 4
Reputation: dashisneo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dashisneo dashisneo is offline Offline
Newbie Poster

Re: need help with this.

  #3  
Jul 25th, 2008
#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();
}
Last edited by Tekmaven : Jul 25th, 2008 at 1:04 pm. Reason: Code tags
Reply With Quote  
Join Date: Jul 2008
Posts: 4
Reputation: dashisneo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dashisneo dashisneo is offline Offline
Newbie Poster

Re: need help with this.

  #4  
Jul 25th, 2008
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
Reply With Quote  
Join Date: Dec 2005
Posts: 3,639
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 22
Solved Threads: 418
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: need help with this.

  #5  
Jul 25th, 2008
Maybe, but you'll need to figure out code tags first/
Try reading some forum stickies.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Jan 2008
Posts: 1,776
Reputation: VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice 
Rep Power: 8
Solved Threads: 219
VernonDozier VernonDozier is offline Offline
Posting Virtuoso

Re: need help with this.

  #6  
Jul 25th, 2008
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/c...ime/ctime.html
http://www.cplusplus.com/reference/c.../ctime/tm.html
http://www.cplusplus.com/reference/c.../difftime.html
http://www.boost.org/doc/libs/1_35_0...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.
Reply With Quote  
Join Date: Jul 2008
Posts: 4
Reputation: dashisneo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dashisneo dashisneo is offline Offline
Newbie Poster

Re: need help with this.

  #7  
Jul 25th, 2008
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 ???
Reply With Quote  
Join Date: Jan 2008
Posts: 1,776
Reputation: VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice 
Rep Power: 8
Solved Threads: 219
VernonDozier VernonDozier is offline Offline
Posting Virtuoso

Re: need help with this.

  #8  
Jul 25th, 2008
Originally Posted by dashisneo View Post
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]
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:34 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC