0

so far the address book is compiling and takes in entries and runs all the other functions but the search and delete methods don't work even though I have the entry entered in a file and the code should use that very same file to display the names etc if anyone could help I wanna know why the program will not search/display or delete any entries.

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define NAME 50
#define ADDRESS 50
#define PHONE 20
#define EMAIL 20
#define SIZE 500
void Insert();
void Deleteinfo();
void Display();
void Search();
void Exit();
char info[SIZE];
struct addressbook
{
  char name [NAME];
  char address [ADDRESS];
  char phone [PHONE];
  char email [EMAIL];
  char Target[NAME];
} myaddressbook;

FILE *AddressBook;
FILE *rectemp;
int main()
{  
  int choice=0;
  printf("Welcome \n");
    printf("\n 1> Insert new Entry\n");
    printf("\n 2> Delete Entry\n");
    printf("\n 3> Display all Entry\n");
    printf("\n 4> Search Entry\n");
    printf("\n 5> Exit the program\n");
    printf("\n Enter your choice <1-5>: ");
    scanf("%i",&choice);
    if(choice == 1){
    Insert();
    return main();
    }
    else if(choice ==2){
    Deleteinfo();
    return main();
    }
    else if(choice ==3){
    Display();
    return main();
    }
    else if(choice ==4){
    Search();
    return main();
 }
    else if(choice ==5){
    printf("Exit program \n");
 Exit();
    }
    }
void Insert()
{
  char choice1;
  do
  {  //opening the AddressBook file
    AddressBook = fopen("AddressBook.txt","a+");
    printf("Enter Person's Name\n");
    scanf("%s",myaddressbook.name);
    printf("Enter Person's Address\n");
    scanf("%s",myaddressbook.address);
    printf("Enter Person's Phone no.\n");
    scanf("%s",myaddressbook.phone);
    printf("Enter Person's E-mail\n");
    scanf("%s",myaddressbook.email);
    fprintf(AddressBook,"%s %s %s %s  \n",myaddressbook.name,myaddressbook.address,myaddressbook.phone,myaddressbook.email);
    fclose(AddressBook);
    printf("Press y/Y to Execute the Program Again \n");
    scanf("%c",&choice1);
  }
  while(choice1=='y'||choice1=='Y');
  main();
}
void Deleteinfo()
{
  char choice2;
  char Target[SIZE];
  int Found=0;
  rectemp=fopen("rectemp.txt","w");
  if((AddressBook=fopen("AddressBook.txt","r"))==NULL)
    printf("**The File is Empty**\n\n");
  else{
    printf("\tEnter Person's Name : ");
    while(!(AddressBook))
    {
      fscanf(AddressBook,"%s %s %s %s",myaddressbook.name,myaddressbook.address,myaddressbook.phone,myaddressbook.email);
      if(feof(AddressBook))
        break;
      if(strcmp(Target,myaddressbook.name)!=0)
        fprintf(rectemp,"%s %s %s %s \n",myaddressbook.name,myaddressbook.address,myaddressbook.phone,myaddressbook.email);
      else {
        Found=1;
      }
      if (!Found)
      {
        printf("\t**There is no such Entry\n");
      }
      printf("\t**Item is Deleted**\n");
      fclose(AddressBook);
      fclose(rectemp);
      remove("Addressbook.txt");
      rename("rectemp.txt","AddressBook.txt");
      do{
        printf("Press y/Y to Execute the Program Again \n");
        scanf("%c",&choice2);   
      }
      while(choice2=='y'||choice2=='Y');
   main();
    }
  }
}
void Display()
{
  char choice3;
  AddressBook = fopen("AddressBook.txt","a+");
  do
  {
    fgets(info,SIZE,AddressBook);
    printf("%s\n",info);
  }while(!(AddressBook));
  fclose(AddressBook);
  do{
    printf("Press y/Y to Execute the Program Again \n");
    scanf("%c",&choice3);

  }while(choice3=='y'||choice3=='Y');
  main();
}
void Search()
{
  char choice4; 
  char Target[SIZE];
  int Found=0;
  if((AddressBook=fopen("AddressBook.txt","r"))==NULL)
    printf("**The file is empty** !!\n\n");
  else
  {
    printf("\tEnter The Name:");
    scanf("%s",Target);
    while(!(AddressBook)&& Found==0)
    {
      fscanf(AddressBook,"%s %s %s %s ",myaddressbook.name,myaddressbook.address,myaddressbook.phone,myaddressbook.email);
      if(strcmp(Target,myaddressbook.name)==0)
        Found=1;
    }
    if(Found)
    {
      printf("The Name is:               %s\n",myaddressbook.name);
      printf("The Address is:            %s\n",myaddressbook.address);
      printf("The phone number is:       %s\n",myaddressbook.phone);
      printf("The e-mail is:             %s\n",myaddressbook.email);
    }
    else if(!Found)
      printf("**There is no such Entry**\n");
    fclose(AddressBook);
  }
  do{
    printf("Press y/Y to Execute the Program Again \n");
    scanf("%c",&choice4);
  }
  while(choice4=='y'||choice4=='Y');
  main();
}

void Exit()
{
  exit(0);
}
3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by donelliewhyte
0

I've never seen anybody do something like "return main()" before. Use a while loop instead if you are trying to get the effect of looping.

0

I've tried that but what happens is the program quits after I run the choosen option, everything works perfectly its just I don't see why it won't search and display...

0

so far the address book is compiling and takes in entries and runs all the other functions but the search and delete methods don't work even though I have the entry entered in a file and the code should use that very same file to display the names etc if anyone could help I wanna know why the program will not search/display or delete any entries.

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define NAME 50
#define ADDRESS 50
#define PHONE 20
#define EMAIL 20
#define SIZE 500
void Insert();
void Deleteinfo();
void Display();
void Search();
void Exit();
char info[SIZE];
struct addressbook
{
  char name [NAME];
  char address [ADDRESS];
  char phone [PHONE];
  char email [EMAIL];
  char Target[NAME];
} myaddressbook;

FILE *AddressBook;
FILE *rectemp;
int main()
{  
  int choice=0;
  printf("Welcome \n");
    printf("\n 1> Insert new Entry\n");
    printf("\n 2> Delete Entry\n");
    printf("\n 3> Display all Entry\n");
    printf("\n 4> Search Entry\n");
    printf("\n 5> Exit the program\n");
    printf("\n Enter your choice <1-5>: ");
    scanf("%i",&choice);
    if(choice == 1){
    Insert();
    return main();
    }
    else if(choice ==2){
    Deleteinfo();
    return main();
    }
    else if(choice ==3){
    Display();
    return main();
    }
    else if(choice ==4){
    Search();
    return main();
 }
    else if(choice ==5){
    printf("Exit program \n");
 Exit();
    }
    }
void Insert()
{
  char choice1;
  do
  {  //opening the AddressBook file
    AddressBook = fopen("AddressBook.txt","a+");
    printf("Enter Person's Name\n");
    scanf("%s",myaddressbook.name);
    printf("Enter Person's Address\n");
    scanf("%s",myaddressbook.address);
    printf("Enter Person's Phone no.\n");
    scanf("%s",myaddressbook.phone);
    printf("Enter Person's E-mail\n");
    scanf("%s",myaddressbook.email);
    fprintf(AddressBook,"%s %s %s %s  \n",myaddressbook.name,myaddressbook.address,myaddressbook.phone,myaddressbook.email);
    fclose(AddressBook);
    printf("Press y/Y to Execute the Program Again \n");
    scanf("%c",&choice1);
  }
  while(choice1=='y'||choice1=='Y');
  main();
}
void Deleteinfo()
{
  char choice2;
  char Target[SIZE];
  int Found=0;
  rectemp=fopen("rectemp.txt","w");
  if((AddressBook=fopen("AddressBook.txt","r"))==NULL)
    printf("**The File is Empty**\n\n");
  else{
    printf("\tEnter Person's Name : ");
    while(!(AddressBook))
    {
      fscanf(AddressBook,"%s %s %s %s",myaddressbook.name,myaddressbook.address,myaddressbook.phone,myaddressbook.email);
      if(feof(AddressBook))
        break;
      if(strcmp(Target,myaddressbook.name)!=0)
        fprintf(rectemp,"%s %s %s %s \n",myaddressbook.name,myaddressbook.address,myaddressbook.phone,myaddressbook.email);
      else {
        Found=1;
      }
      if (!Found)
      {
        printf("\t**There is no such Entry\n");
      }
      printf("\t**Item is Deleted**\n");
      fclose(AddressBook);
      fclose(rectemp);
      remove("Addressbook.txt");
      rename("rectemp.txt","AddressBook.txt");
      do{
        printf("Press y/Y to Execute the Program Again \n");
        scanf("%c",&choice2);   
      }
      while(choice2=='y'||choice2=='Y');
   main();
    }
  }
}
void Display()
{
  char choice3;
  AddressBook = fopen("AddressBook.txt","a+");
  do
  {
    fgets(info,SIZE,AddressBook);
    printf("%s\n",info);
  }while(!(AddressBook));
  fclose(AddressBook);
  do{
    printf("Press y/Y to Execute the Program Again \n");
    scanf("%c",&choice3);

  }while(choice3=='y'||choice3=='Y');
  main();
}
void Search()
{
  char choice4; 
  char Target[SIZE];
  int Found=0;
  if((AddressBook=fopen("AddressBook.txt","r"))==NULL)
    printf("**The file is empty** !!\n\n");
  else
  {
    printf("\tEnter The Name:");
    scanf("%s",Target);
    while(!(AddressBook)&& Found==0)
    {
      fscanf(AddressBook,"%s %s %s %s ",myaddressbook.name,myaddressbook.address,myaddressbook.phone,myaddressbook.email);
      if(strcmp(Target,myaddressbook.name)==0)
        Found=1;
    }
    if(Found)
    {
      printf("The Name is:               %s\n",myaddressbook.name);
      printf("The Address is:            %s\n",myaddressbook.address);
      printf("The phone number is:       %s\n",myaddressbook.phone);
      printf("The e-mail is:             %s\n",myaddressbook.email);
    }
    else if(!Found)
      printf("**There is no such Entry**\n");
    fclose(AddressBook);
  }
  do{
    printf("Press y/Y to Execute the Program Again \n");
    scanf("%c",&choice4);
  }
  while(choice4=='y'||choice4=='Y');
  main();
}

void Exit()
{
  exit(0);
}

You could change the format of the while loop to

while (fgets(info,SIZE,AddressBook)!=NULL);
    {printf("%s\n",info);}
0

is that change just for the display method or do I have to change the while loops for delete,search and display...technically the search and display are the same if one method just finds what I'm looking for then gr8 so should I focus more on display or the search methods?

0

is that change just for the display method or do I have to change the while loops for delete,search and display...technically the search and display are the same if one method just finds what I'm looking for then gr8 so should I focus more on display or the search methods?

You could apply them to all if you like

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.