#include <iostream>
using namespace std;
class Date {
private:
int day, month, year;
public:
Date(int, int, int);
Date();
friend istream& operator >> (istream&, Date&);
friend ostream& operator << (ostream&, const Date&);
friend bool operator !=(const Date&, const Date&);
friend bool operator ==(const Date&, const Date&);
friend bool operator <(const Date&, const Date&);
};
Date::Date(int d, int m, int y) {
month = m;
day = d;
year = y;
}
Date::Date() {
day = 0;
month = 0;
year = 0;
}
istream& operator >> (istream& dates, Date& t) {
cin >> t.day;
cout << "Day: " << t.day << endl;
cin >> t.month;
cout << "Month: " << t.month << endl;
cin >> t.year;
cout << "Year: " << t.year << endl;
cout << endl;
return dates;
}
ostream& operator << (ostream& dates, const Date& t) {
dates << t.day << "/" << t.month << "/" << t.year << endl;
return dates;
}
bool operator <(const Date& date, const Date& dat) {
return (date.year < dat.year);
return (date.month < dat.month);
return (date.day < dat.day);
}
bool operator == (const Date& date, const Date& dat) {
return (date.year == dat.year);
}
bool operator !=(const Date& date, const Date& dat) {
return (date.year != dat.year);
return (date.month != dat.month);
return (date.day != dat.day);
}
int main()
{
Date dates(0, 0, 0);
Date dates2(0, 0, 0);
cout << "Please enter a date: " << endl;
cin >> dates;
cout << "Your date is: " << dates;
cout << endl;
cout << "Please enter a date: " << endl;
cin >> dates2;
cout << "Your date is: " << dates2;
cout << endl;
if (dates != dates2) {
cout << "Your date is not the same " << endl;
}
else {
cout << "Your date is the same " << endl;
}
cout << endl;
if (dates < dates2) {
cout << "Person 2 is older ";
}
else if (dates == dates2) {
cout << "They we're born on the same date. ";
}
else {
cout << "Person 1 is younger ";
}
return 0;
}
I'm currently having issues with my if-statement logic, when I input something like 11/26/2001 for my first date and 11/23/2001, it'll say that it is the same date and that they we're born on the same date. My goal is for my if-statement to go through Day-Month-Year and to check if they are the same and if all of those are the same then it should output, "You we're born on the same day" and "It's the same date" but right now it's only printing out those two lines of code when only one of the variables match each other.
CodePudding user response:
bool operator <(const Date& date, const Date& dat) {
return (date.year < dat.year);
return (date.month < dat.month);
return (date.day < dat.day);
}
Only the first return
is ever used, the other two are dead code.
What you need to do here is make the returns conditional, like so:
bool operator <(const Date& date, const Date& dat) {
if (date.year == dat.year) {
if (date.month == dat.month) {
return date.day < dat.day;
}
return date.month < dat.month;
}
return date.year < dat.year;
}
CodePudding user response:
looks like the operator == code compares only the year and returns true if the year matches.
bool operator == (const Date& date, const Date& dat) {
return (date.year == dat.year);
}
try this:
return ((date.month == dat.month) && (date.day == dat.day) && (date.year ==
dat.year));