sort a dynamic array

Hello Guys,

Before you read the next line know that I do have code :D
instead of explaining what my assignment is heres the link:http://www.ecst.csuchico.edu/~tyson/classes/211.s11/p2.html

I have the code right where I can instert the date/activity and it will print it out, but it is unordered, I can't for the life of me figure out how to organize it. The array I am creating has values for ints chars and strings, and i only need to sort it by looking at the int, but I do not know where to start. Thank you! heres the code separated into 3 files. event.cpp is my main.

event.cpp;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

#include "event.h"


Event::Event(int month, int day, int year, string activity)
{
    m_month = month;
    m_day = day;
    m_year = year;
    m_activity = activity;
	
}
void Event::print()
{
	cout <<m_month<< "/"<<m_day << "/" << m_year <<m_activity<< endl;
}




event.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef EVENT_H
#define EVENT_H

class Event
{
    public:
      Event(int month, int day, int year, string activity);
        void print();
    private:
        int m_month;
        int m_day;
	int m_year;
	string m_activity;
};

#endif 





calendar.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 #include <iostream>
#include <string>

using namespace std;
#include "event.h"
int main()
{
    const int MAX = 100;
    int month, day, year;
    char slash;
    int last_index;

    Event *values[MAX];
    int size = 0;
   
    while (cin >> month>>slash>>day>>slash>>year)
    {
	
	string activity;
        getline(cin, activity);
	    
       values[size] = new Event(month, day,year,activity);
	size=size+1;
	
	// cout<<size<<endl;   
	

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


Since you have a class, you can make an operator <, and then just use std::sort().

If you can't, I'd still suggest to make an operator <, but you would have to make a sort algorithm yourself (there are plenty of simple examples out there on wikipedia etc if you need to see some).
Thanks for your awesomely fast response time and pushing me in the right direction!! :DD
I'm just having problems accessing the ints from the array
Topic archived. No new replies allowed.