I am trying to make this accept a 4 digit year. Here is the code I have so far.
Code:
using namespace std;
DateClass::DateClass()
: Month(0), Day(0), Year(0)
{
}
//--------------------------------------------------------------------------------
DateClass::DateClass(int MonthArg, int DayArg, int YearArg)
: Month(MonthArg), Day(DayArg), Year(YearArg)
{
}
//--------------------------------------------------------------------------------
bool DateClass::Read(istream & InFile)
/* Reads a date from InFile. Input is assumed to be in mm/dd/yy form.
true returned if date not 0/0/0.
Post: String read from InFile and parsed into Month, Day, and Year.
true returned if date not 0/0/0. false returned otherwise. */
{
lvpstring S, MonthStr, DayStr, YearStr;
getline(InFile,S);
MonthStr = S.substr(0,S.find('/'));
S = S.substr(S.find('/')+1,S.length()-S.find('/')-1);
DayStr = S.substr(0,S.find('/'));
YearStr = S.substr(S.find('/')+1,S.length()-S.find('/')-1);
Month = atoi(MonthStr.c_str());
Day = atoi(DayStr.c_str());
Year = atoi(YearStr.c_str());
return ((Month!=0) && (Day!=0) && (Year!=0));
}
//--------------------------------------------------------------------------------
void DateClass::SetDate(int MonthArg, int DayArg, int YearArg)
{
Month = MonthArg;
Day = DayArg;
Year = YearArg;
}
//--------------------------------------------------------------------------------
void DateClass::Write(ostream & OutFile) const
/* Writes D to the output stream
Post: D written to OutFile */
{
OutFile.width(2); OutFile << Month << '/';
OutFile.width(2); OutFile << Day << '/';
OutFile.width(2); OutFile << Year;
}
//--------------------------------------------------------------------------------
bool DateClass::operator <(const DateClass Date2) const
{
if (Year < Date2.Year)
return (true);
else if (Year > Date2.Year)
return (false);
else if (Month < Date2.Month)
return (true);
else if (Month > Date2.Month)
return (false);
else
return (Day < Date2.Day);
}
//--------------------------------------------------------------------------------
bool DateClass::operator > (const DateClass Date2) const
{
return (!(operator==(Date2)) && !(operator<(Date2)));
}
//--------------------------------------------------------------------------------
bool DateClass::operator == (const DateClass Date2) const
{
return ((Day == Date2.Day) && (Month == Date2.Month) &&
(Year == Date2.Year));
}
//--------------------------------------------------------------------------------
bool DateClass::operator != (const DateClass Date2) const
{
return !(operator==(Date2));
}
//--------------------------------------------------------------------------------
bool DateClass::operator >= (const DateClass Date2) const
{
return ((operator==(Date2)) || (operator>(Date2)));
}
//--------------------------------------------------------------------------------
bool DateClass::operator <= (const DateClass Date2) const
{
return ((operator==(Date2)) || (operator<(Date2)));
}
//--------------------------------------------------------------------------------
ostream& operator << (ostream & OutFile, const DateClass &D)
{
D.Write(OutFile);
return (OutFile);
}
//--------------------------------------------------------------------------------
istream & operator >>(istream &InFile, DateClass &D)
/* Reads from InFile to D
Post: D is read from Infile */
{
D.Read(InFile);
return (InFile);
}
Here is my date.h file
Code:
#ifndef _DATECLASS_
#define _DATECLASS_
#include <iostream.h>
#include <lvpstring.h>
#include <stdlib.h> // for atoi()
using namespace std;
class DateClass {
public:
DateClass();
DateClass(int MonthArg, int DayArg, int YearArg);
bool Read(istream & InFile);
void SetDate(int MonthArg, int DayArg, int YearArg);
void Write(ostream & OutFile) const;
bool operator ==(const DateClass Date2) const;
bool operator !=(const DateClass Date2) const;
bool operator <(const DateClass Date2) const;
bool operator <=(const DateClass Date2) const;
bool operator >(const DateClass Date2) const;
bool operator >=(const DateClass Date2) const;
private:
int Month;
int Day;
int Year;
};
#include "date.cpp"
#endif