Solved

Selection of people born in given year

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

706 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

16 Experts available now in Live!

Get 1:1 Help Now