| | |
please help!! have some problems with my address book
![]() |
•
•
Join Date: Sep 2008
Posts: 3
Reputation:
Solved Threads: 0
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
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
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> enum { MAXLEN=255 }; typedef struct list { char name[MAXLEN]; char address[MAXLEN]; char phone[MAXLEN]; struct list *next; } ADDRESS; void add_to_list (void); void delete_from_list (void); void search_name (void); void list_names (void); void quit_program (void); void edit(void); ADDRESS *hol= NULL; int main() { char input[MAXLEN]; while (1) { printf ("What you want to do?\n"); printf("a.Search\n"); printf("b.Add\n"); printf("c.Delete\n"); printf("d.Edit\n"); printf("e.Display Option\n"); printf("f.Exit\n"); fgets (input, MAXLEN,stdin); switch (input[0]) { case ('a'): case ('A'): search_name(); break; case ('b'): case ('B'): add_to_list(); break; case ('c'): case ('C'): delete_from_list(); break; case ('d'): case ('D'): edit(); break; case ('e'): case ('E'): quit_program(); return (0); default: printf ("Unknown command\n"); } } return 0; } void add_to_list (void) { ADDRESS *new_name; new_name= (ADDRESS *)malloc (sizeof (ADDRESS)); if (new_name == NULL) { printf ("Out of memory!\n"); exit (-1); } printf ("Name> "); fgets (new_name->name, MAXLEN, stdin); printf ("Address> "); fgets (new_name->address, MAXLEN, stdin); printf ("Tel> "); fgets (new_name->phone, MAXLEN, stdin); /* Chain the new item into the list */ new_name->next= hol; hol= new_name; } void delete_from_list (void) { ADDRESS *del_ptr; /* Pointer to find name to delete */ ADDRESS *prev_ptr; /* Pointer to name BEFORE this name */ char del_name[MAXLEN]; /* Name to delete */ printf ("Name> "); fgets (del_name, MAXLEN, stdin); if (hol == NULL) { printf ("No list to delete from\n"); return; } if (strcmp(hol->name, del_name) == 0) { del_ptr= hol; hol= hol->next; free(del_ptr); return; } prev_ptr= hol; while (prev_ptr->next != NULL) { if (strcmp(prev_ptr->next->name,del_name) == 0) { del_ptr= prev_ptr->next; prev_ptr->next= del_ptr->next; free(del_ptr); return; } prev_ptr= prev_ptr->next; } printf ("Name not found!\n"); } void search_name (void) { char name[MAXLEN]; /* Name to look for */ ADDRESS *search_ptr; printf ("Name> "); fgets (name, MAXLEN, stdin); search_ptr= hol; while (search_ptr != NULL) { if (strcmp (search_ptr->name, name) == 0) { printf ("Address: %s", search_ptr->address); printf ("Tel: %s", search_ptr->phone); return; } search_ptr= search_ptr->next; } printf ("No such name\n"); } void list_names (void) { ADDRESS *tmp_ptr; /* Traverses list */ printf ("All names in address book:\n"); tmp_ptr= hol; while (tmp_ptr != NULL) { printf ("%s",tmp_ptr->name); tmp_ptr= tmp_ptr->next; } } void quit_program (void) { ADDRESS *del_ptr; while (hol != NULL) { del_ptr= hol; hol= hol->next; free(del_ptr); } } void edit (void) { ADDRESS *edi_ptr; /* Pointer to find name to edit */ ADDRESS *prev_ptr; /* Pointer to name BEFORE this name */ char edi_name[MAXLEN]; /* Name to edit */ printf ("Name> "); fgets (edi_name, MAXLEN, stdin); if (hol == NULL) { printf ("No list to edit from\n"); return; } if (strcmp(hol->name, edi_name) == 0) { edi_ptr= hol; hol= hol->next; free(edi_ptr); return; } prev_ptr= hol; while (prev_ptr->next != NULL) { if (strcmp(prev_ptr->next->name,edi_name) == 0) { edi_ptr= prev_ptr->next; prev_ptr->next= edi_ptr->next; free(edi_ptr); return; } prev_ptr= prev_ptr->next; } printf ("Name not found!\n"); }
Last edited by Ancient Dragon; Sep 8th, 2008 at 4:30 am. Reason: add code tags
>>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.
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Sep 2008
Posts: 3
Reputation:
Solved Threads: 0
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?
Which function i need to use? and how?
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
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Sep 2008
Posts: 3
Reputation:
Solved Threads: 0
now my program can be saved, however when i want to delete a contact, it can't be deleted completely, what's wrong?
C Syntax (Toggle Plain Text)
void delete_from_list (void) { ADDRESS *del_ptr; /* Pointer to find name to delete */ ADDRESS *prev_ptr; /* Pointer to name BEFORE this name */ char del_name[MAXLEN]; /* Name to delete */ printf ("Name> "); fgets (del_name, MAXLEN, stdin); if (hol == NULL) { printf ("No list to delete from\n"); return; } if (strcmp(hol->name, del_name) == 0) { del_ptr= hol; hol= hol->next; free(del_ptr); return; } prev_ptr= hol; while (prev_ptr->next != NULL) { if (strcmp(prev_ptr->next->name,del_name) == 0) { del_ptr= prev_ptr->next; prev_ptr->next= del_ptr->next; free(del_ptr); return; } prev_ptr= prev_ptr->next; } printf ("Name not found!\n"); }
![]() |
Similar Threads
- Problems in Address book programming (C++)
- Address book problems....Any advice??? (C++)
- Problem encounter by user when use the manual address book system (C)
- C++ Address Book (C++)
- Address Book Application... (here's what I have done so far..) (C++)
Other Threads in the C Forum
- Previous Thread: number of digits in a base
- Next Thread: sprintf: makes pointer from integer without a cast`
| Thread Tools | Search this Thread |
* ansi array asterisks binarysearch calculate changingto char character cm convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createprocess() database feet fflush fgets file floatingpointvalidation fork forloop function givemetehcodez global grade gtkwinlinux hacking histogram homework i/o inches infiniteloop input interest intmain() iso kernel keyboard kilometer km linked linkedlist linux locate looping loopinsideloop. lowest match meter microsoft mqqueue number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc process program programming pyramidusingturboccodes radix read recv recvblocked research reversing scanf segmentationfault sequential single socket socketprograming socketprogramming standard string suggestions systemcall threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windowsapi






