What I have is a very crude linked list..THe problem for me is that I am getting a memory leak in the constructor or main. I think it is the constructor. I have not yet deleted the eventName varaible that I have allocated memory for.
Could someone help please? :/ (This is not a homework question)
class Event
{
private:
char * eventName
;
string userEvent;
struct node
{
node();
node * nextByName;
const char * eventName;
};
node * headByName;
public:
Event(const char * eventName, const Date &myDate);
Event();
virtual ~Event();
void insert(const char * eventName, const Date &myDate, const Time &myTime);
void setEvent();
const char * const getEvent() const { return userEvent.c_str(); };
void displayByName(ostream& out) const;
};
Event::Event(const char * eventName, const Date &myDate) : eventName(new char[strlen(eventName)+1]),
headByName(NULL),
userEvent("")
{
if (eventName)
{
size_t length = strlen(eventName) +1;
strcpy_s(this->eventName, length, eventName);
}
else eventName = NULL;
}
Event::Event() : eventName(NULL), userEvent(NULL), headByName(NULL)
{
}
Event::~Event()
{
node * temp_node = NULL;
node * current_node = headByName;
while ( current_node )
{
temp_node = current_node->nextByName;
delete current_node;
current_node = temp_node;
}
}
void Event::insert(const char * eventName, const Date &myDate, const Time &myTime)
// when we insert we dont care about the time, just the name and the date
{
node * current_node = new node();
if ( headByName == NULL )
{
headByName = current_node;
headByName->eventName = eventName;
}
else
{
node * search_node = headByName;
node * prev_node = NULL;
while ( search_node != NULL )
{
prev_node = search_node;
search_node = search_node->nextByName;
}
if ( NULL == prev_node )
{
headByName = current_node;
}
else
{
prev_node->nextByName = current_node;
}
current_node->nextByName = search_node;
current_node->eventName = eventName ;
}
}
void Event::displayByName(ostream& out) const
{
cout << "Scheduled Events are: " << endl << endl;
node * current_node = headByName;
while ( current_node )
{
(char*)eventName = (char*)current_node->eventName;
out << eventName << endl;
current_node = current_node->nextByName;
}
}
Event::node::node() : nextByName(NULL), eventName(NULL)
{
}
void Event::setEvent()
{
cout << "\n\nEnter a new event! ";
cin.getline((char*)userEvent.c_str(), 256);
size_t length = strlen(userEvent.c_str()) +1;
strcpy_s((char*)this->userEvent.c_str(), length, userEvent.c_str());
}
/*********************************************************************************
**********************************************************************************/
int main()
{
Date * dPtr = new Date("March", 21, 2010); // instaintiate our Date class object by allocating default date paramateres.
Event * ePtr = new Event("First Day of Spring", *dPtr);
Time * tPtr = new Time(10,12,"PM");
cout << "default Time is: " << tPtr << endl;
cout << "default Date is: " << dPtr << endl;
ePtr->insert("First Day of Spring",*dPtr, *tPtr);
ePtr->insert("Valentines Day", Date("February",14,2010), *tPtr);
ePtr->insert("New Years Day", Date("Janurary",1,2011), *tPtr);
ePtr->insert("St. Patricks Day", Date("March",17,2010), *tPtr);
ePtr->displayByName(cout);
ePtr->setEvent();
const char * const theEvent = ePtr->getEvent();
dPtr->setDate();
ePtr->insert(theEvent, *dPtr, *tPtr);
tPtr->setTime();
cout << "Your event: " << theEvent << " is scheduled for: " << endl
<< dPtr << "at" << tPtr;
ePtr->displayByName(cout);
delete tPtr;
delete dPtr;
delete ePtr;
cin.ignore();
return 0;
}