943,872 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 542
  • C++ RSS
Apr 1st, 2008
0

help me with this....

Expand Post »
The problem:
Given a list of imployees, create a simple program that allows the user to insert, delete, display and search an employee. The prog. shall display 10 imployees as default. Also a list of menu should be display. The menu names are insert, delete, dsplay and search.

My questions are:
1. what syntax should i use to insert character that allows 'space'?
2. is there a syntax that can 'search' name within a list,,, what is it?

*ideally, it should delete the list or name with a 'current node'.
*the bold part of the prog. is my problem....'searchng to a list'.

i created this prog. guided by the notes given to me.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. struct node
  3. {
  4. char name[50], search[50];
  5. node *nxt;
  6. };
  7.  
  8. node *strtPtr = NULL;
  9. node *current;
  10. int choice;
  11.  
  12. void insert()
  13. {
  14. node *emp,*emp1;
  15. emp=new node;
  16. cout<<"Name of Employee:\n";
  17. cin>>emp->name;
  18. emp->nxt=NULL;
  19. if(strtPtr==NULL)
  20. {
  21. strtPtr=emp;
  22. current=strtPtr;
  23. }
  24. else
  25. {
  26. emp1= strtPtr;
  27. while(emp1->nxt != NULL)
  28. {
  29. emp1=emp1->nxt;
  30. }
  31. emp1->nxt = emp;
  32. current=emp;
  33. }
  34. cout<<"\n\n";
  35. }
  36.  
  37. void display()
  38. {
  39. node *emp;
  40. emp=strtPtr;
  41. cout<<"\nList of the Employees: \n";
  42. if(emp==NULL)
  43. cout<<"List is empty\a\a\n\n";
  44. else
  45. {
  46. while(emp!=NULL)
  47. {
  48. cout<<emp->name;
  49. if(emp==current)
  50. cout<<" <---";
  51. cout<<endl;
  52. emp=emp->nxt;
  53. }
  54. cout<<"\nEnd of List\n\n\n";
  55. }
  56. }
  57.  
  58. void del()
  59. {
  60. node *emp, *emp1;
  61. current=strtPtr;
  62. if(strtPtr==NULL)
  63. cout<<"The List is empty\a\a\n";
  64. else
  65. {
  66. emp=strtPtr;
  67. if(emp->nxt==NULL)
  68. {
  69. delete emp;
  70. strtPtr=NULL;
  71. }
  72. else
  73. {
  74. while(emp->nxt!=NULL)
  75. {
  76. emp1=emp;
  77. emp=emp->nxt;
  78. }
  79. delete emp;
  80. emp1->nxt=NULL;
  81. }
  82. }
  83. }
  84.  
  85. void search()
  86. {
  87. node *emp,*emp1;
  88. emp=strtPtr;
  89. if(emp==NULL)
  90. {
  91. cout<<"List is empty\a\a\n\n";
  92. }
  93. else
  94. {
  95. emp1=new node;
  96. cout<<"Search (name): ";
  97. cin>>emp1->search;
  98. cout<<endl;
  99. if(emp==emp1)
  100. {
  101. emp=current;
  102. }
  103. else
  104. {
  105. while(emp!=emp1)
  106. {
  107. emp=emp->nxt;
  108. emp->nxt=emp1;
  109. current=emp;
  110. }
  111. }
  112. }
  113. }
  114.  
  115. void main()
  116. {
  117. strtPtr=NULL;
  118. do
  119. {
  120. cout<<"Select your option\n"
  121. <<"[0] Exit\n"
  122. <<"[1] Insert new employee\n"
  123. <<"[2] Display list of employees\n"
  124. <<"[3] Delete employee\n"
  125. <<"[4] Search employee\n\n>>";
  126. cin>>choice;
  127. cout<<"\n";
  128.  
  129. switch(choice)
  130. {
  131. case 1: insert(); break;
  132. case 2: display(); break;
  133. case 3: del(); break;
  134. case 4: search(); break;
  135. }
  136. }
  137. while(choice != 0);
  138. }

Sir/madam, i'm new in making c++ program...hope you can help me with this... thank you..God bless..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mengmarc is offline Offline
4 posts
since Mar 2008
Apr 1st, 2008
0

Re: help me with this....

>1. what syntax should i use to insert character that allows 'space'?
cin.getline:
C++ Syntax (Toggle Plain Text)
  1. cin.getline ( emp->name, 50 );
>2. is there a syntax that can 'search' name within a list,,, what is it?
It's called a loop:
C++ Syntax (Toggle Plain Text)
  1. node *it = strtPtr;
  2.  
  3. while ( it != 0 ) {
  4. if ( strcmp ( it->name, key ) == 0 )
  5. break;
  6.  
  7. it = it->nxt;
  8. }
  9.  
  10. if ( it != 0 )
  11. cout<<"Found '"<< it->name <<"'\n";
And if I may be so bold, is there a shortage of vowels on your system? nxt and strtPtr are exceptionally annoying to type and read.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 1st, 2008
0

Re: help me with this....

Quote ...
1. what syntax should i use to insert character that allows 'space'?
U can declare ur variables using string or even character (eg) string Name; and if u gona accept it from the user u can achieve that by this getline(cin, Name) or if u just want to right u can also use getline() method
geline(Name);

Quote ...
2. is there a syntax that can 'search' name within a list,,, what is it?
Yes there is a way U can use a comparison of variables with boolean to search
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
Apr 1st, 2008
0

Re: help me with this....

Traicey, that's all well and good, if the OP's compiler supports the string class, which it may not as shown by the use of the pre-standard iostream.h header.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 2nd, 2008
0

Re: help me with this....

tnx guys...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mengmarc is offline Offline
4 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reading in C++ code at runtime?
Next Thread in C++ Forum Timeline: Urgent: How to read a file again uisng c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC