Solved

Selection of people born in given year

Posted on 2003-10-25
3
276 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now