469,610 Members | 2,486 Online

# how to add date in date checking c++ program?

i have built a program ,that checks wheteher a date is valid or not.
now if i want to add date,month year (in same input) to a particular date,,how wud the logic work?
it should be like dis :
void add(int d, int m,int y)
Apr 30 '07 #1
7 13657
Ganon11
3,652 Expert 2GB
Well, I'd add the days together. If the resulting days were greater than a normal amount, I'd increment the month and subtract the proper amount from the days. Now that I've incremented the month, I need to check if the month is greater than a normal amount...if so, I'd increment the years and subtract the proper amount from months.

Repeat for months and years.
Apr 30 '07 #2
Well, I'd add the days together. If the resulting days were greater than a normal amount, I'd increment the month and subtract the proper amount from the days. Now that I've incremented the month, I need to check if the month is greater than a normal amount...if so, I'd increment the years and subtract the proper amount from months.

Repeat for months and years.

how wud i check that normal amout for month?
pl\z tell me the actual logic to be put,wheder it wud be switch case,how to put,,or if condition is to be used ?
Apr 30 '07 #3
JosAH
11,448 Expert 8TB
A quite simple alternative is to convert your date to a Julian Day number, add the number
of days to that day number and convert back again.

kind regards,

Jos
Apr 30 '07 #4
Ganon11
3,652 Expert 2GB
Sorry, these questions are things that you should either know on your own or be able to find out easily on your own; I can't do your homework for you.
Apr 30 '07 #5
I don't mean any disrespect, but really... learn to spell properly. Don't you have any idea how difficult it is to read your gibberish on a professional forum? It makes you look too dumb to even know what C++ is.
May 10 '07 #6
Hi Kanushree ,

I hope by now you might have got your answer however if you have not i have recently able to run the program i have created i would like to share it with you please
find below program:
#include<iostream.h>
#include<process.h>
#define TRUE 1
#define FALSE 0
typedef int BOOL;

class TDate
{
private:
int _date;
int _month;
int _year;
public:
TDate()
{
this->_date=1;
this->_month=1;
this->_year=100;
} /*Default Constructor*/

void AcceptDateFromConsole()
{
cout<<"Enter Date:"<<endl;
cin>>this->_date;
cout<<"Enter Month:"<<endl;
cin>>this->_month;
cout<<"Enter Year:"<<endl;
cin>>this->_year;
} /*Function to Accept the date from the user*/

BOOL ValidateDate()
{
int months[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if((this->_year%400==0 && this->_year%100!=0)||(this->_year%4==0))
months[1]=29;
if(!(this->_date>0 && this->_date<=months[_month-1]))
return FALSE;
if(!(this->_month>=1 && this->_month<=12))
return FALSE;
if(!(this->_year>100 && this->_year<9999))
return FALSE;
else
return TRUE;
}/*Function to validate the Date entered */

{
int DaysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool flag;
int theDays;
cout<<"Enter how many days to be added"<<endl;
cin>>theDays;"\n";

if((this->_year%400==0 && this->_year%100!=0)||(this->_year%4==0))
DaysInMonth[1] = 29;
int tempDays =this->_date;
tempDays += theDays;

do
{
flag = false;
if(tempDays>DaysInMonth[this->_month-1])
{
tempDays-=DaysInMonth[this->_month-1];
this->_month++;
flag = true;
}
if(this->_month > 12)
{
this->_month=1;
this->_year++;
}
}
while(flag);
this->_date = tempDays;

}

void returnDate()
{
cout<<"date is "<<this->_date<<"-"<<this->_month<<"-"<<this->_year<<endl;
}

void DisplayDate()
{
cout<<"Date:"<<this->_date<<"-"<<this->_month<<"-"<<this->_year<<endl;
}

{
int choice;
cout<<"1.Accept Date"<<endl;
cout<<"2.Display Date"<<endl;
cout<<"3.Add number of days to the date"<<endl;
cout<<"4.Exit"<<endl;
cin>>choice;
return choice;

}/*Function to generate the Menu List*/

};

int main(void)
{
int choice;
int validate;
TDate d;
{
switch(choice)
{
case 1:

do
{
system("cls");
d.AcceptDateFromConsole();

}while((validate=d.ValidateDate())!=TRUE);

break;
case 2:
system("cls");
d.DisplayDate();
break;
case 3:
system("cls");
d.returnDate();

}
}
return 0;
}
Mar 6 '11 #7
Hi, for this code are all your functions written inside the class or are they written in another file? It's hard to tell. I notice you have two .h files included and I'm curious about what's in those. I'm writing a similar code but I have a .h that includes my class, and two other files, one for functions and one for main. I wanted to play with your code and break it and fix it to see how it works to help me understand classes better. Thanks for any info. :)
Apr 11 '15 #8