943,708 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 679
  • C RSS
Sep 8th, 2008
0

please help!! have some problems with my address book

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ivylim88 is offline Offline
3 posts
since Sep 2008
Sep 8th, 2008
0

Re: please help!! have some problems with my address book

>>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.

Just add another field called UpdateDateTime (or whatever you want to call it) to each record which would be time_t. When you write a record get the system's time using time() function from time.h and save it to the field along with all the other information. If you don't want to save the date/time as an integer then you can call localtime() that returns a string and save that, but it will take up alot more space in the file.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Sep 8th, 2008
0

Re: please help!! have some problems with my address book

oh..ok..thank you so much!!now i have no problem with the time and date thingy..but still have another question that is on exiting the program all active records must be stored in a file eg. phonebook.txt. On start up, the program should read the stored file and display records on the screen.If the file does't exist, have to create a new one to store information. The program should prompt the user for input to perform the functions listed above.
Which function i need to use? and how?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ivylim88 is offline Offline
3 posts
since Sep 2008
Sep 8th, 2008
0

Re: please help!! have some problems with my address book

you need to use FILE*. On program startup, attempt to open the file for reading. If successful, then do what you mentioned. If not, then the file doesn't exist.

>>Which function i need to use? and how?
This is just normal file i/o stuff that should be explained in your textbook.

fopen() -- opens the file
fread() and fgets() -- reads the file
fwrite() -- writes the file
fseek() -- move file pointer around the file
fclose() -- close the file
Last edited by Ancient Dragon; Sep 8th, 2008 at 5:50 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Sep 8th, 2008
0

Re: please help!! have some problems with my address book

now my program can be saved, however when i want to delete a contact, it can't be deleted completely, what's wrong?

  1. void delete_from_list (void)
  2.  
  3. {
  4. ADDRESS *del_ptr; /* Pointer to find name to delete */
  5. ADDRESS *prev_ptr; /* Pointer to name BEFORE this name */
  6. char del_name[MAXLEN]; /* Name to delete */
  7.  
  8. printf ("Name> ");
  9. fgets (del_name, MAXLEN, stdin);
  10.  
  11. if (hol == NULL) {
  12. printf ("No list to delete from\n");
  13. return;
  14. }
  15. if (strcmp(hol->name, del_name) == 0) {
  16. del_ptr= hol;
  17. hol= hol->next;
  18. free(del_ptr);
  19. return;
  20. }
  21.  
  22. prev_ptr= hol;
  23. while (prev_ptr->next != NULL) {
  24.  
  25. if (strcmp(prev_ptr->next->name,del_name) == 0) {
  26. del_ptr= prev_ptr->next;
  27. prev_ptr->next= del_ptr->next;
  28. free(del_ptr);
  29. return;
  30. }
  31. prev_ptr= prev_ptr->next;
  32. }
  33. printf ("Name not found!\n");
  34. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ivylim88 is offline Offline
3 posts
since Sep 2008

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: number of digits in a base
Next Thread in C Forum Timeline: sprintf: makes pointer from integer without a cast`





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


Follow us on Twitter


© 2011 DaniWeb® LLC