Got most of this done. I just can't see why my advance_date() method is only partially working...I think it's something with all my bools....but I don't know how else to get this condition met. Project is to Design, implement, and test an ADT that represents a calendar date..Integers are fine...include an operation that will advance the date 'one day.' and display with words or numbers. As an 'enhancement," include the name of the day. Well, I'd like to do that as well but can't figure out the day_of_function; which is why it's commented out. Once again, the first part of my advance_date() is working, February 28 goes to 29 and 29 goes to March 1. But all the rest jump to the next month instead of incrementing by one...Just can't see anymore...Thank you:

Header:

#include <iostream>
#include <string>
using namespace std;

class Date
{
public:

	//day_of_week();
	void advance_date();
	// increments the date
	void display_date();
	// outputs the date
	void change_date(int month1, int day1, int year1);
	// Reads in new values for the arguments day, month, and year of Date
	void name_date();
	// Assigns a numeric value which represents a determined date
	Date();
	// Default Constructor	
	Date(int month1, int day1, int year1);
	// Constructor with neccessary arguments to produce date output

private:
	
		int day, month, year;
};
//End Date

Implementation:

#include "DATE.h"
#include <iostream>
using namespace std;


	Date::Date():month(01),day(01),year(2008)
	{
	}

	Date::Date(int month1, int day1, int year1)
	{
		
		month = month1;
		day = day1;
		year = year1;
	}

	void Date::advance_date()
	{
		
		if(month == 2 && day > 1 && day < 29)
		{
			day += 1;
			//month = month;
		}

		else if(day == 29)
		{
			day = 1;
			month += 1;
		}
		//day = day + 1;
		else if((month == 4 || month == 6 || month == 9 || month == 11) && (day > 1 && day < 30))
		{			
			day += 1;
			//month += 1;
		}

		else if(day == 30)
		{
			day == 1;
			month += 1;
		}

		else if((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day > 1 && day < 31))
		{			
				day += 1;
			//month = month;
		}

		else if(day > 31)
		{
			day == 1;
			month += 1;
		}

			if(month > 12)
			{
				month == 1;
			}
	}


	void Date::display_date()
	{
		cout << month << "-" << day << "-" << year << endl;
	}

	void  Date::name_date()
	{

		if(month == 1)
		{
			cout << "January " << day << ", " << year << endl;
		}
		else if(month == 2)
		{
			cout << "February " << day << ", " << year << endl;
		}
		else if (month == 3)
		{
			cout << "March " << day << ", " << year << endl;
		}
		else if (month == 4)
		{
			cout << "April " << day << ", " << year << endl;
		}
		else if (month == 5)
		{
			cout << "May " << day << ", " << year << endl;
		}
		else if (month == 6)
		{
			cout <<"JUNE " << day << ", " << year << endl;
		}
		else if (month == 7)
		{
			cout << "July " << day << ", " << year << endl;
		}
		else if (month == 8)
		{
			cout << "August " << day << ", " << year << endl;
		}
		else if (month == 9)
		{
			cout << "September " << day << ", " << year << endl;
		}
		else if (month == 10)
		{
			cout << "October " << day << ", " << year << endl;
		}
		else if (month == 11)
		{
			cout << "November " << day << ", " << year << endl;
		}
		else if(month == 12)
		{
			cout << "December " << day << ", "<< year<< endl;
		}
	}
		


	void Date::change_date(int month1, int day1, int year1)
	{
		day = day1;
		month = month1;
		year = year1;
	}

Test File:

#include <iostream>
#include "DATE.h"
using namespace std;

int main()
{
	Date make_a_date;
	make_a_date.change_date(04,29,2008);
	make_a_date.display_date();
	make_a_date.name_date();
	make_a_date.advance_date();
	cout << endl;
	cout << "The advanced date is: " << endl;
	make_a_date.display_date();
	system("pause");

	return 0;
}

You should really consider using arrays for some of your data. For example, you could create an array that contains the number of days in each month. Your advance_date() function would be considerably simpler:

day++;

// check to see if this is the last day of the month
if (day > daysInMonth[month])
{
   day = 1;
   month++;

   // check to see if we're at the end of the year
   if (month > 12)
   {
      month = 1;
      year++;
   }
}

You can apply a similar technique when printing out the names of days and months.

Your tests and adjustments for month rollover should be inside the month group if's, not as separate else if's.

You testing for day ranges should be day >=1, not strictly >1 (what happens when you try to advance from first of a month?)

Do you really need to test day value? If it's January, add a day, then test for month change.

Thank you. I may try to do that.

Your tests and adjustments for month rollover should be inside the month group if's, not as separate else if's.

You testing for day ranges should be day >=1, not strictly >1 (what happens when you try to advance from first of a month?)

Do you really need to test day value? If it's January, add a day, then test for month change.

Well it's just that the other months aren't advancing properly...I'll try setting >=1; I should've seen that one...Regarding the rollovers being part of the ifs; I don't know how I can do that and keep the conditions? And I still don't get why the January works properly but the others don't

// Read the comments embedded in the code.
// Some Advice. Read this carefully and understand it. Then and only then read the rest of the code.
int primer_on_else_if()
{
  if( day == 1 )
    // do this when day == 1
    do_something();
  else
    // do then when day != 1
    do_something();

  // nested else if
  if( day == 1 )
    // do this when day == 1
    do_something();
  else if( day < 15 )
    // do this when day != 1 && day < 15
    do_something();
  else if( day == 29 )
    // do this when day != 1 && day >= 15 && day == 29
    do_something();
  else
    // do this when day != 1 && day >= 15 && day != 29
    do_something();

  // see the pattern. for control to reach an 'if' attached to an 'else', the 'else' clause must first execute

  // another way of writing the above would be
  if( day == 1 )
    // do this when day == 1
    do_something();
  else
    {
      if( day < 15 )
	// do this when day != 1 && day < 15
	do_something();
      else
	{
	  if( day == 29 )
	    // do this when day != 1 && day >= 15 && day == 29
	    do_something();
	  else
	    {
	      // do this when day != 1 && day >= 15 && day != 29
	      do_something();
	    } // else( day == 29 )
	} // else( day < 15 )
    } // else( day == 1 )
}  

#include "DATE.h"
#include <iostream>
using namespace std;

Date::Date():month(01),day(01),year(2008) 
{}

//---> instead of this
#if 0
Date::Date(int month1, int day1, int year1)
{
  month = month1;
  day = day1;
  year = year1;
}
#endif
//---> do this
Date::Date(int m, int d, int y): month(m),day(d),year(y)
{}

void Date::advance_date()
{
  /*
   * Year 2008 is the next leap year, with 29 days in February.  A leap
   * year is a year with one extra day inserted into February, the
   * leap year is 366 days with 29 days in February as opposed to the
   * normal 28 days.
   */
  // on feb 29, 2008 advance_date() won't work
  // i suggest keeping things simple and ignoring leap years entirely
  if(month == 2 && day > 1 && day < 29)
    {
      day += 1;
      //month = month;
    }
  // this 'else if' does not do what you think it does
  // what happens when month = 1, day = 29, whoops....
  else if(day == 29)
    {
      day = 1;
      month += 1;
    }
  // ok yea, i'm writing an else_if tutorial, read that first
  else if((month == 4 || month == 6 || month == 9 || month == 11) && (day > 1 && day < 30))
    {			
      day += 1;
      //month += 1;
    }
  // ok, snip rest of code... instead of what you have, please do this
  
  // identify the number of days in this month
  int days_in_month;

  // use branching if/else, or as joeprogrammer suggested use a data table
  
  // increment day

  // increment month if days overflow the number of days in month

  // don't do this
  if( day > days_in_month )
    {
      day == 1;
      month += 1;
    }
  /*
   * why? b/c you may need to add dates some day. for instance
   * date a(1,1,2008);
   * date b(2,1,2008);
   * date c = a;
   * c.add_days( days_between( a, b ) );
   */
  // ok, on second thought add_days() requires a recursive call. we
  // don't need that complexity right now so lets keep to the KISS
  // principle and do this
  if( day > days_in_month )
    {
      day == 1;
      month += 1;
    }
  
}


void Date::display_date()
{
  cout << month << "-" << day << "-" << year << endl;
}

void  Date::name_date()
{
  // read joe programmers suggestion. i would suggest a const char array
  char const * const [] month_name = { "jan", "feb", "mar", ... };
  // remember that c/c++ indexes arrays at base 0, so to access
  // month_name[ month-1 ] is "jan"

  // yay!! if/else if used correctly here
  if(month == 1)
    {
      cout << "January " << day << ", " << year << endl;
    }
  else if(month == 2)
    {
      cout << "February " << day << ", " << year << endl;
    }
  // snip, good stuff, long, but correct
}
		
// you don't need change_date(), why? copy constructor, like so
// date a(1,1,2008);
// now to change
// a = Date(2,10,2009);

void Date::change_date(int month1, int day1, int year1)
{
  day = day1;
  month = month1;
  year = year1;
}

Keeping it simple (no leap year handling)

void Date::advance_date()
{
  
  day += 1;
  if(month == 2 && day > 28)
    {
        day = 1;
        month++;
    }
    else if((month == 4 || month == 6 || month == 9 || month == 11) && (day >  30))
    {			
      day = 1;
      month++;
    }
    else if ( day > 31 )  //all other months
    {
       day = 1;
      month++;
    }

    if( month > 12 )
    {
          month = 1;
          year++;
     }      
  
 }

Thank you for this lesson!

Thank you for this intensive lesson!!!

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.