Selection of people born in given year

xLeon used Ask the Experts™
Hi can you advise me?

I should create a program which is called from the command line with one parameter. This parameter will be the number representing a year. The input is a list of records with 3 attributes just in this order:name, surname and birth-year. Output is a sorted list of persons born in year specified by command-line parameter.


- For the purposes of selection and sorting of the list create a function select_people and   its definition place to the   separated module (unit) born.C.
  The funtion must have this prototype vector<string> select_people(multimap<string, int> &people, int year);
  Parameter people is a reference on the indexed container (associated array), which   associates "surname name" of type string with the birth-year (type int) for certain person.
  Parameter year determines the year, according to which will be performed the selection of   corresponding records from array people.
  Function returns a vector (type string), which containes list of persons in alphabetical   order (first surname)
- program will end after the input is read to the end (in Unix shortcut ctrl-D)
- Main module will read the individual records into associated array which will be then given to the function select_people. And then it will write output of this function.

for the insertion of records into the container multimap you can use the member function insert(pair<string, int>(string, int)).

thank you for your help
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Author of the Year 2009

The Experts here will not do your homework and will get suspended if they do.  http:/help/guidelines.jsp#22

You need to get started on the progject and do as much as you can, then you can ask specific questions when you run into a problem.

-- Dan


I understand I should work it out myself, so
I was trying to run into the problem and I wrote this program with subtle differences which are easy to modify, but my program doesn't write out the desirable output and I can't figure out why. I'm not conversant with stl yet.

#include <string>
#include <map>
#include <vector>
#include <stdlib.h>

using namespace std;
vector<string> select_people(multimap<string, int> &people, int rok);

int main(int argc, char *argv[])
  string name;
  string surname;
  int b_year;
  long given_year;

  multimap<string, int> apole;
  vector<string> v;

  cout<<"Write a name, surname and born-year\n";
  for (int i=0;i<=3;i++){
    apole.insert(pair<string, int>(surname+" "+name,b_year));
  given_year = strtol(argv[1]);
  v = select_people(apole, (int) given_year);
  for (int i=0; i<v.size(); i++)
  return 0;

vector<string> select_people(multimap<string, int> &people, int year)
  vector<string> V;
  typedef multimap<string, int>::iterator multit;

  for (multit it = people.begin(); it != people.end(); ++it)
    if ((it->second) == year)
  return V;

Author of the Year 2009
In my library, the strtol function requires two more parmaters.  All you wan to do is convert a sting from the command line into an integer, so I suggest using

  long given_year= atoi( argv[1] );

Other than that, it works exactly right, as far as I can tell.  Here is the output of a trial run:

Write a name, surname and born-year
john smith 2000
bill jones 1000
tom thumb 2000
dan rollins 3000
smith john
thumb tom

The "given_year" was 2000, and the last two lines show the name of the two people who were associated with that year.

What do you need help with?

-- Dan

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial