I'm currently experiencing something I'm not sure about. Here is the code
ostream& operator<<(ostream &out, Date other)
{
out << other.month << "/" << other.day << "/" << other.year;
return out;
}
istream& operator>>(istream &in, Date &other)
{
char dummy;
in >> other.month >> dummy >> other.day >> dummy >> other.year;
return in;
}
And here it is in main
//create an object
Date date1(1,1,2000);
cout << "enter a date: ";
cin >> date1;
cout << "Date is: " << date1 << endl;
date1.increment();
cout << "Date is: " << date1 << endl;
I'm getting the date and everything right, but for some reason in the day section of the date, mm/dd/yy, it is only showing the single digits number. Specifically, I get this on my compile:
enter a date: 12
10
2021
Date is: 12/0/21
Date is: 12/1/21
Here is the date.cpp file:
Date::Date()
{
month = 1, day = 1, year = 2000;
}
//default constructor. data validation to check for month, day, years
Date::Date(int mon, int days, int yer)
{
bool badDate = false;
if ((mon < 0) || (mon > 12))
{
badDate = true;
}
if ((days < 0) || (days > 30))
{
badDate = true;
}
if (yer < 0)
{
badDate = true;
}
//if all data is good, default deconstructor if no parameters
if (badDate)
{
month = 1;
day = 1;
year = 2000;
}
//else data in code
else {
month = mon;
day = days;
year = yer;
}
}
ostream& operator<<(ostream &out, Date other)
{
out << other.month << "/" << other.day << "/" << other.year;
return out;
}
istream& operator>>(istream &in, Date &other)
{
char dummy;
in >> other.month >> dummy >> other.day >> dummy >> other.year;
return in;
}