Please Help me with this Date Operation Program. I think that this works....yet!
Thanks

#include <iostream.h>
#include <conio.h>

int a[7]={1,3,5,7,8,10,12};
int b[7]={4,6,9,11};

class manipdate
{
    int date;
    int month;
    int year;

public:

    int d,m,y;

    void getdata()
    {
        cout<<"\n\n Date  : ";
        cin>>date;
        cout<<"\n Month : ";
        cin>>month;
        cout<<"\n Year  : ";
        cin>>year;
    }

    void display(int d, int m, int y)
    {
        cout<<" "<<d<<" / "<<m<<" / "<<y;
    }

    manipdate sub(manipdate q)
    {
        if(q.d < 0)
        {
            q.d = -q.d;
            if(q.d>30)
            {
                for(int i=0;i<7;i++)
                {
                    if(q.m==a[i])
                    {
                        if(q.d>31)
                        {
                            q.d -= 31;
                            q.m--;
                        }
                    }
                    else if(q.m==b[i])
                    {
                        q.d -= 30;
                        q.m++;
                    }
                }
            }
        }

        if(q.m < 0)
        {
            q.m = -q.m;         
            while(q.m > 12)
            {
                q.m -= 12;
                q.y--;
            }
        }

        return q;
    }

    manipdate operator +(manipdate &x)
    {
        manipdate q;
        q.d=date+x.date;
        q.m=month+x.month;
        q.y=year+x.year;
        while(q.m>12)
        {
            q.m -= 12;
            q.y++;
        }

        if(q.m == 2)
        {
            if(q.y%4==0)
            {
                if(q.d>29)
                    q.d -= 29;
            }
        }

        if(q.d>30)
        {
            for(int i=0;i<7;i++)
            {
                if(q.m==a[i])
                {
                    if(q.d>31)
                    {
                        q.d-=31;
                        q.m++;
                    }
                }
                else if(q.m==b[i])
                {
                    q.d-=30;
                    q.m++;
                }
            }
        }

        while(q.m>12)
        {
            q.m-=12;
            q.y++;
        }

        display(q.d,q.m,q.y);
        return (q);
    }

    manipdate operator -(manipdate &x)
    {
        manipdate q;

        if(year > x.year)
        {
            q.y = year - x.year;
            q.m = month - x.month;
            q.d = date - x.date;
            q=sub(q);
        }

        else if(x.year > year)
        {
            q.y = x.year - year;
            q.m = x.month - month;
            q.d = x.date - date;
            q=sub(q);
        }

        else if(x.year = year)
        {
            q.y = 0;
            if(month > x.month)
            {
                q.m = month - x.month;
                q.d = date - x.date;
                q=sub(q);
            }

            else if(month < x.month)
            {
                q.m = x.month - month;
                q.d = x.date - date;
                q=sub(q);
            }

            else
            {
                q.y = 0;
                q.m = 0;

                if(date > x.date)
                {
                    q.d = date - x.date;
                    q=sub(q);
                }

                else if(date < x.date)
                {
                    q.d = x.date - date;
                    q=sub(q);
                }

                else
                {
                    q.d = 0;
                    q=sub(q);
                }
            }
        }

        display(q.d,q.m,q.y);
        return q;
    }
};

void main()
{
    clrscr();
    manipdate d1,d2;
    cout<<"\n Enter the Date\n";
    d1.getdata();
    cout<<"\n Enter the Date\n";
    d2.getdata();
    cout<<"\n\n The Addition of the Dates    : \n\n";
    d1+d2;
    cout<<"\n\n The Subtraction of the Dates : \n\n";
    d1-d2;
    getch();
}

I created the above Program using Turbo C++. Hence I haven't used namespace.

What do you want from us?

As AD says, "What do you want from us?" - you just say you want help, but with what? For whatever it's worth (FWIW), date manipulation classes should use Julian dates (integral or floating-point numbers representing the date/date+time - no years, months, days). Then, date arithmetic is simple, and the same equations are used to convert the julian value to the current year+month+day (+ optionally time). This is how it is done for professionally written programs. Here is the header for a Julian date class that I wrote some time ago:

    class Date
    {

    private:

        uint32_t m_JulianDate;

    public:

        /**
         * Date constructed from a system time value. Since these are trivially
         * created from struct tm objects using the system ::mktime() function,
         * we don't need a constructor for that structure type. Use of a
         * default argument makes this the default constructor.
         */
        Date( time_t ctm = ::time(0) );

        /**
         * Create date object with specified year, month, and day where
         * the date is in the Gregorian calendar from 15 October 1582 on.
         * The 32bit unsigned integer value will store dates until sometime
         * after the year 5,878,800.
         */
        Date(int year, int month, int day);

        /**
         * Date constructed from string. The 'fmt' argument specifies how the string is to
         * be decoded, using the same tokens as is used for output by the asString()
         * method and the system strftime() function.
         */
        Date(const char* dateStr, const char* fmt = "%Y%m%d");

        /**
         * Date constructed from a julian date value.
         */
        Date( uint32_t jdate );

        /**
         * Copy constructor.
         */
        Date(const Date& cpy);
        virtual ~Date() throw (LiveObjectError);
        Date& operator=(const Date& rhs);

        /**
         * Date arithmetic operators. Note there is no int operator+( const Date& )
         * method as adding 2 dates is nonsensical. Subtraction (diff) is another
         * matter, however.
         */
        Date operator+( int rhs ) const;
        Date operator-( int rhs ) const;
        int operator-( const Date& rhs ) const;
        Date& operator+=( int rhs );
        Date& operator-=( int rhs );

        /**
         * Date comparison operators.
         */
        bool operator==( const Date& rhs ) const { return m_JulianDate == rhs.m_JulianDate; }
        bool operator!=( const Date& rhs ) const { return m_JulianDate != rhs.m_JulianDate; }
        bool operator<( const Date& rhs ) const { return m_JulianDate < rhs.m_JulianDate; }
        bool operator<=( const Date& rhs ) const { return m_JulianDate <= rhs.m_JulianDate; }
        bool operator>( const Date& rhs ) const { return m_JulianDate > rhs.m_JulianDate; }
        bool operator>=( const Date& rhs ) const { return m_JulianDate >= rhs.m_JulianDate; }

        void setDate( time_t newVal, bool usegmt = false );
        void setDate( const char* newVal, const char* fmt );
        void setDate( int year, int month, int day );

        int year() const;
        int month() const;
        int day() const;
        int dayOfWeek() const;
        bool isLeapYear() const;

        /**
         * getDate() - sets year, month, day, and returns dayOfYear.
         */
        int getDate( int& y, int& m, int& d ) const;

        uint32_t julian() const { return m_JulianDate; }

        /**
         * asString() - returns string formatted as using format characters
         * specified by strftime. The default returns string formatted as YYYYMMDD.
         */
        std::string asString(const char* fmt = "%Y%m%d") const;

        /**
         * Validate the date. Returns false if not valid.
         */
        bool isValid() const;

        static const char* nameOfDay( int d, bool abbrev = false );
        static const char* nameOfMonth( int m, bool abbrev = false );
        static uint32_t daysBetween( const Date& lhs, const Date& rhs );

    };

Here is some of the implementation (the rest is left to you as an exercise):

static const uint32_t s_secondsInDay = (24 * 3600);
static const char* s_unknown = "UNKNOWN";
static const char* s_invalid = "INVALID";

static const char* mabbrev[] = { "Jan", "Feb", "Mar",
                                 "Apr", "May", "Jun",
                                 "Jul", "Aug", "Sep",
                                 "Oct", "Nov", "Dec" };

static const char* mnames[] = { "January", "February", "March",
                                "April",   "May",      "June",
                                "July",    "August",   "September",
                                "October", "November", "December" };

static const char* dabbrev[] = { "Sun", "Mon", "Tue", "Wed",
                                 "Thu", "Fri", "Sat" };

static const char* dnames[] = { "Sunday", "Monday",   "Tuesday", "Wednesday",
                                "Thursday", "Friday",  "Saturday" };
/*
 * SDateToJulian() - computes the julian decimal date from
 * the calendar date.
 *
 * Output: the julian date as a 32bit unsigned integer value.
 *
 * This algorithm was derived from the Wikipedia entry "Julian day" at
 * http://en.wikipedia.org/wiki/Julian_date and verified
 * against results of open source julian date computation package
 * "jday" version 2.4 from SourceForge.net. The jday package
 * utilizes extensive floating point calculations, whereas the
 * algorithm described by the Wikipedia, and as I have implemented
 * here, utilize much simpler and more efficient integer computations.
 *
 * Important Note: this package does not properly deal with dates before
 * the start of the Gregorian calendar which began October 15, 1582.
 *
 * bboyle - 11/21/2006.
 *
 * Returns julian date on success, UINT_MAX on error.
 */
static uint32_t SDateToJulian( int year, int month, int day )
{
    uint32_t retval = UINT_MAX;
    if (month > 0 && month <= 12 && year > 1582 && day > 0 && day <= 31)
    {
        int a = (14 - month)/12;
            int y = (year + 4800 - a);
        int m = (month + (12*a) - 3);
            retval = (uint32_t) (day + (((153*m)+2)/5) + (365*y) + (y/4) - (y/100) + (y/400) - 32045);
    }
    return retval;
}

/*
 * SJulianToDate() - derives the year, month, day, day-of-week,
 * and day-of-year from a julian date and stores them in the
 * struct tm object provided by the caller.
 *
 * Note: it will not convert the julian date if its value is UINT_MAX.
 */
static void SJulianToDate( uint32_t jd, int& year, int& month, int& day )
{
    year = month = day = INT_MIN;
    if (jd != UINT_MAX)
    {
        uint32_t j = jd + 32044;
        uint32_t g = j / 146097;
        uint32_t dg = j % 146097;
        uint32_t c = (((dg / 36524) + 1) * 3) / 4;
        uint32_t dc = dg - (c * 36524);
        uint32_t b = dc / 1461;
        uint32_t db = dc % 1461;
        uint32_t a = (((db / 365) + 1) * 3) / 4;
        uint32_t da = db - (a * 365);
        uint32_t y = (g * 400) + (c * 100) + (b * 4) + a;
        uint32_t m = (((da * 5) + 308) / 153 ) - 2;
        uint32_t d = da - (((m + 4) * 153) / 5) + 122;
        uint32_t Y = y - 4800 + ((m + 2) / 12);
        uint32_t M = ((m + 2) % 12) + 1;
        uint32_t D = d + 1;

        year = (int)Y;
        month = (int)M;
        day = (int)D;
    }
}

static void SJulianToDate( uint32_t jd, struct tm* ptm )
{
    if (ptm != 0)
    {
        int year, month, day;
        SJulianToDate(jd, year, month, day);

        if (year != INT_MIN && month != INT_MIN && day != INT_MIN)
        {
            int dw = (jd % 7) + 1;
            uint32_t ny = SDateToJulian(year, 1, 1);

            ptm->tm_year = year - 1900;
            ptm->tm_mon = month - 1;
            ptm->tm_mday = day;
            ptm->tm_wday = (dw < 7) ? dw : 0;

            // Compute day-of-year.
            ptm->tm_yday = (jd - ny);
        }
    }
}

/**
 * Default constructor creates the object with today's date unless
 * specified otherwise.
 */
Date::Date( time_t ctm )
    : m_JulianDate(UINT_MAX)
{
    // Set date from system time here.
    setDate( ctm );
}

/**
 * Create date object with specified year, month, and day.
 */
Date::Date(int year, int month, int day)
    : m_JulianDate(SDateToJulian(year, month, day))
{
}


/**
 * Date constructed from string. The 'fmt' argument specifies how the string is to
 * be decoded, using the same tokens as is used for output by the asString()
 * method and the system strftime() function.
 */
Date::Date(const char* dateStr, const char* fmt)
    : m_JulianDate(UINT_MAX)
{
    setDate(dateStr, fmt);
}

Date::Date( uint32_t jdate )
    : m_JulianDate(jdate)
{
}

/**
 * Default constructor creates the object with today's date.
 */
Date::Date(const Date& cpy)
    : m_JulianDate(cpy.m_JulianDate)
{

}

Date::~Date() throw (LiveObjectError)
{

}

Date& Date::operator=(const Date& rhs)
{
    if (this != &rhs)
    {
        m_JulianDate = rhs.m_JulianDate;
    }
    return *this;
}

void Date::setDate( time_t newVal, bool usegmt )
{
    struct tm  theTime;
    struct tm* ptm = 0;

    ::memset((void*)&theTime, 0, sizeof(theTime));
    if (usegmt)
    {
        ptm = gmtime_r(&newVal, &theTime);
    }
    else
    {
        ptm = localtime_r(&newVal, &theTime);
    }
    if (ptm)
    {
        m_JulianDate = SDateToJulian(ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);
    }
}

void Date::setDate(const char* newVal, const char* fmt)
{
    struct tm  theTime;
    struct tm* ptm = 0;

    ::memset((void*)&theTime, 0, sizeof(theTime));

    try
    {
        ptm = getDateFromString(newVal, fmt, &theTime);
        if (ptm)
        {
            m_JulianDate = SDateToJulian(ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);
        }
    }
    catch (InvalidDateError err)
    {
        m_JulianDate = UINT_MAX;
        TRACE_ERROR(err, Date, setDate);
    }
}

void Date::setDate( int year, int month, int day )
{
    m_JulianDate = SDateToJulian(year, month, day);
}

/**
 * Returns string formatted as using format characters specified by strftime. The
 * default returns string formatted as YYYYMMDD.
 */
std::string Date::asString(const char* fmt) const 
{
    std::string retval( "INVALID_DATE" );

    if (isValid())
    {
        size_t maxlen = PATH_MAX + ((fmt) ? ::strlen(fmt) : 0);
        char buffer[maxlen + 1];
        struct tm theDate;

        ::memset((void*)&theDate, 0, sizeof(theDate));
        SJulianToDate(m_JulianDate, &theDate);
        if ( ::strftime(buffer, maxlen, fmt, &theDate) > 0 )
        {
            retval = (const char*)buffer;
        }
    }
    return retval;
}


/**
 * Validate the date. Returns false if not valid.
 */
bool Date::isValid() const 
{
    return (m_JulianDate != UINT_MAX);
}

int Date::year() const
{
    int year, month, day;
    SJulianToDate(m_JulianDate, year, month, day);
    return year;
}

int Date::month() const
{
    int year, month, day;
    SJulianToDate(m_JulianDate, year, month, day);
    return month;
}

int Date::day() const
{
    int year, month, day;
    SJulianToDate(m_JulianDate, year, month, day);
    return day;
}

int Date::dayOfWeek() const
{
    int retval = INT_MIN;
    if (isValid())
    {
        int dw = (m_JulianDate % 7) + 1;
        retval = (dw == 7) ? 0 : dw;
    }
    return retval;
}

int Date::getDate( int& y, int& m, int& d ) const
{
    SJulianToDate(m_JulianDate, y, m, d);
    return (int)(m_JulianDate - SDateToJulian(y, 1, 1));
}

bool Date::isLeapYear() const
{
    bool retval = false;
    if (isValid())
    {
        int yr = year();
        retval = ((yr % 4) == 0 && ((yr % 100) != 0 || (yr % 400) == 0));
    }
    return retval;
}

Date Date::operator+( int rhs ) const
{
    Date retval = *this;
    retval.m_JulianDate += rhs;
    return retval;
}

Date Date::operator-( int rhs ) const
{
    Date retval = *this;
    retval.m_JulianDate -= rhs;
    return retval;
}

int Date::operator-( const Date& rhs ) const
{
    return m_JulianDate - rhs.m_JulianDate;
}

Date& Date::operator+=( int rhs )
{
    m_JulianDate += rhs;
    return *this;
}

Date& Date::operator-=( int rhs )
{
    m_JulianDate -= rhs;
    return *this;
}

const char* Date::nameOfDay( int d, bool abbrev )
{
    const char* retval = s_unknown;
    if (d >= 0 && d <= 7)
    {
        if (abbrev)
        {
            retval = dabbrev[d];
        }
        else
        {
            retval = dnames[d];
        }
    }
    return retval;
}

const char* Date::nameOfMonth( int m, bool abbrev )
{
    const char* retval = s_unknown;
    if (m > 0 && m <= 12)
    {
        if (abbrev)
        {
            retval = mabbrev[m-1];
        }
        else
        {
            retval = mnames[m-1];
        }
    }
    return retval;
}

uint32_t Date::daysBetween( const Date& lhs, const Date& rhs )
{
    return (uint32_t) ::abs( lhs - rhs );
}

Thanks for your reply guys. I am a beginner to C++. I just created this program and had a doubt if it is working perfectly. Thanks Rubberman for your snippet!

If you want to find out if the code works correctly then you have to write driver programs to test all of its features. In main() instantiate an instance of the class then add a few lines of code that call each of the class's methods and print out the results of the calculations. Don't expect other people to do your job for you because it ain't going to happen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.