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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kannushree
    New Member
    • Apr 2007
    • 5

    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)
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #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.

    Comment

    • kannushree
      New Member
      • Apr 2007
      • 5

      #3
      Originally posted by Ganon11
      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 ?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        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

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          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.

          Comment

          • mjoinsd
            New Member
            • May 2007
            • 1

            #6
            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.

            Comment

            • sherin mathew
              New Member
              • Mar 2011
              • 1

              #7
              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<iostre am.h>
              #include<proces s.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 AcceptDateFromC onsole()
              {
              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,3 1,30,31,31,30,3 1,30,31};
              if((this->_year%400==0 && this->_year%100!=0)| |(this->_year%4==0))
              months[1]=29;
              if(!(this->_date>0 && this->_date<=month s[_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>Day sInMonth[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.Displa y 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.AcceptDateFro mConsole();

              }while((validat e=d.ValidateDat e())!=TRUE);

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


              }
              }
              return 0;
              }

              Comment

              • brosephius
                New Member
                • Apr 2015
                • 1

                #8
                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. :)

                Comment

                Working...