Solved

Selection of people born in given year

Posted on 2003-10-25
3
297 Views
Last Modified: 2010-04-02
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.

requests

- 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
0
Comment
Question by:xLeon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 9620352
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
0
 
LVL 1

Author Comment

by:xLeon
ID: 9663450
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++){
    cin>>name>>surname>>b_year;
    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++)
   cout<<v[i]<<endl;
  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)
      V.push_back(it->first);
  return V;
}

0
 
LVL 49

Accepted Solution

by:
DanRollins earned 125 total points
ID: 9665250
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question