Month = 1, Day = 1
Month = 1, Day = 1
The dates are the same!
Could someone point me in the direction of the problem. Please don't just give me the fixed code. I want to understand the problem and the solution.
#include <iostream>
using namespace std;
class DayOfYear
{
public:
DayOfYear(int theMonth, int theDay);
DayOfYear();
void setMonth(int newMonth);
void setDay(int newDay);
int getMonth();
int getDay();
friend istream& operator >>(istream& ins, DayOfYear date);
friend ostream& operator <<(ostream& outs, const DayOfYear date);
friend bool operator ==(DayOfYear date1, DayOfYear date2);
private:
int month;
int day;
};
int main()
{
DayOfYear date_1, date_2;
cout << "Enter the Day and Month separated by a space." << endl;
cin >> date_1;
cout << "Enter the Day and Month separated by a space." << endl;
cin >> date_2;
cout << "Date 1:" << endl;
cout << date_1;
cout << "Date 2:" << endl;
cout << date_2;
if(date_1 == date_2){
cout << "The dates are the same!" << endl;
}else{
cout << "The dates are different!" << endl;
}
return 0;
}
bool operator ==(DayOfYear date1, DayOfYear date2)
{
return ((date1.month == date2.month) &&
(date1.day == date2.day));
}
DayOfYear::DayOfYear(int theMonth, int theDay)
{
if ((theMonth >= 1) && (theMonth <= 12) && (theDay >= 1) && (theDay <= 31))
{
month = theMonth;
day = theDay;
}
else
{
cout << "Error! Invalid initial value for month and / or day." << endl;
cout << "Setting both values to 1." << endl;
day = 1;
month = 1;
}
}
DayOfYear::DayOfYear()
{
month = 1;
day = 1;
}
void DayOfYear::setMonth(int newMonth)
{
if ((newMonth >= 1) && (newMonth <= 12))
month = newMonth;
else
cout << "Error! The month must be between 1 and 12." << endl;
}
void DayOfYear::setDay(int newDay)
{
if ((newDay >= 1) && (newDay <= 31))
day = newDay;
else
cout << "Error! The month must be between 1 and 31." << endl;
}
int DayOfYear::getMonth()
{
return month;
}
int DayOfYear::getDay()
{
return day;
}
istream& operator >>(istream& ins, DayOfYear date)
{
int tempMonth, tempDay;
ins >> tempMonth;
date.setMonth(tempMonth);
ins >> tempDay;
date.setDay(tempDay);
return ins;
}
ostream& operator <<(ostream& outs, const DayOfYear date)
{
outs << "month = " << date.month << ", day = " << date.day << endl;
return outs;
}

New Topic/Question
Reply




MultiQuote



|