Thread: 4 digit year

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    31

    4 digit year

    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

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    what is the problem youre having?

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    It takes a two digit year. I am trying to get it to use a four digit year so it knows 12/10/03 is 2003 and not 1903

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >It takes a two digit year.
    It takes a four digit year too. Your class simply restricts itself to showing two digits. If you want you can make changes to Write and it should work properly for you if you enter dates such as /1/22/2004. If you want two digit years such as 03 to evaluate correctly, you need to take the current century into account as well.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    If you want two digit years such as 03 to evaluate correctly, you need to take the current century into account as well.
    How would I go about doing that?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How would I go about doing that?
    The easy way would be to assume the current century and add the appropriate amount to your output.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    I have been playing arounf with this and I think I need to make a change somewhere in this piece of code.
    Code:
    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));
    }
    I want it to be mm/dd/yyyy and not mm/dd/yy

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I think I need to make a change somewhere in this piece of code.
    Are you sure? Try printing out the value of Year before you return and see what it prints to see if you're right. In other words:
    Code:
    Year = atoi(YearStr.c_str());
    cout<< Year <<endl;
    
    return ((Month!=0) && (Day!=0) && (Year!=0));
    It would also be a good idea to direct your attentions here as well:
    Code:
    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;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Calendar Problem
    By wordup in forum C Programming
    Replies: 7
    Last Post: 10-29-2002, 03:36 PM
  3. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  4. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 05:23 AM
  5. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 03:50 PM