Thread: Advancing Days

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4

    Advancing Days

    Hi, I need to write a function that will be passed two days, and the function needs to advance the day of the first date until it matches the second date. I have written all of my code until this function. I tried to break the function into different sections but I am completely lost. I know I need to increment the day until a new month, then start over with incrementing the days, but I am not sure how to do this from month to month. Any help would be greatly appreciated!!! I only got the difference function up to Month2 and tested it, but it was not working.

    Code:
    //
    //This program will allow the user to enter in two dates, and will return each date in the written month, day, year
    //format.  For example, if 3/3/2009 is entered, March 3, 2009 will be returned.  It will also determine which date 
    //is earlier and which is later, and will calculate the difference in days between the two dates.
    
    #include <iostream>
    using namespace std;
    
    class Date
    {
       public:
    	Date() {month=1; day=1; year=1900;}; 
    	  //default constructor; initializes the date to 1/1/1900
    	Date(int m, int d, int y); 
     	  //constructor; initializes the date that is entered in
    	void set_date(int m, int d, int y); 
    	  //allows the user to enter in the date
    	void display_date();
    	  //prints the date with the month in word format; i.e. 1/1/2000 will print out January 1, 2000
    	void advance_date();
    	  //advances the date by 1 day
    	int get_day();
    	  //returns the value of the invoking day
    	int get_month();
    	  //returns the value of the invoking month
    	int get_year();
    	  //returns the value of the invoking year
    	bool is_less_than(Date&);
    	  //returns true if the original invoking date is less than the one passed in, and false otherwise
    	bool is_equal(Date&);
    	  //returns true if the original invoking date is equal to the one passed in, and false otherwise
    
       private:
    	int month;
    	int day;
    	int year;
    };
    
    int difference(Date earlier_date, Date later_date);
    //calculates the difference in days between the earlier_date (date1) and the later_date (date2) passed in by the user
    
    bool is_leap(int year);
    //determines if the year is a leap year
    
    void adjust_for_leap(int days_in_month);
    //if it is a leap year, adjusts February to have 29 days rather than 28
    
    int main()
    {
    	Date first_date;
    	Date second_date;
    	Date earlier_date;
    	Date later_date;
    	char slash; //reads the forward slash in the date  
    	int month1, day1, year1, month2, day2, year2;
    	int days_apart;
    
    	cout << "Please enter a Date in the following format: xx/xx/xxxx\n";
    	cin >> month1 >> slash >> day1 >> slash >> year1;
    	first_date.set_date(month1, day1, year1);
    
    	cout << "First Date is: ";
    	first_date.display_date();
    
    	cout << "Please enter a second Date in tbe following format: xx/xx/xxxx\n";
    	cin >> month2 >> slash >> day2 >> slash >> year2; 
    	second_date.set_date(month2, day2, year2);
    
    	cout << "Second Date is: ";
    	second_date.display_date();
    	
    	if (first_date.is_equal(second_date))
    	{
    	  cout << "The first date is the same as the second date.\n";
    	}
    	else if (first_date.is_less_than(second_date))
    	{
    	  earlier_date = first_date;
    	  later_date = second_date;
    	  cout << "The first date is earlier than the second date.\n";
    	}
    	else
    	{
    	  earlier_date = second_date;
    	  later_date = first_date;
    	  cout << "The first date is later than the second date.\n";
    	}
    
    	difference(earlier_date, later_date);
    	
    	return 0;
    }
    
    int Date::get_month()
    {
    	return month;
    }
    
    int Date::get_day()
    {
    	return day;
    }
    
    int Date::get_year()
    {
    	return year;
    }
    
    void Date::set_date(int m, int d, int y)
    {
    	month = m;
    	day = d;
    	year = y;
    	return;
    }
    
    void Date::display_date()
    {
    	string name_of_month[12];
    	
    	name_of_month[0] = "January";
    	name_of_month[1] = "February";
    	name_of_month[2] = "March";
    	name_of_month[3] = "April";
    	name_of_month[4] = "May";
    	name_of_month[5] = "June";
    	name_of_month[6] = "July";
    	name_of_month[7] = "August";
    	name_of_month[8] = "September";
    	name_of_month[9] = "October";
    	name_of_month[10] = "November";
    	name_of_month[11] = "December";
    
    	cout << name_of_month[month-1] << " " << day << ", " << year << endl;
    	return;
    }
    
    
    bool Date::is_equal(Date& second_date)
    {
    	if ((month == second_date.month) && (day == second_date.day) && (year == second_date.year))
    	  return true;
    	else
    	  return false;
    }
    
    bool Date::is_less_than(Date& second_date)
    {
    	if (year < second_date.year)
    	{
    	  return true;
    	}
    	else if ((month < second_date.month) && (year < second_date.year) && (day < second_date.year))
    	{
    	  return true;
    	}
    	else if (day < second_date.day)
    	{
    	  return true;
    	}
    	else 
    	{ 
    	  return false;
    	}
    }
    
    int difference(Date earlier_date, Date later_date)
    {
          int diff_days = 0;
          int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
          //if month and year are the same, and days are either equal or different
          if ((earlier_date.get_month() == later_date.get_month()) && (earlier_date.get_year() == later_date.get_year()))
    	{
    	  while(earlier_date.get_day() < later_date.get_day())
    	  {    
    	    earlier_date.advance_date();
    	    diff_days++;
    	  }
    	}
    
    	//if year is the same but months or days are different
    	if (earlier_date.get_year() == later_date.get_year())
    	{
    	  while (earlier_date.get_month() != later_date.get_month())
    	  { 
    	    if (earlier_date.get_month() == 1)
    	    {
    	     while (earlier_date.get_month() < days_in_month[0])	  
    	     {
    	      earlier_date.advance_date();
    	      diff_days++;
    	     }
    	    }
    	    if (earlier_date.get_month() == 2)
    	    {
    	     while (earlier_date.get_month() < days_in_month[1])
    	     {
    	      earlier_date.advance_date();
    	      diff_days++;
    	     }
    	    }
    	  }
    	}
    
    	cout << "The two dates are " << diff_days << " days apart.\n";
    }
    
    void Date::advance_date()
    {
    	day++;
    	return;	
    }
    
    bool is_leap(int year)
    {
    	if (year %4 != 0) return(false);
    	else if (year %100 != 0) return(true);
    	else if (year % 400 == 0) return(true);
    	else return(false); 
    }
    
    void adjust_for_leap(int days_in_month[])
    {
    	days_in_month[1] = 29;
    	return;
    }
    I am having trouble writing the difference(Date&, Date&) function. Thanks in advance for any help!
    Last edited by nhubred; 06-01-2009 at 06:25 PM. Reason: removing/adding some code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Plz help me C programm
    By ferroz1 in forum C Programming
    Replies: 7
    Last Post: 05-10-2008, 06:55 AM
  2. summing number of days between 2 months
    By duff_johnny in forum C Programming
    Replies: 5
    Last Post: 11-03-2005, 02:36 PM
  3. I have a function and I need multiple averages returned
    By tommy69 in forum C++ Programming
    Replies: 20
    Last Post: 04-13-2004, 11:45 AM
  4. Counting Number of days from year zero
    By wireless in forum C++ Programming
    Replies: 4
    Last Post: 06-16-2002, 07:31 AM
  5. while, sentinel, if, switch
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2001, 11:50 PM