>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;
}
}
} :D
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401