A C++ program to find month,date and day after 50 days from the current date.


The aim of this program is to find the exact date after 50 days from the current date.First we have structure name Date that store the current date and month.We also have a function name void date_after50days( Date &dat ) that accept a reference to Date structure.The purpose of this function is to find the month and date after 50 days from the current date.The function definition is shown below.The explanation is given after the function definition.
 

void date_after50days( Date &dat )
{
int RemainingDays ;

///checking if the current month has 31 days
if( (( dat.month >= 8 ) && ( dat.month%2 ==0 )) || ( ( dat.month<8 ) && ( dat.month%2 !=0 ) ) )
{
 RemainingDays=50-(31-dat.day) ; //the days that remain after the current month
 if( dat.month==7 )  ///it’s July
 {
  ++dat.month ; ///month is incremented

  if( RemainingDays>31 )
  {
  RemainingDays=RemainingDays-31 ;
  ++dat.month ; ///the month is incremented after skipping 31 days
  }
 }
 else
 {
  ++dat.month ;
  if( RemainingDays>30 )
  {
  ++dat.month ;
  RemainingDays=RemainingDays-30 ; ///the next month will have 30 days so subtract 30
  }
  }
 }
else   ///if the current month has only 30 days
{
 RemainingDays=50-( 30-dat.day ) ;
 ++dat.month ;
 if(RemainingDays>31 ) ///the next month will have 31 days
 {
  ++dat.month ;
  RemainingDays=RemainingDays-31 ;
 }
}

 dat.date=RemainingDays ;   //the date after 50 days is stored in dat.date
}

 
First of all the function contains a variable name RemainingDays ,this variable will store how many days remains after skipping every month.If the RemainingDays is still more than 30 or 31 days after counting every month we will subtract 30 or 31 depending on how many days the consecutive month have.At last when it’s value becomes less than 30 that value is the required date after 50 days.

We also divide the function into two parts :
i)If the current month is has 31 days.
ii)If the current month has only 30 days.

We require another condition for the first part.If the current moth is July the next month(August) also has 31 days so we should subtract 31 not 30 from RemainingDays if the RemainingDays value is more than 31.But for other case the subtraction should be made by 30 because after every 31 days we have 30 days and again 31 days and so on.



The main() function uses some functions from <ctime> library to find the exact month and day of the date after 50 days.I recommend that you check up Cplucplus website to known the purpose of the functions used.The complete program to find the month,date and day is given below.
 

#include <iostream>
#include <ctime>   /// important!

using namespace std ;

struct Date
{
int date ,
month ;
} ;

void date_after50days( Date &dat )
{
int RemainingDays=0 ;

///checking if the current month has 31 days
if( (( dat.month >= 8 ) && ( dat.month%2 ==0 )) || ( ( dat.month<8 ) && ( dat.month%2 !=0 ) ) )
{
 RemainingDays=50-(31-dat.date) ;   //the days that remain after this month
 if( dat.month==7 ) ///it’s July
 {
  ++dat.month ; ///month is incremented

  if( RemainingDays>31 )
  {
  RemainingDays=RemainingDays-31 ;
  ++dat.month ; ///the month is incremented after skipping 31 days
  }
 }
 else
 {
  ++dat.month ;
  if( RemainingDays>30 )
  {
  ++dat.month ;
  RemainingDays=RemainingDays-30 ; ///the next month will have 30 days so subtract 30
  }
  }
 }
else   ///if the current month has only 30 days
{
 RemainingDays=50-( 30-dat.date ) ;
 ++dat.month ;
 if(RemainingDays>31 ) ///the next month will have 31 days
 {
  ++dat.month ;
  RemainingDays=RemainingDays-31 ;
 }
}

 dat.date=RemainingDays ;   //the date after 50 days is stored in dat.date
}

int main( )
{
Date dat ;
string day[]={“Sunday” , “Monday” , “Tuesday” , “Wednesday” ,
“Thursday” , “Friday” , “Saturday” } ,

month[]={ “Jan” , “Feb” , “March” , “April” , “May” , “June”,
“July” , “Aug” , “Sept” , “Oct” , “Nov” , “Dec” } ;

time_t rawtime ;
struct tm *timeinfo ;

time( &rawtime ) ;
timeinfo=localtime( &rawtime ) ;

cout<< “Enter date::” ; cin>> dat.date ;
cout<< “\nEnter month(in number)::” ; cin >> dat.month ;
cin.ignore( ) ;

date_after50days(dat) ;

timeinfo->tm_mon=dat.month-1 ;
timeinfo->tm_mday=dat.date ;

mktime(timeinfo) ;

cout<<“\n\nThe month,date and day after 50 days is \n\n” ;

cout<< ” The month is ::” << month[timeinfo->tm_mon] << endl
  << ” The date is::” << dat.date << endl
  << ” The day is ::” << day[timeinfo->tm_wday] ;

cin.get( ) ;
return 0 ;
}

 
The output screen shot of the program is shown below.
 

 


Related Link

->C++ program to add , subtract , multiply and divide two complex numbers.