Help with this program?

A society keeps a list of members and uses a program to store the list.For each member, the following information is recorded

membership number
surname
the first name
the date that the person joined the society
whether or not they are still a member
The program is required to offer three options
1) Enter the details of the member(either current or past member).Members may be entered in any order.You can assume the membership number is available from some existing list so just needs to be entered into the program along with other data.

2) Prompt for the current date and produce as output a list of people who have been a member for at least 10years(and still a member currently)The list should be in order of date joined and split up according to how long they have been a member(50+years,40+years etc)If there is more than one person who joined on the same day, they should be given in alphabetical order of surname and if they have same surname, first name.The list should produce by sorting the set of members into the order required.

3)Prompt for membership number and mark the person's membership as not current.

Output should look something like this

Long-standing members at 20/02/2012

50+years
mem no date joined name
432 21/07/1963 Xerxes Smith
3103 20/02/1968 Aloysius Baker

40+years
mem no. date joined name
4934 21/02/1968 Hermione Turner
0123 08/06/1975 Bartholomew Wright
1498 08/06/1975 Ermintrude Wright

30+years
How should I approach this program?

Should I create template for storing 3 types of data or multidimensional array or by creating objects from classes?
Yes.

One member is logically one object that holds all information about that member. Those objects are easy to store in a container. You would have an awful mess to handle, if you would have info in five containers.

I'm not sure what you mean by "template" and how that differs from a "class".

Start by defining class Member (or struct Member -- same thing with different accessibility).
That means should I have an array of person objects each storing details of person? and how do I sort the list according to dates?
Last edited on
How does one sort? With std::sort, of course:
http://www.cplusplus.com/reference/algorithm/sort/

Note that you can give the sort an functor (object, function, or lambda) to define the relation that determines the order.

1
2
3
4
5
6
std::vector<Member> foo;

// sort by date, ascending
std:.sort( foo.begin(), foo.end(), [](const Member& lhs, const Member& rhs)
    { return lhs.date() < rhs.date(); }
  );

how you write the lhs.date() < rhs.date() in practise is up to you.

The Member could use struct tm from <ctime> for its date field.
http://www.cplusplus.com/reference/ctime/tm/
http://www.cplusplus.com/reference/ctime/strftime/
Last edited on
Thank you for your answer but I cannot use vector to do this problem.I have to use arrays and there is no sort function for arrays.
You still can use std::sort with arrays.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <algorithm>

int main()
{
  int numbers[5] = {5, 3 ,2, 1, 4};

  std::sort(numbers, numbers + 5);

  for (int i : numbers)
    std::cout << i << " ";
}
I have to use arrays and there is no sort function for arrays.


How does one sort an array? With std::sort, of course:
http://www.cplusplus.com/reference/algorithm/sort/

Note that you can give the sort an functor (object, function, or lambda) to define the relation that determines the order.

1
2
3
4
5
6
Member foo[ARRAY_SIZE];

// sort by date, ascending
std::sort(foo, foo + ARRAY_SIZE, [](const Member& lhs, const Member& rhs)
    { return lhs.date() < rhs.date(); }
  );


how you write the lhs.date() < rhs.date() in practise is up to you.

The Member could use struct tm from <ctime> for its date field.
http://www.cplusplus.com/reference/ctime/tm/
http://www.cplusplus.com/reference/ctime/strftime/
Last edited on
Topic archived. No new replies allowed.