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!