By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,693 Members | 1,936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,693 IT Pros & Developers. It's quick & easy.

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

P: 5
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
Share this Question
Share on Google+
7 Replies


Ganon11
Expert 2.5K+
P: 3,652
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

P: 5
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

Expert 10K+
P: 11,448
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
Expert 2.5K+
P: 3,652
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

P: 1
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

P: 1
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 */


void addDay()
{
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 Menu_List()
{
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;
cout<<"Enter your choice"<<endl;
cin>>choice;
return choice;

}/*Function to generate the Menu List*/




};

int main(void)
{
int choice;
int validate;
TDate d;
while((choice=d.Menu_List())!=4)
{
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.addDay();
d.returnDate();


}
}
return 0;
}
Mar 6 '11 #7

P: 1
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

Post your reply

Sign in to post your reply or Sign up for a free account.