Question

Hi I am working on a program that should display the latter of two dates, it needs to accept two Date structures and return the latter of them. Latter being 04/31/2014 is latter then 04/30/2014. Here is my code and I do not understand why I am getting a long weird number... Thanks for helping.

#include <iostream>
#include <iomanip>

using namespace std;

struct Date
{
    int day;
    int month;
    int year;
};

int main()
{
    void laterDate(Date, Date);
    Date one;
    Date two;
Date later;

cout << "Enter a Date starting with the day, then month, then year:\n";

cin >> one.day;
cin >> one.month;
cin >> one.year;

cout << "\n\nEnter another date in the same fashion as above:\n";

cin >> two.day;
cin >> two.month;
    cin >> two.year;

cout << "\n\nThank you, I will now tell you which date is later then the other!" <<             endl;
laterDate(one, two);

system("pause");
return 0;
}


void laterDate(Date o,Date t)
{
Date later;


if (o.year >= t.year)
    if (o.month >= t.month)
        if (o.day > t.day)
        {
            later.day= o.day;
            later.month = o.month;
            later.year = o.year;
        }
        else
        {
            later.day = t.day;
            later.month = t.month;
            later.year = t.year;
        }

        cout << later.day << "/" << later.month << "/" << later.year << endl;
}

OUTPUT Enter a Date starting with the day, then month, then year: 04 30 2014

Enter another date in the same fashion as above: 04 31 2014

Thank you, I will now tell you which date is later then the other! -858993460/-858993460/-858993460 Press any key to continue . . .

END OUTPUT

Was it helpful?

Solution 2

if (o.year > t.year || 
   (o.year >= t.year && o.month > t.month) || 
    (o.year >= t.year && o.month >= t.month &&
           o.day > t.day))
        {
            later.day= o.day;
            later.month = o.month;
            later.year = o.year;
        }
        else
        {
            later.day = t.day;
            later.month = t.month;
            later.year = t.year;
        }

in this way , else will always be called , in your implementation it is only called when the two first condition where true, resulting in the display of random bits

OTHER TIPS

You should break out the logic of comparing the dates from the code which displays the results. The standard idiom for comparing objects that can be totally ordered (such as dates) is to overload operator<.

// the intent of this function is to return true if lhs precedes rhs,
// and return false otherwise (rhs precedes lhs, or they are equal)
bool operator<(Date const& lhs, Date const& rhs)
{
    // first test the year.
    // if the years differ, there is no need to test the month or the day
    if (lhs.year < rhs.year) return true;
    if (lhs.year > rhs.year) return false;

    // years are equal, test the month
    if (lhs.month < rhs.month) return true;
    if (lhs.month > rhs.month) return false;

    // months are equal, test the day
    return lhs.day < rhs.day;
}

Then your function can be easily written like this:

void laterDate(Date const& lhs, Date const& rhs)
{
    Date const& later = (lhs < rhs) ? rhs : lhs;

    cout << later.day << "/" << later.month << "/" << later.year << endl;
}

I think I'd structure things a little differently. First, I'd make outputting a Date a friend function of the Date class and next, I'd overload the > operator for comparing Date class instances.

struct Date
{
    int day;
    int month;
    int year;
    friend std::ostream &operator<<(std::ostream& out, const Date &d) {
        return out << d.day << "/" << d.month << "/" << d.year;
    }
    bool operator>(const Date &t) const;
};

The implementation of the > operator would look like this:

bool Date::operator>(const Date &t) const
{
    if (year > t.year) return true;
    if (year < t.year) return false;
    if (month > t.month) return true;
    if (month < t.month) return false;
    return day > t.day;
}

And then your main routine would be this:

int main()
{
    Date one;
    Date two;

    std::cout << "Enter a Date starting with the day, then month, then year:\n";
    std::cin >> one.day >> one.month >> one.year;
    std::cout << "\n\nEnter another date in the same fashion as above:\n";
    std::cin >> two.day >> two.month >> two.year;
    std::cout << "\n\nThank you: " << (one > two ? one : two) 
              << " is the later date\n";
}

Note, too, that one can chain the input operator parts as shown here. In fact, I'd be inclined to declare an input operator operator>> and promote the Date struct to a full-fledged class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top