View Single Post
Join Date: Sep 2008
Posts: 3
Reputation: ivylim88 is an unknown quantity at this point 
Solved Threads: 0
ivylim88 ivylim88 is offline Offline
Newbie Poster

please help!! have some problems with my address book

 
0
  #1
Sep 8th, 2008
i need to write a program that is acting as a simple personal address book with the record of name,email and phone numbers.
The range of function should have:
searching existing records.
adding new records.
deleting existing records.
i need to include the last update date and time of the record in the form of dd-mm-yyyy,hh:mm:ss(it has to be updated by th program automatically) and i don't really know how. Can't someone help me? I'm new to C language. And there is some problem in the delete function, edit function and the file operation.
Below is the code of what my group members and I did so far
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. enum {
  7. MAXLEN=255
  8. };
  9.  
  10. typedef struct list {
  11. char name[MAXLEN];
  12. char address[MAXLEN];
  13. char phone[MAXLEN];
  14. struct list *next;
  15. } ADDRESS;
  16.  
  17. void add_to_list (void);
  18. void delete_from_list (void);
  19. void search_name (void);
  20. void list_names (void);
  21. void quit_program (void);
  22. void edit(void);
  23. ADDRESS *hol= NULL;
  24.  
  25. int main()
  26. {
  27. char input[MAXLEN];
  28.  
  29.  
  30. while (1) {
  31. printf ("What you want to do?\n");
  32. printf("a.Search\n");
  33. printf("b.Add\n");
  34. printf("c.Delete\n");
  35. printf("d.Edit\n");
  36. printf("e.Display Option\n");
  37. printf("f.Exit\n");
  38.  
  39.  
  40. fgets (input, MAXLEN,stdin);
  41. switch (input[0]) {
  42. case ('a'):
  43. case ('A'):
  44. search_name();
  45. break;
  46. case ('b'):
  47. case ('B'):
  48. add_to_list();
  49. break;
  50. case ('c'):
  51. case ('C'):
  52. delete_from_list();
  53. break;
  54. case ('d'):
  55. case ('D'):
  56. edit();
  57. break;
  58. case ('e'):
  59. case ('E'):
  60. quit_program();
  61. return (0);
  62. default:
  63. printf ("Unknown command\n");
  64. }
  65.  
  66. }
  67. return 0;
  68. }
  69.  
  70. void add_to_list (void)
  71.  
  72. {
  73. ADDRESS *new_name;
  74.  
  75. new_name= (ADDRESS *)malloc (sizeof (ADDRESS));
  76. if (new_name == NULL) {
  77. printf ("Out of memory!\n");
  78. exit (-1);
  79. }
  80.  
  81. printf ("Name> ");
  82. fgets (new_name->name, MAXLEN, stdin);
  83. printf ("Address> ");
  84. fgets (new_name->address, MAXLEN, stdin);
  85. printf ("Tel> ");
  86. fgets (new_name->phone, MAXLEN, stdin);
  87. /* Chain the new item into the list */
  88. new_name->next= hol;
  89. hol= new_name;
  90. }
  91.  
  92. void delete_from_list (void)
  93.  
  94. {
  95. ADDRESS *del_ptr; /* Pointer to find name to delete */
  96. ADDRESS *prev_ptr; /* Pointer to name BEFORE this name */
  97. char del_name[MAXLEN]; /* Name to delete */
  98.  
  99. printf ("Name> ");
  100. fgets (del_name, MAXLEN, stdin);
  101.  
  102. if (hol == NULL) {
  103. printf ("No list to delete from\n");
  104. return;
  105. }
  106. if (strcmp(hol->name, del_name) == 0) {
  107. del_ptr= hol;
  108. hol= hol->next;
  109. free(del_ptr);
  110. return;
  111. }
  112.  
  113. prev_ptr= hol;
  114. while (prev_ptr->next != NULL) {
  115.  
  116. if (strcmp(prev_ptr->next->name,del_name) == 0) {
  117. del_ptr= prev_ptr->next;
  118. prev_ptr->next= del_ptr->next;
  119. free(del_ptr);
  120. return;
  121. }
  122. prev_ptr= prev_ptr->next;
  123. }
  124. printf ("Name not found!\n");
  125. }
  126.  
  127. void search_name (void)
  128.  
  129.  
  130. {
  131. char name[MAXLEN]; /* Name to look for */
  132. ADDRESS *search_ptr;
  133. printf ("Name> ");
  134. fgets (name, MAXLEN, stdin);
  135. search_ptr= hol;
  136. while (search_ptr != NULL) {
  137. if (strcmp (search_ptr->name, name) == 0) {
  138. printf ("Address: %s", search_ptr->address);
  139. printf ("Tel: %s", search_ptr->phone);
  140. return;
  141. }
  142. search_ptr= search_ptr->next;
  143. }
  144. printf ("No such name\n");
  145. }
  146.  
  147. void list_names (void)
  148.  
  149. {
  150. ADDRESS *tmp_ptr; /* Traverses list */
  151. printf ("All names in address book:\n");
  152. tmp_ptr= hol;
  153. while (tmp_ptr != NULL) {
  154. printf ("%s",tmp_ptr->name);
  155. tmp_ptr= tmp_ptr->next;
  156. }
  157. }
  158.  
  159. void quit_program (void)
  160. {
  161. ADDRESS *del_ptr;
  162.  
  163. while (hol != NULL) {
  164. del_ptr= hol;
  165. hol= hol->next;
  166. free(del_ptr);
  167. }
  168. }
  169.  
  170.  
  171. void edit (void)
  172.  
  173. {
  174. ADDRESS *edi_ptr; /* Pointer to find name to edit */
  175. ADDRESS *prev_ptr; /* Pointer to name BEFORE this name */
  176. char edi_name[MAXLEN]; /* Name to edit */
  177.  
  178. printf ("Name> ");
  179. fgets (edi_name, MAXLEN, stdin);
  180.  
  181. if (hol == NULL) {
  182. printf ("No list to edit from\n");
  183. return;
  184. }
  185. if (strcmp(hol->name, edi_name) == 0) {
  186. edi_ptr= hol;
  187. hol= hol->next;
  188. free(edi_ptr);
  189. return;
  190. }
  191.  
  192. prev_ptr= hol;
  193. while (prev_ptr->next != NULL) {
  194.  
  195. if (strcmp(prev_ptr->next->name,edi_name) == 0) {
  196. edi_ptr= prev_ptr->next;
  197. prev_ptr->next= edi_ptr->next;
  198. free(edi_ptr);
  199. return;
  200. }
  201. prev_ptr= prev_ptr->next;
  202. }
  203. printf ("Name not found!\n");
  204. }
Last edited by Ancient Dragon; Sep 8th, 2008 at 4:30 am. Reason: add code tags
Reply With Quote