So I am creating a program for a class that reads in from a file the appointments. The file has this format
Date,Subject,Start Time,End Time,Location,,

The interesting thing about the file is that it does not contain the same appointment (same name, location, and time) more than once. For example:
the line
10/2, sub, 1:00 PM, 2:00 PM, loc, MWF, 3
represents a 3 days appointment (the last number in the line). When my program reads a line like this, it will add an appointment for the date 10/2 and if that date is a Monday, the next date to add will be 10/4 (Wednesday), and so on.

I'll put up parts of my class declaration so that I can explain my problem better

class Year
{
  int count;
  LinkedList <Day> days;
};

class Day
{
  short day;
  short month;
  Appointment *appts[8];
  short apptCount;
};

class Appointment
{
  Time startTime;
  Time endTime;
  char *subject;
  char *location;
};

class WeeklyAppointment : public Appointment
{  
  char daysOfWeek[8];
  int count;
};

As you can see, my day class is composed of an array of Appointments pointers. Whenever I read a line from the file that I described above, I will need to create a WeeklyAppointment.

My issue is that, I can only have one WeeklyAppointment object because if I ever delete an appointment day, I will not to decrement the count in the WeeklyAppointment class. It will be a lot easier to decrement only one WeeklyAppointment object instead of all the separate WeeklyAppointment that I will need. I have been staring at my code for a whole day now trying to figure out how to make my *appts[] pointers point to that single WeeklyAppointment object.

Here is part of the codes that matters:

In my year.cpp:

void Year::readWeeklyAppt(int month, int day, char apptday[], int n, int pos)
{
  int i, j, size = strlen(apptday), d = ((month - 1) * 31 + day), m = 1;
  char nextd;
  WeeklyAppointment week(apptday, n); //apptday is the days read in file (ie MWF)
  days[pos].readWeeklyAppt(week); ...

The readWeeklyAppt calls my function in my day class

void Day::readWeeklyAppt(WeeklyAppointment &w)
{
  appts[apptCount] = new WeeklyAppointment(w);
  *appts[apptCount++] = w;
} // readWeeklyAppt()


/* the function above calls this copy constructor in WeeklyAppointment */

WeeklyAppointment::WeeklyAppointment(WeeklyAppointment &w)
{
  count = w.count;
  strcpy(daysOfWeek, w.daysOfWeek);

} //WeeklyAppointment()

My approach was that I create a WeeklyAppointment object inside year.cpp because that is where I will be reading the files. Then I will pass that object into a function in day and set one pointer of appts[] to point at that object.

The *appts[apptCount++] = w; line calls my assignment operator in Appointment copies the base class members of Appointment (location, subject, time).

These codes worked out pretty well when I printed everything out, but the problem was when I deleted a day, it did not decrement the count in the WeeklyAppointment object. This means that I have created separate WeeklyAppointment object that each of my appt[] pointers are pointing to. I have a general idea of what I am doing wrong, but I do not know how to get back into the right direction.

Any helpful tips or advice will be help. Please let me know if there are any parts that does not make sense. Thanks

My professor wrote down this code on the board as a hint which I believe was addressing the problem that I am having; however, I am having a little trouble understanding what he is doing. Wasn't in the state to ask him about it =/

WeeklyAppointment appt = new WeeklyAppointment(appts[apptCount-1] , ptr)
delete appts[apptCount -1];
appts[apptCount - 1] = appt;

I might have also written down the code wrong because he was constantly updating parts of it.

So I rewrote some codes

void Day::readWeeklyAppt(WeeklyAppointment &w)
{
  appts[apptCount++] = new WeeklyAppointment(w);
} // readWeeklyAppt()

WeeklyAppointment::WeeklyAppointment(WeeklyAppointment &w)
{
  count = w.count;
  strcpy(daysOfWeek, w.daysOfWeek);
  Appointment::readWeeklyAppt(w);

} //WeeklyAppointment()


void Appointment::readWeeklyAppt(const Appointment &a)
{
  if(subject)
    delete [] subject;

  if(location)
    delete [] location;

  startTime = a.startTime;
  endTime = a.endTime;
  if(a.subject)
  {
    subject = new char[strlen(a.subject) + 1];
    strcpy(subject, a.subject);
  }
  if(a.location)
  {
    location = new char[strlen(a.location) + 1];
    strcpy(location, a.location);
  }
} //readWeeklyAppt

I still cannot remove a date and decrement the count inside the WeeklyAppointment object. This suggests that I am still not pointing my appts[] pointer to the same object. Instead I am most likely creating a new WeeklyAppointment object to point my appts[] to. So before I delete my day, I am actually decrementing the WeeklyAppointment count for that unique object instead of all of them.

This is my destructor for my Day class to delete appointments

Day::~Day()
{
  for(int j = 0; j < apptCount; j++)
  {
    appts[j]->decrementCount();
    delete  appts[j];
  }
} // ~Day()

Any help? =/ Thanks

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.