View Single Post
Old 03-21-2004, 10:04 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Otherwise this:

If you don't have the time to check out the date libraries in C++ then here is your code revised.

- You definatly want to check out printShortYear():
It answers your question more directly. Look at the "itoa()" and the "string::assign()" function. Both of them come with the standard C++ library.

- I've taken the liberty to remove the comments:
It is obvious what these methods do, so lets not add them.
Certainly don't add comments for constructors. People should (and will) recognize constructors/destructors on-the-fly.


- I've removed the default values in your default ctor:
Default values in a default ctor takes away flexibility. Instead, I've added a ctor wich takes three arguments.

- I've broken certain algorithms in smaller parts.
This way there is no double code. It also makes the various methods more readable. Clearness is all.

Here is the complete code, a main function included to demonstrate it's workings:
In CDate.h
Code:
#ifndef CDATE_H
#define CDATE_H

#include <iostream>
#include <string>

using namespace std;

static const int daysPerMonth[13] = 
	{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

class Date 
{
public:
	Date() { GetDate(); ValidateDate(); } 
	Date(int mt, int dy, int yr) : month(mt), day(dy), year(yr) {	ValidateDate(); }
	~Date() { };
public:
	void print() const; 
	void printJulian() const;
	
private:
	int month;
	int day;
	int year;
	void GetDate();
	void ValidateDate();
	bool CheckValidDay();
	bool CheckForLeapYear() const;
	string printShortYear() const;
	void reset_istream(istream & is);
}; 

void Date::GetDate()
{	
retry_month: cout << "Enter the month: ";
	cin >> month; 
	if(cin.fail()) { reset_istream(cin); goto retry_month; return; }
retry_day: cout << "Enter the day: ";
	cin >> day;
	if(cin.fail()) { reset_istream(cin); goto retry_day; return; }
retry_year: cout << "Enter the year: ";
	cin >> year;	
	if(cin.fail()) { reset_istream(cin); goto retry_year; return; }
}

void Date::ValidateDate()
{
	if ((month > 0 && month <= 12)==false)
		{
			month = 1;
			cout << "Month is invalid. Month has been re-set to 1.\n";
		}	
	//Leapyear and valid day in current month.
	if(!CheckValidDay())
		{ cout<<"Day is invalid, day has been re-set to 1"<<endl; day =1; }
}

void Date::print() const
{	
	cout << "The date is " << month << '/' << day <<
		'/'<<printShortYear()<<endl; 
} 

void Date::printJulian() const
{	
	int julDate = day;
	for (int counter =0; counter < month; counter++)
		julDate = julDate + daysPerMonth[counter];	
	if ((year % 400 == 0 ||CheckForLeapYear() ) && (month >= 2 && day > 28))
		julDate++;

	cout <<"The date in Julian style ddd/yy is " << julDate << 
		'/'<<printShortYear()<< endl; 
} 

string Date::printShortYear() const
{
	char szYearBuffer[4];
	string sShortYear;
	itoa(year, szYearBuffer, 10);
	sShortYear.assign(szYearBuffer, 2, 2);
	return sShortYear;
}

bool Date::CheckForLeapYear() const
{	return (year % 4 ==0 && year % 100 != 0); }

bool Date::CheckValidDay() 
{
	if (!(day > 0 && day <= daysPerMonth [month]))
		return (month == 2 && day == 29 && CheckForLeapYear());	
	
	return true;
} 

void Date::reset_istream(istream & is)
{
	char ch;
	// Reset the state. 
	is.clear();
	// Remove all characters until we find a newline or EOF.
	ch = is.get();
	while ((ch != '\n')&&(ch != EOF))
		ch = is.get();	
	is.clear();
}

#endif //CDATE_H
Just ask if you need more help.
__________________
Valmont is offline   Reply With Quote