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)
{
}

>>and any other pointers with how I should code the else porition on my operator +
The easiest way is to use the mktime function in time.h. Populate a struct tm structure with the year, month, and day + days_to_increment, then call mktime() to normalize the data. Upon return the tm structure will be corrected.

If you want to hand roll it yourself you could try something along these lines for the + operator body:

totalDays = dayInMonth + days;
if totalDays <= numDaysInMonth(month, year)
   dayInMonth = totalDays;
else
  while totalDays > numDaysInMonth(month, year)
    totalDays -= numDaysInMonth(month, year);
    ++month;
    if(month == 13)
      month = 1;
      ++year;

  dayInMonth = totalDays;

I wanted to write it all out instead of calling directly to time.h. Thank you for your input though.

And,
LERNER
your code looks as if it's what I need, but it still doesn't solve my problem of returning. I must return something still. What do I return??

Thanks

Oh wait, can I just return a pointer to this (*this)??

Now, what about this part?....:

What should I put in my "else" part?

And then I don't quite know what the resize is all about. What am I resizing? How do I do that??

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";
	return arr[0];
}



void DateArr::resize(int newSize)
{
}

You're using a unary + operator as a class method so I'd return a dereferenced this pointer. If you made the + operator a friend function and passed it two Dates instead of one, then I'd declare a third Date within the body of the function, assign the sum to it, and return it.

K, great thanks...Any input on my last post?

what about this part?....:

Do you even need an else part? It looks like that could work as is.

Resize probably means you started with an array of size 3 and you want to change it to an array of size 1 or an array of size 47 or something. So you will probably want to declare a temporary array of the current size and copy the information in the current array to the temporary array. Then delete the memory for the current array manually since it was declared with new. Follow that by assigning the pointer returned by a call to new creating an array of desired size to the current array/pointer. Then recopy the information in the temporary array back into the resized array and last, but not least, delete the memory for the temporary array. Better yet, give up the drudgery and start using vectors if you can. It will do all this leg work for you. Well, it won't bounds check for you, but it will handle memory for your.

The way I interpret what you replied with is this:

void DateArr::resize(int newSize)
{
	size = newSize;
	Date* newTempArr;
	newTempArr = new Date[newSize];
	for (int i=0; i <= newSize; i++)
		newTempArr[i] = arr[i];

}

However, then my 4th and 5th locations spit out memory addresses instead of dates. What am i doing wrong there?!? Thanks!

That loop should copy the original number of entries, not the new number, and use < operator, not <=.

void DateArr::resize(int newSize)
{
	Date* newTempArr;
	newTempArr = new Date[newSize];
	for (int i=0; i < size; i++)
		newTempArr[i] = arr[i];
	size = newSize;
                delete[] arr;
                arr = newTempArr;

}

ahh yes, i suppose it would work better if I had set

arr = newTempArr;

Thanks. That results in the last two locations both equalling "1/1/2000". Is that what I want there? I'm a little confused.

>>Is that what I want there?

Probably not. The problem is somewhere else. Remember that after executing that function the last one or two locations in the new array do not contain valid data. How many elemenets are invalid depends on the difference between the original size and the new size.

Ok, great thanks for all the help!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.