Wednesday, November 12, 2008

C++ Date() Class

Our teacher gave us a quiz today which is to improve our previous program, the date class. The concept of this program is to input the number of day, the month, and the year. For example, your input for days = 30, month = 1, and year = 2008. The output will be 1/30/2008. So we need to improve our previous code into a much better program. We need to add functions that can check if the input of number of days are acceptable in the month. For example, the month of April has a maximum of 30 days, if I the input is 31, the function will able to know that input in number of days is wrong. We also add a function that can able to know if the input year is a leap year or not.

Here are some of my codes:
//this funtion will help you to know if the number of days and the month are acceptable
int checkDate()
{
int n;
//this code is for all the months w/ 31 days
if(month==1||month==3||month==4.........................................)
{ if(days<=31){ n=1;} else n=0; }
................................
...............................
...............................
................................
//you will get an idea for all the months w/ 30 days above
if(month==2)//for february
{
if(leapyear()==1)
{
if(days<=29)
n=1;
else n=0;
............................
............................
}
return n;
}
//this function will help you know if the input year is a leapyear
int leapyear()
{
int n;
if(year%4==0)
n=1;
else
n=0;

return n;
}

string getDate()
{
........................
.......................
.......................
char ambot[20]="1\1\1990";
if(checkDate()==1)//if the input days and month are acceptable, statements below will execute
{
...................................
.....................................
....................................
.....................................
so on..................

return "variable";
}
else //if the inputs are not acceptable, the 1\1\1990 will be the value for setDate()
return ambot;
}

Hope you get an idea from that. If you have a better idea than this, you can do what you want. It is a matter of style. ^^ If there are any question about the code or if I'm wrong, feel free to post comments.

2 comments:

Anonymous said...

hey dis is a quite simplified version
i want to make a program wich is able to do othr things like increament date,diference between two dates(using the date class itself as a parameter 4 dis function),to calculate da days of da year passed n da no weeks passed

can u help me in dis regard as soon as possible

Anonymous said...

it takes time to do what u want...