Post by Avtar on Nov 12, 2007 23:38:51 GMT
Right, so unless you have a C++ compiler, you can't execute the following code. However, the IF ELSE structures and everything are basically similar to most languages, so if you're a PHP Developer or a JavaScript Scripter, this will probably make sense to you and I don't mind if you use excerpts here to make a JS or PHP script.
The program basically asks the user to enter a date, and then generates the date that will occur after the given date. Sounds simple, and it's pretty simple, only thing is that you have to make sure the date is valid. The user can't enter 41st August 2019, and can't enter 29th February 1997 either. Check it out, see if you have some tips you'd like to give me.
The program basically asks the user to enter a date, and then generates the date that will occur after the given date. Sounds simple, and it's pretty simple, only thing is that you have to make sure the date is valid. The user can't enter 41st August 2019, and can't enter 29th February 1997 either. Check it out, see if you have some tips you'd like to give me.
// Program to generate the date occuring after the date entered by the user
#include <iostream.h>
#include <conio.h>
class Date {
int dd,mm,yy;
public:
void getdata();
void operator++();
bool validate();
};
void main()
{
Date d;
d.getdata();
if(d.validate())
++d;
else
cout<<"Invalid date.";
getch();
}
void Date::getdata()
{
cout<<"Enter date : ";cin>>dd;
cout<<"Enter month : ";cin>>mm;
cout<<"Enter year : ";cin>>yy;
}
bool Date::validate()
{
int f=0;
if(dd<=0 || dd>31)
f=1;
else if(mm<=0 || mm>12)
f=1;
else if(yy<=0)
f=1;
else if(mm==2 && dd==30)
f=1;
else if((mm==4 || mm==6 || mm==9 || mm==11) && (dd==31))
f=1;
else if((mm==2) && (dd==29) && (yy%4!=0))
f=1;
else if((mm==2) && (dd==28) && (yy%4==0))
f=1;
if(f==0)
return true;
else
return false;
}
void Date::operator++()
{
if((dd==31) && (mm==12))
dd=1, mm=1, yy++;
else if ((mm==2) && (dd==28 || dd==29))
dd=1, mm=3;
else if ((mm==4 || mm==6 || mm==11 || mm==9) && (dd==30))
dd=1, mm++;
else
dd++;
cout<<"\nNext Date : "<<dd<<"/"<<mm<<"/"<<yy;
}