I seriously have no clue why it isn't working, it compiles but no luck getting any of the options to work besides input entry any suggestions will be so greatful!

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define NAME 30
#define ADDRESS 80
#define PHONE 11
#define EMAIL 50
#define SIZE 500
void Insert();
void Deleteinfo();
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> Search Entry\n");
    printf("\n 4> Exit the program\n");
    printf("\n Enter your choice <1-4>: ");
    scanf("%i",&choice);
    if(choice == 1){
    Insert();
    return main();
    }
    else if(choice ==2){
    Deleteinfo();
    return main();
    }
    else if(choice ==3){
    Search();
    return main();
 }
    else if(choice ==4){
    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");
    }
	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);
  }
  main();
}

void Exit()
{
  exit(0);
}

Recommended Answers

All 4 Replies

>>line 38: return main();

Never ever under no circumstances should you make recursive calls to main(). That function is to be called only by the operating system during the program's initial startup. When you want to repeat something, like that menu, use a do or while loop.

>>line 38: return main();

Never ever under no circumstances should you make recursive calls to main(). That function is to be called only by the operating system during the program's initial startup. When you want to repeat something, like that menu, use a do or while loop.

I've gotten rid of the recursive calls to main and edited the methods with delete, search and enter info to return to main after they've finished their use.
Your more than welcome to run the code in a compiler yourself because from what I've learnt I don't know why it won't recognise the entries I enter for the search and delete method.

scanf() is an evil function because it may not process all the keys in the input keyboard buffer(). It can not be used to get strings that contain spaces, such as most addresses, because "%s" stops at the first space. Replace all instances of scanf() with fgets(). For integer input, e.g. "%d" or "%i", use fgets() with character buffer then convert it to integer.

Here is another discussion of that topic within the past week on this board. You might want to read it.

scanf() is an evil function because it may not process all the keys in the input keyboard buffer(). It can not be used to get strings that contain spaces, such as most addresses, because "%s" stops at the first space. Replace all instances of scanf() with fgets(). For integer input, e.g. "%d" or "%i", use fgets() with character buffer then convert it to integer.

Here is another discussion of that topic within the past week on this board. You might want to read it.

I've now got both the search and delete functions working but now I need to specify which entries to delete, right now it displays entries I've just entered, and deletes them when I ask it to but if I put in more than one I don't know how to specify the entries I wish to delete any suggestions?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.