Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Selection of people born in given year

Posted on 2003-10-25
3
Medium Priority
?
307 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 375 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

Industry Leaders: 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

604 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