I need help with this C++ program

Override the operator -= for the Date class given at the end of Unit 16. To support this method, also create a private method called decrement() that works similar to increment, but correctly subtracts one day from current date. Test your program using June 7, 1982 and subtract 5 days, add 1 and then subtract 15. Be sure to print out each intermediary date. Turn in copies of all three files needed to compile your program: main.cpp, Date.cpp, and Date.h

(I have some of this program written from a previous program that i tried to edit it to fit the question above but i am having trouble making the program work the way the questions is asking for. Please help and some of the program lines are not being used because it is from a previous program.)


here is my code:

//header file

#ifndef DATE_H
#define DATE_H

#include <iostream>
using namespace std;

#define nMONTHS 12

class Date
{
public:
//Constructors
Date( int m = 1, int d = 1, int y = 1000 );

friend ostream& operator<<( ostream&, const Date& date );
friend istream& operator>>( istream&, Date& date );

Date operator+( const int d );
Date operator+=( const int d );
Date operator-=( const int d );
Date operator--( ); //prefix operator (ex. ++date;)
Date operator--(int dummy);
Date operator++( ); //prefix operator (ex. ++date;)
Date operator++(int dummy); //postfix operator (ex. date++;)

bool operator<( const Date date );
int& operator[]( const int& index );

void SetDate( int m, int d, int y );
int GetDay( ) const;
int GetMonth( ) const;
int GetYear( ) const;

//Returns true if it is a leap year
bool LeapYear() const;

void AddNumberOfDays( int days );

//aprivate:
int month;
int day;
int year;

int nDays[nMONTHS + 1];

void increment();
void decrement();
};

#endif



//Date.cpp file:

#include <iostream>
#include "date.h"

Date::Date( int m, int d, int y )
{
nDays[0]=0; nDays[1]=31; nDays[2]=28;
nDays[3]=31; nDays[4]=30; nDays[5]=31;
nDays[6]=30; nDays[7]=31; nDays[8]=31;
nDays[9]=30; nDays[10]=31; nDays[11]=30;
nDays[12]=31;

SetDate( m, d, y );
}

ostream& operator<<( ostream& stream, const Date& d)
{
stream << d.month << "/" << d.day << "/" << d.year;

return stream;
}

istream& operator>>( istream& stream, Date& date)
{
int d=0, m=0, y=0;

cout << "Enter the Month: ";
stream >> m;

cout << "Enter the Day: ";
stream >> d;

cout << "Enter the Year: ";
stream >> y;

date.SetDate( m, d, y );

return stream;
}

Date Date::operator+( const int d )
{
Date newDate = *this;

for(int i=1; i<=d; i++)
newDate.increment();

return newDate;
}

Date Date::operator+=( const int d )
{
for(int i=1; i<=d; i++ )
this->increment( );
return *this;
}

Date Date::operator-=( const int d )
{
for(int i=d; i<=1; i-- )
this->decrement( );
return *this;
}

Date Date::operator++( ) //prefix ++ operator
{
this->increment();
return *this;
}

Date Date::operator--( ) //prefix ++ operator
{
this->decrement();
return *this;
}

Date Date::operator--(int dummy) //postfix ++ operator
{
Date temp( month, day, year );
temp.decrement();
return temp;
}

Date Date::operator++(int dummy) //postfix ++ operator
{
Date temp( month, day, year );
temp.increment();
return temp;
}

bool Date::operator<( const Date date )
{
if( year < date.year ){
return true;
}else if( (year == date.year) && ( month < date.month) ){
return true;
}else if( (year == date.year) && (month == date.month) && (day < date.day) ){
return true;
}
return false;
}

int& Date::operator[]( const int& index )
{
if( index == 0 )
return month;
else if (index == 1 )
return day;
else if (index == 2 )
return year;
else
{
cout << "Date::operator[]( const int& index ) - Index out of range error." << endl;
exit(0);
}
}

void Date::SetDate( int m, int d, int y )
{
//Ensure arguments are valid. If not, set to default values
month = ( m>0 && m<13 ) ? m : 1;
year = ( y>0 ) ? y : 1;

//Account for leap years
if( month == 2 && LeapYear( ) )
{
day = ( d>0 && d<=29 ) ? d : 1;
}else{
day = ( d>0 && d<=nDays[month] ) ? d : 1;
}
return;
}

int Date::GetDay( ) const { return day; }

int Date::GetMonth( ) const { return month; }

int Date::GetYear( ) const { return year; }

void Date::AddNumberOfDays( int days )
{
for( int i=1; i<=days; i++ )
increment( );
return;
}

bool Date::LeapYear( ) const
{
// leap years happen every 4 years.
if( year%4 == 0 )
{
//but not every hundred years, unless 400 years have passed.
if( year%400 == 0 )
return true;
if( year%100 == 0 )
return false;

return true;
}
return false;
}

void Date::increment()
{
day++;

//Handle 4 special cases
if( month==2 && LeapYear() && day == 30 ){
// february: leap year
day = 1;
month = 3;
}else if(month==2 && !LeapYear() && day == 29 ){
// february: not a leap year
day = 1;
month = 3;
}else if( month==12 && day == 32 ){
// end of year case
day = 1;
month = 1;
year++;
}else if( month != 2 && day == nDays[month]+1 ){
// end of all months but february
day = 1;
month++;
}
return;
}



//main.cpp

#include <iostream>

using namespace std;

#include "date.h"

int main()
{
Date partyDate( 12, 25, 1999 );
Date yourDate;
cin >> yourDate;

cout << partyDate << endl;
cout << yourDate << endl;

yourDate.AddNumberOfDays( 4 );
cout << yourDate << endl;

yourDate -= 34;
cout << yourDate << endl;

cout << "Difference between postfix and prefix operator++" << endl;
cout << "Start with: " << yourDate << endl;
cout << "Prefix operator: " << ++yourDate << endl;
cout << "After Prefix operator: " << yourDate << endl;
cout << "Postfix operator: " << yourDate++ << endl;
cout << "After Postfix operator: " << yourDate << endl;

Date newDate;
newDate = yourDate + 14;
cout << newDate << endl;

if( partyDate < newDate )
cout << "Party!" << endl;
else
cout << "Don't party." << endl;

cout << newDate[0] << "-" << newDate[1] << "-" << newDate[2] << endl;

newDate[0] = 10;
newDate[1] = 12;
newDate[2] = 2011;

cout << newDate << endl;

return 1;
}
Please edit you post and enclose your code with [ code ] [ /code ]. Its hard to see whats going on otherwise especially with that much code.
Topic archived. No new replies allowed.