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
#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");
}