0

Hello,
Please, i dont know why my implementation is failing the testDemo Fucntion.. Below is the full code.
it is Failing operator < .... testLessThan
interface:

  #ifndef DATE_H_
    #define DATE_H_

    #include<iostream>

    using namespace std;

    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:
        virtual ~Date();
        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&);
    };
1
Contributor
3
Replies
14
Views
2 Months
Discussion Span
Last Post by jnneson
0

implementation: I will also Appreciate if better way to improve the efficiency

#include <iostream>
#include <cstdlib>
#include <cctype>
#include <string>
#include <sstream>
#include <locale>

#include "Date.h"

using namespace std;

string replaceSpace(string& sample);

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

};

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

        month   = theMonth;
        day     = theDay;
        year    = theYear;
        hour    = theHour;
        minute  = theMinute;
        second  = theSecond;
    }
    }

void Date::setMonth(int& newMonthValue)
{
    if(newMonthValue <1 || newMonthValue >12)
    {
        cout << "Illegal argument -- SetMonth Value Error.";
        exit(1);
    }
    else
    {
        month = newMonthValue;
    }
}

void Date::setDay(int& newDay)
{
    if(newDay <1 || newDay >31)
    {
        cout << "Illegal argument Day Value Error.";
        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 --Na Hour Get Issue Value Error.";
        exit(1);
    }
    else
    {
        if(newHour == 24)
            newHour = 0;
        hour = newHour;
    }
}

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

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

// Accessor 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& 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((getMonth() == date2.getMonth()) && (getDay() == date2.getDay()) && (getYear() == date2.getYear()) &&
            (getHour()== date2.getHour()) && (getMinute() == date2.getMinute()) && (getSecond() == date2.getSecond()));

}

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

//  return((getMonth() < secondOperand.getMonth()) &&
//          (getDay() < secondOperand.getDay()) &&
//          (getYear() < secondOperand.getYear()) &&
//          (getHour() < secondOperand.getHour()) &&
//          (getMinute() < secondOperand.getMinute()) &&
//          (getSecond() < secondOperand.getSecond())
//          );
}

// << Operation OverLoad ...
ostream &operator <<(ostream& outputStream, const Date& dateobject)      //ostream& operator <<(ostream& outputStream, const Money& amount)
{
    outputStream << dateobject.getMonth() << "/" << dateobject.getDay() << "/" << dateobject.getYear()
            << " " << dateobject.getHour() << "/" << dateobject.getMinute() << "/" << dateobject.getSecond() << endl;

    return outputStream ;
}

// >> Operation OverLoad.
istream &operator>>(istream& inputStream, Date& dateObject)
{
    string inVariable;
    getline(inputStream, inVariable);
    int valueHolder[6];
    string tempHolder;
    int phaseCounter = 0 ;

    inVariable = replaceSpace(inVariable);

    for(unsigned i=0; i<inVariable.length(); i++)
    {
        if(inVariable[i] == '\n')
            break;

        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];
            cout<< "At phase "<< phaseCounter <<" value is : " <<  tempHolder <<endl;
        }
        if(!(tempHolder.empty()))
        {
            istringstream ss(tempHolder);
            ss>>valueHolder[phaseCounter];
        }
    }

    //dateObject = Date(valueHolder[0], valueHolder[1], valueHolder[2], valueHolder[3], valueHolder[4], valueHolder[5]);
    dateObject.setMonth(valueHolder[0]);
    dateObject.setDay(valueHolder[1]);
    dateObject.setYear(valueHolder[2]);
    dateObject.setHour(valueHolder[3]);
    dateObject.setMinute(valueHolder[4]);
    dateObject.setSecond(valueHolder[5]);

    return inputStream ;

}
string replaceSpace(string& sample)
{
        for(unsigned i =0; i < sample.size(); i++)
        {
            if(sample[i] == ' ')
            {
                sample[i] = '-';
            }
        }
        return sample;
}
0
and the TestDemo Class: it keep failing the Operator < ...testLessThan function 

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>

#include "Date.h"

bool testConstructors(void);
bool testLessThan(void);
bool testInsertion(void);

int main{

    if (!testConstructors()) {
       cerr << "Constructor and\\or accessors failed check\n";
       return EXIT_FAILURE;
    }

    if (!testLessThan()) {
       cerr << "Operator < failed check\n";
       return EXIT_FAILURE;
    }

    if (!testInsertion()) {
       cerr << "Operator >> failed check\n";
       return EXIT_FAILURE;
    }

    cout << "All tests passed\n";

    return EXIT_SUCCESS;
}

bool testInsertion(void) {

    stringstream s;
    s << "8/17/1976 1:2:3\n";

    Date d;
    s >> d;

    if (d.getMonth() == 8 &&
        d.getDay() == 17 &&
        d.getYear() == 1976 &&
        d.getHour() == 1 &&
        d.getMinute() == 2 &&
        d.getSecond() == 3) {

        return true;   
    }
    else {
        return false;
    }
}

bool testConstructors(void) {

    Date d(8, 17, 1976, 1, 2, 3);
    Date e;

    if (d.getMonth() == 8 &&
        d.getDay() == 17 &&
        d.getYear() == 1976 &&
        d.getHour() == 1 &&
        d.getMinute() == 2 &&
        d.getSecond() == 3 &&
        e.getMonth() == 0 &&
        e.getDay() == 0 &&
        e.getYear() == 0 &&
        e.getHour() == 0 &&
        e.getMinute() == 0 &&
        e.getSecond() == 0) {

        return true;   
    }
    else {
        return false;
    }
}

bool testLessThan(void) {

    Date d1(1, 1, 2004, 1, 1, 1);
    Date d2(2, 1, 2004, 1, 1, 1);    
    Date d3(1, 2, 2004, 1, 1, 1);    
    Date d4(1, 1, 2005, 1, 1, 1);
    Date d5(1, 1, 2004, 2, 1, 1);
    Date d6(1, 1, 2004, 1, 2, 1);        
    Date d7(1, 1, 2004, 1, 1, 2);            

    if ((d1 < d2) &&
        (d1 < d3) &&
        (d1 < d4) &&
        (d1 < d5) &&
        (d1 < d6) &&
        (d1 < d7) &&
        !(d2 < d1) &&
        !(d3 < d1) &&
        !(d4 < d1) &&
        !(d5 < d1) &&
        !(d6 < d1) &&
        !(d7 < d1) &&
        (d3 < d2) &&
        (d2 < d4) &&
        !(d4 < d2) &&
        (d7 < d6) &&
        (d6 < d5) &&
        !(d5 < d7)) {
        return true;
    }
    else {
        return false;
    }
}

Thank Everyone in Advance For your Time ... 
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.