Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Printing the date from starting date to ending date (http://www.go4expert.com/articles/printing-date-starting-date-date-t10112/)

bashamsc 18Apr2008 15:42

Printing the date from starting date to ending date
 

Printing the date from starting date to ending date



The program takes the starting date and ending date as inputs and prints the dates in between them.This program is useful if u r dealing with dates.

The code



I have used four date functions i.e date1() , date2() , date3() , date4(). When the starting year and ending year is same it performs date1() function.If the month is equal to starting month then it performs date2() function.If the month is equal to ending month it performs date3() function.If the year is in between starting and ending year then it performs date4() function.

Code: Cpp

#include<iostream>
using namespace std;

int date1(int , int , int , int , int , int);

int date2(int , int , int , int );

int date3(int , int , int , int );

int date4(int , int );

main()
{
   
    cout<<"Enter the starting date, month and year"<<endl;
   
    int sd,sm,sy,ed,em,ey;
   
    cin>>sd>>sm>>sy;
   
    cout<<"Enter the ending date, month and year"<<endl;
   
    cin>>ed>>em>>ey;
   
    int i,j,k=0,n,m;
   
    n = ey - sy;
   
    m=sm-em;
   
    if(sy>ey)
    {
        cout<<"Starting year cannot be greater than ending year"<<endl;
        exit(1);
    }
    if(n==0&&sm>em)
    {
        cout<<"Date entered is wrong"<<endl;
        exit(1);
    }
   
    if(n==0&&sm>em&&sd>ed)
    {
        cout<<"Date entered is wrong"<<endl;
        exit(1);
    }
   
    if(n==0&&sm==em&&sd>ed)
    {
        cout<<"Date entered is wrong"<<endl;
        exit(1);
    }
   
    if(n==0&&m==0)
    {
        cout<<"Month "<<sm<<" Year "<<sy<<endl;
        for(j=sd;j<=ed;j++)
            cout<<j<<" "<<endl;
        exit(1);
    }
   
    if(n==0&&m==0&&sd==ed)
    {
        cout<<"Month "<<sm<<" Year "<<sy<<endl;
        cout<<j<<" "<<endl;
        exit(1);
    }
   
    if(n==0&&m!=0)
    {
        for(j=sm;j<=em;j++)
        {
            date1(j,sy,sm,em,sd,ed);
        }
    }
   
    else
    {//2
       
        for(i=0;i<=n;i++)
        {//3
           
            if(i==0)
            {
                for(j=sm;j<=12;j++)
                    date2(j,sy,sm,sd);
            }
           
            if(i==n)
            {
                for(j=1;j<=em;j++)
                    date3(j,ey,em,ed);
            }
           
            if(i!=0&&i!=n)
            {
                for(j=1;j<=12;j++)
                    date4(j,k);
            }
           
            k=sy+1;
           
        }//3
       
    }//2
   
}//1

int date1(int a , int sy , int sm , int em , int sd , int ed )
{//
    //cout<<"1"<<endl;
    int i;
    if(a==2&&sy%4==0&&(a==sm||a==em))
    {//1
       
        cout<<"Month  "<<a<<" Year  "<<sy<<endl;
        if(a==sm)
        {
            for(i=sd;i<=29;i++)
                cout<<i<<" ";
            cout<<endl;
        }
       
        if(a==em)
        {
            for(i=1;i<=ed;i++)
                cout<<i<<" ";
            cout<<endl;
        }
       
    }//1
   
    if(a==2&&sy%4==0&&a!=sm&&a!=em)
    {
        cout<<"Month  "<<a<<" Year  "<<sy<<endl;
       
        for(i=1;i<=29;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a==2&&sy%4!=0&&(a==sm||a==em))
    {//2
       
        cout<<"Month  "<<a<<" Year  "<<sy<<endl;
        if(a==sm)
        {
            for(i=sd;i<=28;i++)
                cout<<i<<" ";
            cout<<endl;
        }
       
        if(a==em)
        {
            for(i=1;i<=ed;i++)
                cout<<i<<" ";
            cout<<endl;
        }
       
    }//2
   
    if(a==2&&sy%4!=0&&a!=sm&&a!=em)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=1;i<=28;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a<8&&a!=2&&a%2==0&&(a==sm||a==em))
    {//3
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
        if(a==sm)
        {
            for(i=sd;i<=30;i++)
                cout<<i<<" ";
            cout<<endl;
        }
       
        if(a==em)
        {
            for(i=1;i<=ed;i++)
                cout<<i<<" ";
            cout<<endl;
        }
       
    }//3
   
    if(a<8&&a!=2&&a%2!=0&&(a==sm||a==em))
    {//4
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
        if(a==sm)
        {
            for(i=sd;i<=31;i++)
                cout<<i<<" ";
            cout<<endl;
        }
       
        if(a==em)
        {
            for(i=1;i<=ed;i++)
                cout<<i<<" ";
            cout<<endl;
        }
       
    }//4
   
    if(a<8&&a!=2&&a%2==0&&a!=sm&&a!=em)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
        for(i=1;i<=30;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a<8&&a!=2&&a%2!=0&&a!=sm&&a!=em)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=1;i<=31;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a>=8&&a!=2&&a%2==0&&(a==sm||a==em))
    {//5
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        if(a==sm)
        {
            for(i=sd;i<=31;i++)
                cout<<i<<" ";
            cout<<endl;
        }
       
        if(a==em)
        {
            for(i=1;i<=ed;i++)
                cout<<i<<" ";
            cout<<endl;
        }
       
    }//5
   
    if(a>=8&&a!=2&&a%2!=0&&(a==sm||a==em))
    {//6
       
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
        if(a==sm)
        {
            for(i=sd;i<=30;i++)
                cout<<i<<" ";
            cout<<endl;
        }
       
        if(a==em)
        {
            for(i=1;i<=ed;i++)
                cout<<i<<" ";
            cout<<endl;
        }
       
    }//6
   
    if(a>=8&&a!=2&&a%2==0&&a!=sm&&a!=em)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=1;i<=31;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a>=8&&a!=2&&a%2!=0&&a!=sm&&a!=em)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=1;i<=30;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
}//

int date2(int a , int sy , int sm , int sd )
{//
    //cout<<"2"<<endl;
    int i;
    if(a==2&&sy%4==0&&a==sm)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=sd;i<=29;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a==2&&sy%4==0&&a!=sm)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=1;i<=29;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a==2&&sy%4!=0&&a==sm)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=sd;i<=28;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a==2&&sy%4!=0&&a!=sm)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=1;i<=28;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a<8&&a!=2&&a%2==0&&a==sm)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=sd;i<=30;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a<8&&a!=2&&a%2==0&&a!=sm)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=1;i<=30;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a<8&&a!=2&&a%2!=0&&a==sm)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=sd;i<=31;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a<8&&a!=2&&a%2!=0&&a!=sm)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=1;i<=31;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a>=8&&a!=2&&a%2==0&&a==sm)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=sd;i<=31;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a>=8&&a!=2&&a%2==0&&a!=sm)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=1;i<=31;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a>=8&&a!=2&&a%2!=0&&a==sm)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=sd;i<=30;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a>=8&&a!=2&&a%2!=0&&a!=sm)
    {
        cout<<"Month  "<<a<<"Year  "<<sy<<endl;
       
        for(i=1;i<=30;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
}//

int date3(int a , int ey , int em , int ed )
{//
    //cout<<"3"<<endl;
    int i;
    if(a==2&&ey%4==0&&a==em)
    {
        cout<<"Month  "<<a<<"Year  "<<ey<<endl;
       
        for(i=1;i<=ed;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a==2&&ey%4==0&&a!=em)
    {
        cout<<"Month  "<<a<<"Year  "<<ey<<endl;
       
        for(i=1;i<=29;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a==2&&ey%4!=0&&a==em)
    {
        cout<<"Month  "<<a<<"Year  "<<ey<<endl;
       
        for(i=1;i<=ed;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a==2&&ey%4!=0&&a!=em)
    {
        cout<<"Month  "<<a<<"Year  "<<ey<<endl;
       
        for(i=1;i<=28;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a<8&&a!=2&&a%2==0&&a==em)
    {
        cout<<"Month  "<<a<<"Year  "<<ey<<endl;
       
        for(i=1;i<=ed;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a<8&&a!=2&&a%2==0&&a!=em)
    {
        cout<<"Month  "<<a<<"Year  "<<ey<<endl;
       
        for(i=1;i<=30;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a<8&&a!=2&&a%2!=0&&a==em)
    {
        cout<<"Month  "<<a<<"Year  "<<ey<<endl;
       
        for(i=1;i<=ed;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a<8&&a!=2&&a%2!=0&&a!=em)
    {
        cout<<"Month  "<<a<<"Year  "<<ey<<endl;
       
        for(i=1;i<=31;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a>=8&&a!=2&&a%2==0&&a==em)
    {
        cout<<"Month  "<<a<<"Year  "<<ey<<endl;
       
        for(i=1;i<=ed;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a>=8&&a!=2&&a%2==0&&a!=em)
    {
        cout<<"Month  "<<a<<"Year  "<<ey<<endl;
       
        for(i=1;i<=31;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a>=8&&a!=2&&a%2!=0&&a==em)
    {
        cout<<"Month  "<<a<<"Year  "<<ey<<endl;
       
        for(i=1;i<=ed;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a>=8&&a!=2&&a%2!=0&&a!=em)
    {
        cout<<"Month  "<<a<<"Year  "<<ey<<endl;
       
        for(i=1;i<=30;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
}

int date4(int a , int b )
{//
    //cout<<"4"<<endl;
    int i;
    if(a==2&&b%4==0)
    {
        cout<<"Month  "<<a<<"Year  "<<b<<endl;
       
        for(i=1;i<=29;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a==2&&b%4!=0)
    {
        cout<<"Month  "<<a<<"Year  "<<b<<endl;
       
        for(i=1;i<=28;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a<8&&a!=2&&a%2==0)
    {
        cout<<"Month  "<<a<<"Year  "<<b<<endl;
       
        for(i=1;i<=30;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a<8&&a!=2&&a%2!=0)
    {
        cout<<"Month  "<<a<<"Year  "<<b<<endl;
       
        for(i=1;i<=31;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a>=8&&a!=2&&a%2==0)
    {
        cout<<"Month  "<<a<<"Year  "<<b<<endl;
       
        for(i=1;i<=31;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
    if(a>=8&&a!=2&&a%2!=0)
    {
        cout<<"Month  "<<a<<"Year  "<<b<<endl;
       
        for(i=1;i<=30;i++)
            cout<<i<<" ";
        cout<<endl;
    }
   
}//
 

After entering the starting date and ending date the program checks whether the dates are valid or not.

GAURAV SABOO 21Apr2008 12:39

Re: Printing the date from starting date to ending date
 
cant i do it in some easy way.if yas then do it ina easy way

shabbir 1May2008 13:38

Re: Printing the date from starting date to ending date
 
Nomination for article of the month of April

GAURAV SABOO 1May2008 22:26

Re: Printing the date from starting date to ending date
 
no response have been given from your side
please do it in an easy wayin c.its urgent.i am trying this but i cant do it.so please do it and post it to my email id

shabbir 2May2008 13:22

Re: Printing the date from starting date to ending date
 
Quote:

Originally Posted by GAURAV SABOO
no response have been given from your side
please do it in an easy wayin c.its urgent.i am trying this but i cant do it.so please do it and post it to my email id

Can you please be specific as to what you mean by easy because what could be easy for you could be tough for me and its all relative.

GAURAV SABOO 3May2008 23:22

Re: Printing the date from starting date to ending date
 
as you have done it in c++ language and i only know c language so i cant understand that solution.so if you will do the solution of that problem iin c language then i will be able to understand that.

imrantechi 5May2008 14:09

Re: Printing the date from starting date to ending date
 
Quote:

Originally Posted by GAURAV SABOO
as you have done it in c++ language and i only know c language so i cant understand that solution.so if you will do the solution of that problem iin c language then i will be able to understand that.


Whatever in post.. It's only in C. Can you read article once more time? there is no code of c++ in this article.
Please atleast read article and then comment.

bashamsc 12May2008 13:07

Re: Printing the date from starting date to ending date
 
Hi gaurav i have written another method for printing the date check it.

u can implement this program in c also.First read whole article and understand logic behind the program and try to implement ur own logic for that.

shabbir 24May2008 08:51

Re: Printing the date from starting date to ending date
 
Voting for article of the month for Apr 2008


All times are GMT +5.5. The time now is 03:16.