User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 423,871 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,986 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums

HELP me as soon as possible ..!

Join Date: Sep 2004
Posts: 6,306
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 456
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: HELP me as soon as possible ..!

  #2  
May 5th, 2005
>the search shoud work like this
Okay...and what does it look like? Sorry, but I'm not nearly interested enough in your problem to compile and debug your code to find out what's wrong. You'll have to tell me what your problem is to get help.

However, I am interested enough in your problem to play around with my own toy implementation:
#include <algorithm>
#include <functional>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

struct Employee {
  std::string _name;
  int _number, _children;

  bool operator<(const Employee& emp) const { return _name < emp._name; }

  friend std::ostream& operator<<(std::ostream& out, const Employee& emp)
  {
    return out<< emp._name <<' '<< emp._number <<' '<< emp._children;
  }

  friend std::istream& operator>>(std::istream& in, Employee& emp)
  {
    return in>> emp._name >> emp._number >> emp._children;
  }
};

struct by_name: std::binary_function<Employee, std::string, bool> {
  bool operator()(const Employee& a, const std::string& b) const
  {
    return a._name == b;
  }
};

struct by_children: std::binary_function<Employee, int, bool> {
  bool operator()(const Employee& a, const int b) const
  {
    return a._children == b;
  }
};

template <typename Input, typename Output, typename Pred>
void find_all_if(Input first, Input last, Output result, Pred pred)
{
  while (first != last) {
    if (pred(*first))
      *result++ = *first;
    ++first;
  }
}

void menu()
{
  std::cout<<"Choose an option: "<<'\n'
    <<" 1. Search by Employee name"<<'\n'
    <<" 2. Search by Number of childrean"<<'\n'
    <<" 3. Sort by Name"<<'\n'
    <<" 4. To Exit"<<'\n';
}

int main()
{
  std::vector<Employee> emps;
  int opt;

  std::cout<<"Enter employees (Name Number Children). EOF to stop.\n";
  std::copy(std::istream_iterator<Employee>(std::cin),
    std::istream_iterator<Employee>(), std::back_inserter(emps));
  std::cin.clear();

  while (menu(), std::cin>>opt && opt >= 1 && opt < 4) {
    std::vector<Employee> match;
    std::string name;
    int children;

    // Clean the stream
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    switch (opt) {
    case 1: // Search by name
      std::cout<<"Name to search for: ";
      std::getline(std::cin, name);

      find_all_if(emps.begin(), emps.end(), std::back_inserter(match),
        std::bind2nd(by_name(), name));
      std::copy(match.begin(), match.end(),
        std::ostream_iterator<Employee>(std::cout, "\n"));
      break;
    case 2: // Search by children
      std::cout<<"Number of children to search for: ";
      std::cin>> children;

      find_all_if(emps.begin(), emps.end(), std::back_inserter(match),
        std::bind2nd(by_children(), children));
      std::copy(match.begin(), match.end(),
        std::ostream_iterator<Employee>(std::cout, "\n"));
      break;
    case 3: // Sort and print
      std::sort(emps.begin(), emps.end());
      std::copy(emps.begin(), emps.end(),
        std::ostream_iterator<Employee>(std::cout, "\n"));
      break;
    }
  }
}
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
All times are GMT -4. The time now is 4:23 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC