This is a fairly easy homework, but I am new to c++ and would like some help. (to my teacher, if you happen to read this, I'm not trying to cheat, but get help to better understand the concepts in this assignment.)

An overview:
"Read an unordered collection of calendar entries (each containing a month, day, year, and a string describing the activity).
Store all the entries until the end of input has been reached.
Print all the entries sorted by date.

For example, given the following input (this is the required input format, see below):
8/26/2008 Classes start
7/14/2008 Register for classes
9/1/2008 No school -- Labor Day
8/27/2008 First 211 lecture
8/27/2008 Purchase textbooks

Would be printed ordered by date:
7/14/2008 Register for classes
8/26/2008 Classes start
8/27/2008 First 211 lecture
8/27/2008 Purchase textbooks
9/1/2008 No school -- Labor Day

Notice that if two entries are on the same date they are printed in the order in which they appear in the input."

I have made 3 classes, calendar.cpp, event.cpp, and event.h

//calendar.cpp
#include <iostream>
#include "event.h"
using namespace std;

bool done=false;
const int MAX=100;
Event *events[MAX];
int size=0;

int main(){

int month, day, year;
char unused;
string activity;

//& is the reference operator and can be read as "address of"
//* is the dereference operator and can be read as "value pointed by"

while(cin>>month){
	if(size==100){
		cerr<<"Too many events specified.\nThe system can only handle 100 events.\nGiving up...\n";
		goto end;
	}
	cin>>unused;
	cin>>day;
	cin>>unused;
	cin>>year;
	getline(cin, activity);
	
	events[size]=new Event(month,day,year,activity);
	for(int i=size;size>0;i--)

/*
		if(*events[i-1].compare(month,day,year))
			*Event temp=*events[i-1];
			events[i-1]=*events[i];
			events[i]=temp;
		else
			break;
	}*/

	cout<<events[size]<<endl;
	size++;
}



//cout<<*events[size]<<endl;

end:
return 0;
}
#include <iostream>
using namespace std;

#include "event.h"

Event::Event(int month,int day,int year,string txt){
	int emonth=month;
	int eday=day;
	int eyear=year;
	string etxt=txt;
}
Event::~Event(){
}
void Event::print(){
	cout<<emonth<<"/"<<eday<<"/"<<eyear<<" "<<etxt<<endl;
}
bool Event::compare(int month,int day,int year){  //true=earlier
	if(year<=eyear)
		if(month<=emonth)
			if(day<eday)
				return true;
	return false;
}
//event.h
#ifndef EVENT_H
#define EVENT_H

#include <string>
#include <iostream>
using namespace std;

class Event{
	public:
		Event(int month,int day,int year,string txt);
		~Event();
		void print();
		bool compare(int month,int day,int year);
	private:
		int emonth;
		int eday;
		int eyear;
		string etxt;
};qq
#endif

Ok, now for my problem: As I get input for each event, I should compare them to the previous one, and switch their places in the pointer array if necessary; repeat if necessary [How do I use a pointer to refer to the Event so I can compare it to the current Event? When I try to use the commented out segment, it gives me an error]. I make an array of pointers to each event object . Once this is done, I'll use a for loop and the Event's print function to print the array [again, my problem here is how to use the pointers to call on the Event's print function].

Thanks in advance!

Are you aware this is making an array of pointers? Event *events[MAX]; I think it would be best for you to review the basics of pointers and dynamic memory allocation, you leak memory for the Event you allocated.

Some better style may help you detect errors, by initializing the array of pointers to NULL or nullptr, you will receive the dreaded dereferencing null pointer exception at runtime when trying to use an un-init'd pointer or a deleted one--but you will know what it means.

Simple guidlines:

//Initialize to NULL/nullptr
char *buffer = NULL;

buffer = new char[1024];

//Always delete the memory when finished with it.
delete [] buffer;

//Followed by setting pointer to NULL.
buffer = NULL;

The array is supposed to be pointers. Exactly how do I leak memory? I'm confused... is it because I don't immediately allocate memory, so the program assumes it is null?

while(cin>>month){
	if(size==100){
		cerr<<"Too many events specified.\nThe system can only handle 100 events.\nGiving up...\n";
		goto end;
	}
	cin>>slash;
	cin>>day;
	cin>>slash;
	cin>>year;
	getline(cin, activity);
	events[size]=new Event(month,day,year,activity);

	for(int i=size;size>0;i--){
		if(events[i-1]->compare(month,day,year)){
			swap events[i],event[i-1];
		}
		else{
			break;
		}
	}

	cout<<events[size]<<"	"<<size<<endl;
	size++;
}

for(int i=0;i<=size;i++)
events[i]->print();

end:
return 0;
}

The code has been fixed a bit. The program works until the compare and print functions, which fail due to a segmentation fault. I'm not entirely sure how or why this happens, or what causes it. Does it come from the event.h, event.cpp, or the calendar.cpp file (or a combination of the 3)?

The seg fault (in my honest opinion) is happening because you are trying to use a pointer that points to either, 1. garbage, or 2. nothing. Better to point to nothing.

We programmers have modern IDEs to help us debug our programs, and for the most part we probably use them.

If you're using Visual Studio you can hit "F5" to start debugging your program--but you will want to change the project to "debug" mode.

You can set breakpoints in which you want the execution to be halted so that you can examine the contents of variables that are in scope.

If you don't debug in "debug" mode you may not be able to examine the contents of variables while it's executing.

Almost forgot, but if you allocate ANYTHING with the keyword "new" it must be deleted with the keyword "delete". Maybe I've overlooked where you deleted what you allocated, but I didn't see it anywhere.

Smart pointers are almost always preferable to keeping track of an array of pointers.
http://www.boost.org/doc/libs/1_45_0/libs/smart_ptr/shared_ptr.htm

If you accidentally call "delete" on a pointer that points to some random memory address (which they do by default unless you initialize it to NULL) it will probably cause a seg-fault.

Thus the guidelines above are suggested (by me).

events[size]=new Event(month,day,year,activity);
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.