I have a new project I'm working on, and I'm stuck. Notice in my code below that I originally had return dayInMonth;, but commented it out as it was returning the year as 27 in the array and the month and dayInMonth both as 1. My year and month should not change, and the dayInMonth is what should become 27. The weirdest past is, my d2 is working correctly after coming from there, but then when I cout the info, it says that it is January of the year 27. Haha Anyway, as you can tell, I've still got some work to do, and any other pointers with how I should code the else porition on my operator + would also be appreciated. If someone could just help me figure out what I need to do differently on those, I could continue on. I'm really confused, although I assume it has to do with what or how I'm returning..Thanks
Here are my files so far....
header:
// **********************************
//DateArr.h
// **********************************
#include <string.h>
#include <iostream>
using namespace std;
class Date
{
int year;
int month;
int dayInMonth;
int NumDaysInMonth(int m, int y);
public:
// Month should be 1, 2, ... 12
// d should be 1, 2, ... 31
// Depending on the month and year,
// Throw exceptions if bad parameters come in.
Date (int y=2000, int m=1, int d=1);
Date operator + (int days);
friend ostream& operator << (ostream& s, Date & d);
};
class DateArr {
private:
int size;
Date *arr;
public:
DateArr(int size=10);
~DateArr();
Date & operator[] (int pos);
void resize(int newSize);
};
main:
// **************************************
// main.cpp
// **************************************
#include <iostream>
#include <fstream>
using namespace std;
#include "DateArr.h"
int main()
{
DateArr dates(3);
Date d1(2006, 11, 11);
Date d2(2005, 12, 4);
Date d3(1999, 8, 29);
dates[0] = d1;
dates[1] = d2 + 23;
dates[2] = d3 + 100;
for (int i=0; i < 3; i++)
{
cout << "i=" << i << " value=" << dates[i] << endl;
}
dates.resize(5);
for (int i=0; i < 5; i++)
{
cout << "i=" << i << " resized value=" << dates[i] << endl;
}
// Try out some error cases
try
{
Date derr1(1998, 4, 31);
}
catch (char * err)
{
cout << "Test1: error: " << err << endl;
}
try
{
Date derr1(2000, 2, 28);
cout << "2000 should not be a leap year" << endl;
Date derr2(2000, 2, 29);
}
catch (char * err)
{
cout << "Test2 error: " << err << endl;
}
return 0;
}
and my implementation file:
#include <iostream>
#include <fstream>
using namespace std;
#include "DateArr.h"
int Date::NumDaysInMonth(int m, int y)
{
// Assuming m = 1, 2, ... 12
int months[] = {31,28,31,30,31,30,31,31,30,31,30,31};
if (m != 2) return months[m-1];
if ((y % 4 == 0) && (y % 100 != 0))
return 29; // Leap year
return 28;
}
Date::Date(int y, int m, int d)
{
year = y;
month = m;
dayInMonth = d;
}
Date Date::operator + (int days)
{
if ((dayInMonth + days) < NumDaysInMonth(this->month, this->year))
{
dayInMonth += days;
return this->year, this->month, dayInMonth;
}
else
{
cout << "Must change this so it accounts for overflow of month and goes into next month\n";
return 0;
}
}
ostream& operator << (ostream& s, Date & d)
{
s << d.month << "-" << d.year;
return s;
}
DateArr::DateArr(int s)
{
size = s;
arr = new Date[s];
}
DateArr::~DateArr()
{
delete [] arr;
}
Date & DateArr::operator[] (int pos)
{
if (pos >= 0 && pos < size)
{
return arr[pos];
}
//cout << "array reference out of bounds: " << pos << endl;
//cout << "Returning first array entry\n";
cout << "Huh?\n";
return arr[0];
}
void DateArr::resize(int newSize)
{
}