GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Feb-2005, 08:55
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

need a little help


here's a template for a class definition of a program,what i need is a way to do error checking on the initializer values for data members month,day and year.

CPP / C++ / C Code:
#ifndef DATE_H
#define DATE_H

// class Date definition
class Date {

public:
   Date( int = 1, int = 1, int = 1900 ); // default constructor
   void print();

   void setDate( int, int, int );

   void setMonth( int );
   int getMonth();

   void setDay( int );
   int getDay();

   void setYear( int );
   int getYear();

   bool isLeapYear();
   int monthDays();
   void nextDay();
   /* Write prototype for nextDay */

private:
   int month;
   int day;
   int year;

}; // end class Date

#endif // DATE_H

thanks a bunch guys.
Last edited by LuciWiz : 01-Mar-2005 at 00:37. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 28-Feb-2005, 09:22
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

nevermind got it..lol


sorry guys.thought it was going to be desperate,but i was mistaken.got it done and all.here is the entire program in case somebody needs it.the code sets a date and increments days by one with account to date changes.
this program can be found in the lab accompanion book,of c++ how o program 4th edition,since it's the book i'm working with.

CPP / C++ / C Code:
// date.h
#ifndef DATE_H
#define DATE_H

// class Date definition
class Date {

public:
   Date( int = 1, int = 1, int = 1900 ); // default constructor
   void print();

   void setDate( int, int, int );

   void setMonth( int );
   int getMonth();

   void setDay( int );
   int getDay();

   void setYear( int );
   int getYear();

   bool isLeapYear();
   int monthDays();
   void nextDay();
   /* Write prototype for nextDay */
   void fixit();

private:
   int month;
   int day;
   int year;

}; // end class Date

#endif // DATE_H

CPP / C++ / C Code:
// datem.cpp
// member function definitions for date.cpp
#include <iostream>

using std::cout;

#include "date.h"

// constructor
Date::Date( int m, int d, int y ) 
{
   setDate( m, d, y );

} // end class Date constructor

// return day
int Date::getDay() 
{
   return day;

} // end function getDay

// return month
int Date::getMonth() 
{
   return month;

} // end function getMonth

// return year
int Date::getYear() 
{
   return year;

} // end function getYear

// set day
void Date::setDay( int d )
{
   if ( month == 2 && isLeapYear() )
      day = ( d <= 29 && d >= 1 ) ? d : 1;

   else
      day = ( d <= monthDays() && d >= 1 ) ? d : 1;

} // end function setDay

// set month
void Date::setMonth( int m ) 
{ 
   month = m <= 12 && m >= 1 ? m : 1;

} // end function setMonth

// set year
void Date::setYear( int y )
{
   year = y <= 2010 && y >= 1900 ? y : 1900;

} // end function setYear

// set date
void Date::setDate( int mo, int dy, int yr )
{
   setMonth( mo );
   setDay( dy );
   setYear( yr );

} // end function setDate

// output Date
void Date::print()
{ 
   cout << month << '-' << day << '-' << year << '\n';

} // end function print

/* Write function nextDay here */
void Date::nextDay()
{
	
	day++;	
	
}

// test whether is a leap year
bool Date::isLeapYear()
{
   if ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) )
      return true;

   else
      return false;    // not a leap year

} // end function isLeapYear

// return days in month
int Date::monthDays()
{
   const int days[ 12 ] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 
                            31, 30, 31 };

   return month == 2 && isLeapYear() ? 29 : days[ month - 1 ];

} // end function monthDays
void Date::fixit()
{
	if ( day >30)
	{
		day = 1;
		month++;
	}
	if (month > 12)
	{
		month = 1;
		year++;
	}
}

CPP / C++ / C Code:
// date.cpp
#include <iostream>

using std::cout;
using std::endl;

#include "date.h"

int main()
{
   const int MAXDAYS = 160;
   Date d( 10, 2, 2002 );

   // test print and nextDay
   for ( int loop = 1; loop <= MAXDAYS; ++loop ) {
	  d.print();
	 d.nextDay();
	 d.fixit();
      /* Write call to nextDay */

   } // end for

   cout << endl;

   return 0;

} // end main

taddaaaa.
have a nice day everybody.
Last edited by LuciWiz : 01-Mar-2005 at 00:39. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #3  
Old 28-Feb-2005, 11:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by ap6118
the code sets a date and increments days by one with account to date changes.

A good effort.

However...

In testing this I see a couple of issues:

1. Did you notice that all months have 30 days? (Look at fixit())

2. In the constructor, you call setMo(), then setDay(), then setYear(). However , I see that setDay() calls isLeapYear(), which needs to know the year. (So call setYear() must be called before setDay())
Try initializing February 29 in a leap year, such as 2004, then a non-leap year, such as 2005 to see the result.

You have certainly learned how to put a program together in C++. A few details that can be easily fixed, and you are ready for Bigger and Better things. Keep up the good work!

Regards,

Dave
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.