Hi,

This program supposed to ask the user to enter the names and the ID numbers and
the number of children the employee have,

the search shoud work like this


taylar 845 3
john 123 2
sara 526 2


**search by name**

the user should enter the employee name he's searching for
if he entered sara
sara 526 2

**search by number of children**

if the user enterd 2
john 123 2
sara 526 2

**sort by name**

the names sould sorted in alphapatical order
john 123 2
sara 526 2
taylar 845 3


************************************

#include <iostream>
#include <cstring>
using namespace::std;
int main ()
{
  int counter=0;
  char *s[100];
  char names[100];
  int num[3];
  char *j;
  int child[3];
  int x,n,y,s1,s2;

  cout <<"Input the information of 10 imployees: "<< endl;
  cout << "Names "<< "Number" <<" childs"<<endl;
  for ( int i=0 ; i <3 ; i++ ){

    s[i]= names;
    cin >> names ;
    cout<< "\t";
    cin >> num[i] ;
    cout << "\t";
    cin >> child[i] ;
    cout<< "\t" ;

    cout << endl;
  }

  cout << "What type of operation you are interested in: " << endl;
  cout << " 1. Search by Employee name" << endl;
  cout << " 2. Search by Number of childrean" << endl;
  cout << " 3. Sort by Name" << endl;
  cout << " 4. To Exit" << endl;

  cin >> x ;

  while ( x != 4 ) {
    if ( x == 1 ) {
      cout << "Input the employee name:" << endl;
      cin >> j ;


      for ( i=0 ; i < 3 ; i++ )
      {

        int n;
        n = strcmp( j , s[i]);
        if ( n=0)
        {
          cout << "The employee you are looking for are :" << endl;
          cout << s[i] <<"\t"<< num[i] <<"\t"<< child[i] << endl;
        }
      }
    }

    if ( x == 2 ){
      cout << "What is the number of children you are looking for:" << endl;
      cin >> n;
      cout << "The employees you are looking for are:" << endl;
      for ( i=0 ; i < 3 ; i++ ){
        if ( child[i] == n )
          cout << s[i] <<"\t"<< num[i] << "\t" << child[i] << endl;
      }
    }

    if ( x == 3 ){
      for ( i=0 ; i <3 ; i++ ){
        s1=*s[i];
        s2=*s[i+1];
        y=strcmp ( s[i] , s[i+1]);
        if ( y == 1 ) {
          *s[i] = s1;
          *s[i+1]=s2;
        }
        else {
          *s[i]=s2;
          *s[i+1]=s1;
        }
      }
      cout << "Sort by Name:" << endl;
      for ( i=0 ; i<3 ; i++ )
        cout << s[i] << "\t" << num[i] << "\t" << child[i] << endl;
    }
  }

  return 0;
}

>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

My problem with searching by employee name,
when I enter a name to search for ,I face a debug,

also if I entered
taylar
john
sara
array s should include:
s[0].....taylar
s[1].....john
s[2].....sara

but in my program it is:
s[0]....sara
s[1]....sara
s[2]....sara

it always takes the last name entered

this is my problem ,if you fix it to me then I can fix the hole program,
because my main problem only with searching by employee name .

thank you for trying to help me :)

>it always takes the last name entered
That's because you're using a pointer. When you assign name to s, s points to name. When the loop is done, all of the pointers still point to name, which contains the last name entered. To fix this, use arrays instead:

char s[10][100];
char name[100];

...

cin.width(99); // Protection from buffer overflow
cin>> name;
strcpy(s[i], name);

>when I enter a name to search for ,I face a debug
You mean you get an access violation when you try to write to an uninitialized pointer. You seem to have trouble with pointers, so I would suggest using arrays instead for any project. Leave the pointers for toy programs meant to experiment with features:

char j[100];

...

cout << "Input the employee name:" << endl;
cin.width(100);
cin >> j ;

I changed the sort by name like this

char s[10][100];
if ( x == 3 ){
      for ( i=0 ; i <3 ; i++ ){
        char s1=*s[i];
        char s2=*s[i+1];
        y=strcmp ( s[i] , s[i+1]);
        if ( y > 0 ) {
          *s[i] = s2;
          *s[i+1]=s1;
        }

<< moderator edit: added [code][/code] tags >>

but it doesn't work like what I want,

if I entered

aaa 111 1
ccc 222 2
bbb 333 3

the program should sort them like this

aaa 111 1
bbb 333 3
ccc 222 2

but it do this

aaa 111 1
bcc 222 2
cbb 333 3

How can I fix this problem ,plz ??

>How can I fix this problem ,plz ??
You fix the problem by taking my advice:

You seem to have trouble with pointers, so I would suggest using arrays instead for any project.

When comparing strings you use strcmp, you've got that part down. When copying strings, you use strcpy. Your problem is that you're dereferencing an array, which gives you the first element of the array. You're basically sorting the first character of the strings rather than the whole strings.

Thay's it, I'm facing problems with the pointers , bcz I learned it just a week before , but I should use pointers ,I have no choice :)

>but I should use pointers ,I have no choice
Well, you still have no choice but to use strcpy if you want to actually swap the strings. Here's a tip. Get it working first, then modify it to suit your requirements. You'll find that using pointers and dynamic memory isn't terribly different than just using arrays.

well, Thank u very much for trying to help me,
I think I have to learn more a bout using pointers :)
My be next time I'll be better

thanx again :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.