HELP me as soon as possible ..!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2005
Posts: 5
Reputation: hessa is an unknown quantity at this point 
Solved Threads: 0
hessa hessa is offline Offline
Newbie Poster

HELP me as soon as possible ..!

 
0
  #1
May 5th, 2005
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


************************************
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace::std;
  4. int main ()
  5. {
  6. int counter=0;
  7. char *s[100];
  8. char names[100];
  9. int num[3];
  10. char *j;
  11. int child[3];
  12. int x,n,y,s1,s2;
  13.  
  14. cout <<"Input the information of 10 imployees: "<< endl;
  15. cout << "Names "<< "Number" <<" childs"<<endl;
  16. for ( int i=0 ; i <3 ; i++ ){
  17.  
  18. s[i]= names;
  19. cin >> names ;
  20. cout<< "\t";
  21. cin >> num[i] ;
  22. cout << "\t";
  23. cin >> child[i] ;
  24. cout<< "\t" ;
  25.  
  26. cout << endl;
  27. }
  28.  
  29. cout << "What type of operation you are interested in: " << endl;
  30. cout << " 1. Search by Employee name" << endl;
  31. cout << " 2. Search by Number of childrean" << endl;
  32. cout << " 3. Sort by Name" << endl;
  33. cout << " 4. To Exit" << endl;
  34.  
  35. cin >> x ;
  36.  
  37. while ( x != 4 ) {
  38. if ( x == 1 ) {
  39. cout << "Input the employee name:" << endl;
  40. cin >> j ;
  41.  
  42.  
  43. for ( i=0 ; i < 3 ; i++ )
  44. {
  45.  
  46. int n;
  47. n = strcmp( j , s[i]);
  48. if ( n=0)
  49. {
  50. cout << "The employee you are looking for are :" << endl;
  51. cout << s[i] <<"\t"<< num[i] <<"\t"<< child[i] << endl;
  52. }
  53. }
  54. }
  55.  
  56. if ( x == 2 ){
  57. cout << "What is the number of children you are looking for:" << endl;
  58. cin >> n;
  59. cout << "The employees you are looking for are:" << endl;
  60. for ( i=0 ; i < 3 ; i++ ){
  61. if ( child[i] == n )
  62. cout << s[i] <<"\t"<< num[i] << "\t" << child[i] << endl;
  63. }
  64. }
  65.  
  66. if ( x == 3 ){
  67. for ( i=0 ; i <3 ; i++ ){
  68. s1=*s[i];
  69. s2=*s[i+1];
  70. y=strcmp ( s[i] , s[i+1]);
  71. if ( y == 1 ) {
  72. *s[i] = s1;
  73. *s[i+1]=s2;
  74. }
  75. else {
  76. *s[i]=s2;
  77. *s[i+1]=s1;
  78. }
  79. }
  80. cout << "Sort by Name:" << endl;
  81. for ( i=0 ; i<3 ; i++ )
  82. cout << s[i] << "\t" << num[i] << "\t" << child[i] << endl;
  83. }
  84. }
  85.  
  86. return 0;
  87. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: HELP me as soon as possible ..!

 
0
  #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:
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iostream>
  4. #include <limits>
  5. #include <string>
  6. #include <vector>
  7.  
  8. struct Employee {
  9. std::string _name;
  10. int _number, _children;
  11.  
  12. bool operator<(const Employee& emp) const { return _name < emp._name; }
  13.  
  14. friend std::ostream& operator<<(std::ostream& out, const Employee& emp)
  15. {
  16. return out<< emp._name <<' '<< emp._number <<' '<< emp._children;
  17. }
  18.  
  19. friend std::istream& operator>>(std::istream& in, Employee& emp)
  20. {
  21. return in>> emp._name >> emp._number >> emp._children;
  22. }
  23. };
  24.  
  25. struct by_name: std::binary_function<Employee, std::string, bool> {
  26. bool operator()(const Employee& a, const std::string& b) const
  27. {
  28. return a._name == b;
  29. }
  30. };
  31.  
  32. struct by_children: std::binary_function<Employee, int, bool> {
  33. bool operator()(const Employee& a, const int b) const
  34. {
  35. return a._children == b;
  36. }
  37. };
  38.  
  39. template <typename Input, typename Output, typename Pred>
  40. void find_all_if(Input first, Input last, Output result, Pred pred)
  41. {
  42. while (first != last) {
  43. if (pred(*first))
  44. *result++ = *first;
  45. ++first;
  46. }
  47. }
  48.  
  49. void menu()
  50. {
  51. std::cout<<"Choose an option: "<<'\n'
  52. <<" 1. Search by Employee name"<<'\n'
  53. <<" 2. Search by Number of childrean"<<'\n'
  54. <<" 3. Sort by Name"<<'\n'
  55. <<" 4. To Exit"<<'\n';
  56. }
  57.  
  58. int main()
  59. {
  60. std::vector<Employee> emps;
  61. int opt;
  62.  
  63. std::cout<<"Enter employees (Name Number Children). EOF to stop.\n";
  64. std::copy(std::istream_iterator<Employee>(std::cin),
  65. std::istream_iterator<Employee>(), std::back_inserter(emps));
  66. std::cin.clear();
  67.  
  68. while (menu(), std::cin>>opt && opt >= 1 && opt < 4) {
  69. std::vector<Employee> match;
  70. std::string name;
  71. int children;
  72.  
  73. // Clean the stream
  74. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  75.  
  76. switch (opt) {
  77. case 1: // Search by name
  78. std::cout<<"Name to search for: ";
  79. std::getline(std::cin, name);
  80.  
  81. find_all_if(emps.begin(), emps.end(), std::back_inserter(match),
  82. std::bind2nd(by_name(), name));
  83. std::copy(match.begin(), match.end(),
  84. std::ostream_iterator<Employee>(std::cout, "\n"));
  85. break;
  86. case 2: // Search by children
  87. std::cout<<"Number of children to search for: ";
  88. std::cin>> children;
  89.  
  90. find_all_if(emps.begin(), emps.end(), std::back_inserter(match),
  91. std::bind2nd(by_children(), children));
  92. std::copy(match.begin(), match.end(),
  93. std::ostream_iterator<Employee>(std::cout, "\n"));
  94. break;
  95. case 3: // Sort and print
  96. std::sort(emps.begin(), emps.end());
  97. std::copy(emps.begin(), emps.end(),
  98. std::ostream_iterator<Employee>(std::cout, "\n"));
  99. break;
  100. }
  101. }
  102. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 5
Reputation: hessa is an unknown quantity at this point 
Solved Threads: 0
hessa hessa is offline Offline
Newbie Poster

Re: HELP me as soon as possible ..!

 
0
  #3
May 6th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: HELP me as soon as possible ..!

 
0
  #4
May 6th, 2005
>it always takes the last name entered
That's because you're using a pointer. When you assign name to s[i], s[i] 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:
  1. char s[10][100];
  2. char name[100];
  3.  
  4. ...
  5.  
  6. cin.width(99); // Protection from buffer overflow
  7. cin>> name;
  8. 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:
  1. char j[100];
  2.  
  3. ...
  4.  
  5. cout << "Input the employee name:" << endl;
  6. cin.width(100);
  7. cin >> j ;
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 5
Reputation: hessa is an unknown quantity at this point 
Solved Threads: 0
hessa hessa is offline Offline
Newbie Poster

Re: HELP me as soon as possible ..!

 
0
  #5
May 6th, 2005
I changed the sort by name like this

  1. char s[10][100];
  2. if ( x == 3 ){
  3. for ( i=0 ; i <3 ; i++ ){
  4. char s1=*s[i];
  5. char s2=*s[i+1];
  6. y=strcmp ( s[i] , s[i+1]);
  7. if ( y > 0 ) {
  8. *s[i] = s2;
  9. *s[i+1]=s1;
  10. }
<< moderator edit: added [code][/code] tags >>

but it doesn't work like what I want,

if I entered
  1. aaa 111 1
  2. ccc 222 2
  3. bbb 333 3

the program should sort them like this
  1. aaa 111 1
  2. bbb 333 3
  3. ccc 222 2

but it do this

  1. aaa 111 1
  2. bcc 222 2
  3. cbb 333 3

How can I fix this problem ,plz ??
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: HELP me as soon as possible ..!

 
0
  #6
May 6th, 2005
>How can I fix this problem ,plz ??
You fix the problem by taking my advice:
Originally Posted by Narue
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 5
Reputation: hessa is an unknown quantity at this point 
Solved Threads: 0
hessa hessa is offline Offline
Newbie Poster

Re: HELP me as soon as possible ..!

 
0
  #7
May 6th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: HELP me as soon as possible ..!

 
0
  #8
May 6th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 5
Reputation: hessa is an unknown quantity at this point 
Solved Threads: 0
hessa hessa is offline Offline
Newbie Poster

Re: HELP me as soon as possible ..!

 
0
  #9
May 6th, 2005
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC