C++ program to calculate tomorrow’s date

by Vivek Gite · 8 comments

Q. How do I write C++ program that calculate tomorrow’s date? For example user input a date 30/03/2007, then it should print 31/03/2007. Your program must take care of following issues:
=> Leap year
=> Validate date for month, day etc
=> Change of date/month/year should be handled properly.

A:

Source code : tdate.cpp

#include<iostream>
 
#define TRUE 1
#define FALSE 0
 
using namespace std;
 
class mydate{
   int day;
   int month;
   int year;
public:
   void readdate();
   int isLeapYear();
   string month2string();
   int exceedsDaysInMonth();
   void tommorow();
};
 
// Input date from user
void mydate::readdate(){
   cout << "Enter day (dd - 23 ) : ";
   cin >> day;
   cout << "Enter Month (mm - 04 ) : ";
   cin >> month;
   cout << "Enter year (yyyy - 2007 ) : ";
   cin >> year;
}
// make sure month days are correct
// return TRUE if days exceeds in a month
int mydate::exceedsDaysInMonth(){
  int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  if ( month < 1 || month > 12 || day > days[month-1])
     return TRUE;
  else
     return FALSE;
}
// convert numeric month into string
string mydate::month2string(){
   char *months[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
   if ( exceedsDaysInMonth() ) {
    if ( ! (month < 1 || month > 12) )
       return months[month-1];
    else
       return "Unknown month";
   }
   else
    return "Unknown month";
}
// return TRUE if a year is leap
int mydate::isLeapYear(){
   if ( (year % 4)  != 0 ){
      return FALSE;
   }
   else if ( (year % 400)  != 0 ){
      return TRUE;
   }
   else if ( (year % 100)  == 0 ){
      return FALSE;
   }
   else
   {
      return FALSE;
   }
}
// validate and calculate tommorows date
void mydate::tommorow(){
  int td, tm, ty;
  int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  if ( year < 0 ){
       cerr << year << " is not a valid year" << endl;
       exit(1);
  }
  if ( exceedsDaysInMonth() ){
    if ( month == 2 && day == 29 ){
        if ( ! isLeapYear() ){
          cerr << year << " is not a leap year, so Feb doesn't have 29 days" << endl;
          exit(1);
        }
    }
    else
    {
      cerr << "Bad day value month - " << month << " (" << month2string();
      cerr <<  ") doesn't have " << day << " days "<< endl;
      exit(1);
    }
  }
  // calculate tommorow
 td=day;
 tm=month;
 ty=year;
 td++;
 if ( td > days[month-1]){
   td=1;
   tm++;
   if ( tm > 12 )
    { ty++; tm=1; }
 }
 cout << "Valid date, found and tommorow is " <<  td << "/" <<  tm << "/" <<  ty << endl;
}
 
// main
int main(){
  mydate date;
  date.readdate();
  date.tommorow();
  return 0;
}
 

Compile and execute code

To compile code under UNIX/Linux, enter:
$ make tdate
OR
$ g++ -o tdate tdate.cpp

To execute code under Linux/UNIX, enter:
$ ./tdate

A note for TC++ 3.0 (DOS version) users

Replace line
#include<iostream>
With
#include<iostream.h>
Save the file and compile/run code from menu.

Please note that above code should work with another compiler (such as VC++) without modification.

Download source code

Click here to download source code.

{ 8 comments… read them below or add one }

1 sarain August 20, 2008 at 7:06 am

How do I write C++ program that tells the day of entered date? For example user input a date 20/08/2008, then it should print the day(in string)like the day on that date is Wednesday.
As user input any date it tells the current day on that date.(the name of the day should be in character)

Reply

2 milesxhour September 17, 2008 at 5:11 am

I need a function that advances any given date by one day. It is very similar your program. Where is your header file though? Regards!

Reply

3 milesxhour September 17, 2008 at 5:17 am

Nevermind! I’m sleepy!

Reply

4 g pradeepkumar October 25, 2008 at 6:10 pm

hi, i am gpradeepkumar. I like the way u answer the questions asked by the visitors of ur site. So please helpme in creating my own blog and the requirements that should be according to google about the site map generator .How to generate the sitemap , how to upload the sitemap ,how to submit the sitemap and how to add google adsense to my site.please give information about the above things.
I will be very thankful to you

Reply

5 Sankari December 30, 2008 at 2:46 am

How to write a program that displays the day of a given date…

Reply

6 ftlouie March 28, 2009 at 5:05 pm

how do i write a programme to calculate certain formula,but if user’s input are not integers (i.e characters), then a message of ‘invalid data’ displayed??

Reply

7 ram April 13, 2009 at 3:44 pm

Its not working in dos

Reply

8 Sipex May 15, 2009 at 10:11 am

This code has a bug.
tomorrow() function does not check leap year.

also the leap year detection function seems peculiar.

Reply

Leave a Comment

Previous post:

Next post: