Hi,

I am to write a C++ date class.

I have set up the class. I am able to add and substract to the number of years.

The requirements are that I have to do the same for the number of days and the number of months as well. I am not really sure how to go about doing that part.

The default date is 1/1/2000, so if I add 31 days, the new date should be 2/1/2000. Similary if the date is 1/1/2000 and I add 24 months the new date should be 1/1/2002 (actually 1/2/2002 since 2000 is a leap year).

The starting date is always 1/1/2000 as it is the default.

I have attached my code below. This is what I have so far for the class.

class date{


	private:
		int curr_day; // this block for default
		int curr_month;
		int curr_year;
		
		int new_day; //this block for changes
		int new_month;
		int new_year;
		
		int month; //this block for comparisons
		int day;
		int year;

		string date1;

	public:

		date(){
		//sets the default date to 1/1/2000
			curr_day   = 1;
			curr_month = 1;
			curr_year  = 2000;

			new_day = curr_day;
			new_month = curr_month;
			new_year = curr_year;

			
			//print_Date();
			
		}

				
		//MODIFIER Functions
	        //add days to default date
		void add_days(int num_days){
		
			
		
		}//end


		//substract days from default date
		void substr_date(int num_days){
		
		
		}//end
		
		
		//add months t default date
		void add_months(int num_months){
			int temp = curr_month + num_months;

			if (temp <= 12)
				new_month = temp;
			else
			{

					




			}
		
		
		}//end

		
		//substract months from defualt date
		void substr_months (int num_months){
			int temp = curr_month - num_months;

			if ((temp <= 12) && (temp >= 1))
				new_month = temp;
			
		
		}//end
		
		
		//add years to default date
		void add_years (int num_years) {
		
			new_year = curr_year + num_years;
			//print 
		
		}//end

		
		//substract years from default date
		void subtr_years (int num_years) {
		
			new_year = curr_year - num_years;

		}//end
		
		
		
		//get size of month
		int check_month(int ind_month){
			
			switch (ind_month){
				
				case 1 : return 31;
						break;

				case 2 : {	if (isLeapYear())
								return 29;
							else
								return 28;
							break;
						 }

				case 3 : return 31;
						break;
				case 4 : return 30;
						break;

				case 5 : return 31;
						break;

				case 6 : return 30;
						break;

				case 7 : return 31;
						break;

				case 8 : return 31;
						break;
				case 9 : return 30;
						break;
				case 10: return 30;

				case 11: return 31;
						break;

				case 12: return 31;
						break;

			}//end switch
		
		}//end
		
			
		bool isLeapYear(){
		
			if (isLeapYear(curr_year))
				return true;
			else
				return false;
		}//end

		
		
		bool isLeapYear(int year){
		
			if ( (year%4 == 0) && (year%100 == 0) && (year%400 == 0))
				return true;
			else
				return false;
		
		}//end
		
		

		void print_new_Date(){
		
			cout << setw(20) << "\t" << new_month <<"/" <<new_day << "/" << new_year;
			
			if (isLeapYear(new_year))
				cout << "   Leap Year Too!!" << endl <<endl;
		
		}//end



		void print_Date(){
			if (isLeapYear(curr_year))
				cout << setw(27) << "   Leap Year!!" << endl;

			cout << setw(10) << "\t" << curr_month <<"/" << curr_day << "/" << curr_year;
			
		}

It would have been much better if you had posted a part of you code where you are facing problem.

Ok,

Whenever you add days, you do it like this

First make an array say montd which stores the number of days in each month.

Now here the function for adding:

void addDays(int number)
{
    days = days + number;
    
    if(days>montd[month])
    {
        days = days - montd[month];
        ++month;
        if(month>12)
        {
            month = 1;
            ++year;
        }
    }
}

check_month() is incorrect. November has only 30 days, not 31.

class data objects: I think all you need are current_day, current_month, and current_year. You don't really need any of the others. add_years() for example should be coded like this:

//add years to default date
		void add_years (int num_years) {
		
			curr_year = curr_year + num_years;
			//print 
		
		}//end

You need a loop to add months,

while new months > 12
    increment year
    subtract 12 from months
end of while loop
//Now check the value of day to make sure it does not exceed 
//number of days in the new month so that you don't wind up
// with something like 31 February.

Oh yes picked out mistake in my code. You will also need loop or calculate remainder to increment months. It would work if days are less than the no of days in month but not if larger, eg. 65 days.

Thanks. I got it.

I used the following code. It works because I have a default date of 1/1/2000

void add_months(int num_months){		
		
		int months =  Month + num_months;


		if (months < 12)
			Month = months;
		else{

			while (months > 12){
		
				Year++;
				months = months - 12;		
			}	

			Month = months;
			
			
		}//end else
			

	}//end

it might work but it could be a little more efficient. Don't need variable months and you don't need that if statement, just the while loop.

Month = Month + nun_months;
while(Month > 12)
{
    Year++;
    Month = Month - 12;
}

Now after that code you need to verify that Day is in range for the given Month.

But the problem is though, if the user inputs a certain number of days to subtract, it would be the default day(1) - user input. So let's say the user inputs 500 days. Then the subtract_day will do 1 - 500 right? But if it does it, then the day = -499? How would we do it without negative numbers. Since the default is 1/1/2000 and we want to subtract 10 days, the new date will be...12/21/1999 right? So what code can we implement for that?

Well I did the substract months

Just do the reverse of the add months.

while (num_months > 12){

			//update the years
			Year--;
			num_months = num_months - 12;
		}

After that, update the number of months based on how many months are left over when you leave that loop.

The substract days is a little bit more complex, at least it was for me. I will post the code as soon as I've polished it up a bit.

But basically you do the opposite. Since the default date is one, you just have to remember to keep track of the 1 day in January while you are substracting from the other months. Basically you are counting backwards. So after Jan the next month is Dec and so on.

Will post code soon...too mess right now

Depends on how the dates are formatted. If they are in YYYYMMDD format then you can simply use strcmp()

char *date1 = "2010/01/01";
char *date2 = "2010/06/05";

int n = stramp(date1,date2);
This article has been dead for over six months. Start a new discussion instead.