Hello,
Please I want to get an input of string, like mm/dd/yyyy hh/mm/ss
where first mm is month,.... and second mm is minutes....
then want to split each into date object.. like ... month day year.... after each / ....
I am lost here...
thanks

istream &operator>>(istream& inputStream, Date& dateObject)
{
    string inVariable[20];
    inputStream.getline(inVariable, 20, "\n" );
    //inputStream >> inVariable;

}

Recommended Answers

All 3 Replies

How is your Date class defined?

Following an interface of

class Date;

ostream &operator<<(ostream&, const Date&);
istream &operator>>(istream&, Date&);

class Date {

  private:
    int month;
    int day;
    int year;
    int hour;
    int minute;
    int second;

  public:

    Date(void);
    Date (int month, int day, int year, int hour, int minute, int second);

    virtual void setMonth(int&);
    virtual void setDay(int&);
    virtual void setYear(int&);
    virtual void setHour(int&);
    virtual void setMinute(int&);
    virtual void setSecond(int&);

    virtual int getMonth(void) const;
    virtual int getDay(void) const;
    virtual int getYear(void) const;
    virtual int getHour(void) const;
    virtual int getMinute(void) const;
    virtual int getSecond(void) const;

    virtual bool operator== (const Date &rhs);
    virtual bool operator< (const Date &rhs);

//    friend ostream &operator<<(ostream&, const Date&);
//    friend istream &operator>>(istream&, Date&);

};

#endif

Then I implement the below code...

/** Class Date models a date and time.
//This class contains private data members to store the day, month, and year of a date
//and the hour, minute, and second of a time.
//The class declaration includes the following public members

**/
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <sstream>

#include "Date.h"

using namespace std;

Date::Date( )//: month(0), day(0), year(0), hour(0), minute(0), second(0)
{
    month(0), day(0), year(0), hour(0), minute(0), second(0);
    /**Default constructor, all variable are set to Zero..  **/
};

Date::Date(int theMonth, int theDay, int theYear, int theHour, int theMinute, int theSeconds)
{
    if(theHour < 0 || theHour > 24 || theMinute < 0 || theMinute > 59 || theSeconds < 0 || theSeconds >59
        || theDay < 1|| theDay >31 || theMonth <1 || theMonth > 12 )
    {
        cout << "Illegal argument to Date Class constructor.";
        exit(1);
    }
    else
    {
        month   = theMonth;
        day     = theDay;
        year    = theYear;
        hour    = theHour;
        minute  = theMinute;
        second  = theSeconds;
    }
    if(theHour == 24)
    {
        theHour = 0; //Standardize midnight Time as 0:00
    }
}

Date::setMonth(int& newMonthValue)
{
    if(newMonthValue <1 || newMonthValue >12)
    {
        cout << "Illegal argument to Date Class constructor.";
        exit(1);
    }
    else
    {
        month = newMonthValue;
    }
}

void Date::setDay(int& newDay)
{
    if(newDay <1 || newDay >31)
    {
        cout << "Illegal argument to Date Class constructor.";
        exit(1);
    }
    else
    {
        day = newDay;
    }
}

void Date::setYear(int& newYear)
{
    year = newYear;
}

void Date::setHour(int& newHour)
{
    if(newHour < 0 || newHour >24)
    {
        cout << "Illegal argument to Date Class constructor.";
        exit(1);
    }
    else{
        if(newHour == 24)
            hour = 0;
        hour = newHour;
    }
}

void Date::setMinute(int& newMinutes)
{
    if(newMinutes <0 || newMinutes> 59)
    {
        cout << "Illegal argument to Date Class constructor.";
        exit(1);
    }
    else{
        minute = newMinutes;
    }
}

void Date::setSecond(int& newSeconds)
{
    if(newSeconds<0 || newSeconds >59)
    {
        cout << "Illegal argument to Date Class constructor.";
        exit(1);
    }
    else{second = newSeconds;}
}

// Accessors Start from here.. the getters
int Date::getMonth()const
{
    return month;
}

int Date::getDay()const
{
    return day;
}

int Date::getYear() const
{
    return year;
}

int Date::getHour() const
{
    return hour;
}

int Date::getMinute()const
{
    return minute;
}

int Date::getSecond() const
{
    return second;
}

bool Date::operator ==(const Date& date1, const Date& date2)
{
    /**
    After I tested this, I should change it to the one that use one argument, while the calling object is inherited automatic
    bool Date::operator ==(const Date& date2) const
    {
        return((return((getMonth() == date2.getMonth()) && (getDay() == date2.getDay()) && (getYear() == date2.getYear()) &&
           (getHour()== date2.getHour()) && (getMinute() == date2.getMinute()) && (getSecond() == date2.getSecond()));
    }

    */
    return((date1.getMonth() == date2.getMonth()) && (date1.getDay() == date2.getDay()) && (date1.getYear() == date2.getYear()) &&
           (date1.getHour()== date2.getHour()) && (date1.getMinute() == date2.getMinute()) && (date1.getSecond() == date2.getSecond()));
}
//virtual bool operator== (const Date &rhs);
//virtual bool operator< (const Date &rhs);

bool Date::operator<(const Date& secondOperand) const
{
    return((getMonth() < date2.getMonth()) && (getDay() < date2.getDay()) && (getYear() < date2.getYear()) &&
           (getHour()< date2.getHour()) && (getMinute() < date2.getMinute()) && (getSecond() < date2.getSecond()));
}

// << Operation OverLoad ...
ostream &operator<<(ostream& outputStream, const Date& dateObject);      //ostream& operator <<(ostream& outputStream, const Money& amount)
{
    outputStream << dateObject.month << "/" << dateObject.day << "/" << dateObject.year
    << " " << dateObject.hour << "/" << dateObject.minute << "/" << dateobject.second << "\n" ;

    return outputStream ;
}

// >> Operation OverLoad.
istream &operator>>(istream& inputStream, Date& dateObject)
{
    string inVariable[20], string valueHolder[20], tempHolder;
    int phaseCounter =0, ;
    inputStream >> inVariable;
    inputStream.getline(inVariable, 20, "\n" );
    //inputStream >> inVariable;
    for(int i=0; i<inVariable.size(); i++)
    {
        if (inVariable[i] = "/" || inVariable[i] == ':' || inVariable[i] = ' ')
        {
            ++phaseCounter;
            tempHolder = " ";
            continue;
        }
        else
        {
            if(!(isdigit(inVariable[i])))
            {
                cout<< "Illegal input -- non-digit character as Date and Time \n" <<endl;
                exit(1);
            }
            tempHolder += inVariable[i];
        }
        valueHolder[phaseCounter] = tempHolder;
    }

    //dateObject = Date(valueHolder[1], valueHolder[2], valueHolder[3], valueHolder[4], valueHolder[5], valueHolder[6])
    month = dateObject.setMonth(valueHolder[1]);
    day = dateObject.setDay(valueHolder[2]);
    year = dateObject.setYear(valueHolder[3]);
    hour = dateObject.setHour(valueHolder[4]);
    minute = dateObject.setMinute(valueHolder[5]);
    second = dateObject.setSecond(valueHolder[6]);

    return inputStream ;

}

I am lost with the overloading of >> and << , Since the TestDrive.cpp, is not compling
please help pointer out my error..
Thanks

Thank.. I have solve the Issue...

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.