Tuesday 4 March 2014

Days Between Two Days Calculator - C++ Program Source Code

Hey there! Here is an awesome program which outputs the number of days between two given dates! This program is particularly useful for students like me, who wants to calculate duration to the day of examination. No time for working now, but I will update it later!
Code:

#include<iostream>
#include "process.h";
using namespace std;
int main()
{

      int days_in_months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
      int day1, day2;
      int month1, month2;
      int year1, year2;
      int y_diff, d_diff;
      int tot_mon;
      int years = 365;
    
      cout<<"Program to calculate how many days are in between the day/month/year entered"<<endl;
      cout<<endl;

  start:
      cout<<"Please enter the date by day, month, year"<<endl;
      cout<<endl;

      cout<<"First date : "<<endl;
      cout<<endl;

      cout<<"Day : ";
      cin>>day1;
      if(day1 > 31 || day1 <= 0)
      {
          cout<<"Incorrect day entered"<<endl;
          cin.ignore();
          return 0;
      }
      cout<<"Month: ";
      cin>>month1;
      if(month1 > 12 || month1 <= 0)
      {
          cout<<"Incorrect Month entered"<<endl;
          cin.ignore();
          return 0;
      }
      cout<<"Year: ";
      cin>>year1;
      if(year1 > 9999 || year1 < 0)
      {
          cout<<"Incorrect Year Entered"<<endl;
          cin.ignore();
          return 0;
      }
 
      cout<<endl;
      cout<<"\nSecond date:: "<<endl;
      cout<<endl;

      cout<<"Day: ";
      cin>>day2;
      if(day2 > 31 || day2 <= 0)
      {
          cout<<"Incorrect day entered"<<endl;
          cin.ignore();
          return 0;
      }
      cout<<"Month: ";
      cin>>month2;
      if(month2 > 12 || month2 <= 0)
      {
          cout<<"Incorrect Month entered"<<endl;
          cin.ignore();
          return 0;
      }
      cout<<"Year: ";
      cin>>year2;
      if(year2 > 9999 || year2 < 0)
      {
          cout<<"Incorrect Year Entered"<<endl;
          cin.ignore();
          return 0;
      }
 
      if(year1 == year2)
      {
          y_diff = 0;
      }
      else
      {
         if(year1 % 4 == 0 && year1 % 100 != 0 || year1 % 400 == 0)
         {
             if(year2 % 4 == 0 && year2 % 100 != 0 || year2 % 400 == 0)
             {
                if(year1 > year2)
                {
                     y_diff = (year1 - year2) * (years) + 2;
                }
                else
                {
                     y_diff = (year2 - year1) * (years) + 2;
                }
                if(month2 > month1)
                {
                    if(days_in_months[month1 - 1] > days_in_months[1])
                    {
                        --y_diff;
                    }
                }
             }
             else
             {
                if(year1 > year2)
                {
                    y_diff = (year1 - year2) * (years) + 1;
                }
                else
                {
                    y_diff = (year2 - year1) * (years) + 1;
                }
                if(month1 > month2)
                {
                    if(days_in_months[month2 - 1] > days_in_months[1])
                    {
                        --y_diff;
                    }
                }
             }
         }
         else
         {
            if(year1 > year2)
            {
                y_diff = (year1 - year2) * (years);
            }
            else
            {
                  y_diff = (year2 - year1) * (years);
            }
         }
     }
 
     if(month1 == month2)
     {
         tot_mon = 0;
     }
     else
     {
         if(month1 > month2)
         {
             for(int i = (month1 - 1); i > (month2 - 1); i--)
             {
                  static int tot_mon_temp = 0;
                  tot_mon_temp += days_in_months[i];
                  tot_mon = tot_mon_temp;
             }
         }
         else
         {
             for(int i = (month1 - 1); i < (month2 - 1); i++)
             {
                  static int tot_mon_temp = 0;
                  tot_mon_temp += days_in_months[i];
                  tot_mon = tot_mon_temp;
             }
         }
     }

     int days_total;

     if (day1 == day2)
     {
        d_diff = 0;
        days_total = (y_diff + tot_mon) - d_diff;
     }
     else
     {
        if(day1 > day2)
        {
            d_diff = day1 - day2;
            days_total = (y_diff + tot_mon) - d_diff;
        }
        else
        {
            d_diff = day2 - day1;
            days_total = (y_diff + tot_mon) + d_diff;
        } 
     }

     if(year1 == year2)
     {
     }
     else
     {
        if(year1 > year2)
        {
           for(int i = (year2 + 1); i < year1; i++)
           {
               if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
               {
                  cout<<endl;
                  cout<<i<<endl;
                  ++days_total;
               }
           }
       }
       else
       {
           for(int i = (year1 + 1); i < year2; i++)
           {
                if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
                {
                     cout<<endl;
                     cout<<i<<endl;
                     ++days_total;
                }
           }
       }
  }

 here:
     cout<<"\nInclude the end date? (Y/N) : ";
     char ch;
     cin>>ch;
     if(ch=='Y'||ch=='y')
     {
          cout<<endl;
          cout<<"\nThe total days in between your dates are: "<<days_total<<endl;
          cout<<endl;
     }
     else if(ch=='N'||ch=='n')
     {
         cout<<endl;
         cout<<"\nThe total days in between your dates are: "<<(days_total-1)<<endl;
         cout<<endl;
     }
     else
     {
         cout<<"Invalid choice, try again!";
         goto here;
     }
 here2:
     cout<<"\n\nCheck with another pair of dates? (Y/N)"<<endl;
     char ch2;
     cin>>ch2;
     if(ch2=='Y'||ch2=='y')
     {
         goto start;
     }
     else if(ch2=='N'||ch2=='n')
     {
         exit(0);
     }
     else
     {
          cout<<"Invalid choice, try again!";
          goto here2;
     }
     cin.get();
     cin.ignore();
     return 0;
}


Happy Coding!
:)

No comments:

Post a Comment

You might also like