Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help writing a simulation for an address book. I need to use a while loop

ID: 3558176 • Letter: I

Question

I need help writing a simulation for an address book. I need to use a while loop to display the menu choices. The menu choices are:

1. Add a new record to the address book

2. Modify a record in the address book using the name as the key

3. Print information about a record using the name as the key

4. Print all information in the address book

5. Delete an existing record from the address book using the name as a key

6. Exit program

This is the information that should be inside the address book when being printed out:

char                        name[25];

          char                        address[80];

int                           yearofbirth;

char                        telno[15];

struct record*          next;

I just dont know how to start when simulating this and how to write the while loop?

Explanation / Answer

#include<stdio.h>
#include<malloc.h>
#include<string.h>
struct Record{
   char name[25];
   char address[80];
   int yearofbirth;
   char telno[15];
   struct Record * next;
};
void addRecord(struct Record **headAdd)
{
   struct Record *head= *headAdd;
   struct Record *temp= (struct Record *)malloc(sizeof(struct Record));
   printf(" Enter Name : ");
   scanf("%s",&temp->name);
   printf(" Enter Address : ");
   scanf("%s",&temp->address);
   printf(" Enter year of birth : ");
   scanf("%d",&temp->yearofbirth);
   printf(" Enter telephone number : ");
   scanf("%s",&temp->telno);
   if(head==NULL)
   {
       *headAdd = temp;
       temp->next=NULL;
   }
   temp->next=head;
   *headAdd =temp;
   printf(" Contact has been added ");
}
void modifyRecord(struct Record *head)
{
   char name[25];
   printf(" Enter name of the record:");
   scanf("%s",&name);
   struct Record *ptr=head;
   while(ptr!=NULL)
   {
       if(!strcmp(name,ptr->name))
       {
           printf(" Enter New Neme : ");
           scanf("%s",ptr->name);
           printf(" Enter New Address : ");
           scanf("%s",&ptr->address);
           printf(" Enter new year of birth : ");
           scanf("%d",&ptr->yearofbirth);
           printf(" Enter New telephone number : ");
           scanf("%s",&ptr->telno);
           printf(" Contact has been updated ");
           break;
          
       }
       ptr=ptr->next;
   }
   if(ptr==NULL)
   {
       printf(" No record found for given name ");
   }
}
void printRecord(struct Record *head)
{
   char name[25];
   printf(" Enter name of the record:");
   scanf("%s",&name);
   struct Record *ptr=head;
   while(ptr!=NULL)
   {
       if(!strcmp(name,ptr->name))
       {
           printf(" Record Details ");
           printf(" Name : %s",ptr->name);
           printf(" Address : %s",ptr->address);
           printf(" year of birth : %d",ptr->yearofbirth);
           printf(" telephone number : %s ",ptr->telno);
           break;
          
       }
       ptr=ptr->next;
   }
   if(ptr==NULL)
   {
       printf(" No record found for given name ");
   }
}
void printThemAll(struct Record *head)
{
   char name[25];
   struct Record *ptr=head;
   int i=1;
   if(ptr==NULL)
   {
       printf(" There are no records ");
   }
   while(ptr!=NULL)
   {
          
           printf(" Record number %d ",i++);
           printf(" Name : %s",ptr->name);
           printf(" Address : %s",ptr->address);
           printf(" year of birth : %d",ptr->yearofbirth);
           printf(" telephone number : %s ",ptr->telno);
           ptr=ptr->next;
   }
  
}
void deleteRecord(struct Record **headAdd)
{
   char name[25];
   printf(" Enter name of the record:");
   scanf("%s",&name);
   struct Record *ptr=*headAdd;
   struct Record *pre=*headAdd;
   if(ptr==NULL)
   {
       printf(" There are no records to be deleted ");
   }
   else
   {
       while(ptr!=NULL)
       {
           if(!strcmp(name,ptr->name))
           {
              
               if(ptr==pre)//when first node is matched
               {
                   *headAdd = ptr->next;
                   free(ptr);
               }
               else if(ptr->next==NULL)//when last node matched
               {
                   pre->next=NULL;
                   free(ptr);
               }
               else
               {
                   pre->next=ptr->next;
                   free(ptr);
               }
               printf(" Record has been deleted ");
               break;
              
          
           }
           pre=ptr;
           ptr=ptr->next;
       }
   }
  
   if(ptr==NULL&&pre!=NULL)
   {
       printf(" No record found for given name ");
   }
}
int main()
{
   struct Record *addressBook=NULL;
   int flag=1;
   int choice;
   while(flag)
   {
       printf(" Main Menu For Address Book ");
       printf("1) Add a new record ");
       printf("2) Modify a record ");
       printf("3) Print a record ");
       printf("4) Print all recorsd ");
       printf("5) Delete a record ");
       printf("6) Exit ");
       printf("Your choice:");
       scanf("%d",&choice);
       switch(choice)
       {
           case 1: addRecord(&addressBook);
           break;
           case 2:modifyRecord(addressBook);
           break;
           case 3: printRecord(addressBook);
           break;
           case 4: printThemAll(addressBook);
           break;
           case 5:deleteRecord(&addressBook);
           break;
           case 6:flag=0;
           break;
           default: printf("Invalid Entry. Try again...... ");
               break;
       }
   }
   printf(" you choice is number : %d ",choice);
   return 0;
}