Hello oh helping minds I have a dilema...I've got to create a C language address book and not C++ or turbo C...I've already done a majority so far but I need help with a certain problem if there are any other problems please let me know.

I keep getting an error declaring:

85 'addressbook' undeclared <first use in this function>
85'error' < each undeclared function identifier is reported only once for each function it appears in>

I have the same error in 3 places under 'Insert','Del_info' and 'search'

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>

//defining the properties of the fields used in the program

#define NAME 50
#define ADDRESS 50
#define PHONE 20
#define EMAIL 20
#define SIZE 500


void Insert();
void Del_info();
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];
};

//initializing the files used in the program

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);

    switch (choice)
    {	
    case 1:
      Insert(); 
    break;
    case 2:
      Del_info();
    break;
    case 3:	
      Display();
    break;
    case 4:
      Search();	
    break;
    case 5:
      exit(0);
    break;
	case 6: break;
    default: printf("**Invalid Input.**\n");
 }
 return (0);
 }
  
void Insert()
{
  char choice1;
  do
  {		//opening the AddressBook file
    AddressBook = fopen("AddressBook.txt","a+");

    printf("Enter Person's Name\n");
    scanf("%s",addressbook.name);

    printf("Enter Person's Address\n");
    scanf("%s",addressbook.address);

    printf("Enter Person's Phone no.\n");
    scanf("%s",addressbook.phone);

    printf("Enter Person's E-mail\n");
    scanf("%s",addressbook.email);

    fprintf(AddressBook,"%s %s %s %s  \n",addressbook.name,addressbook.address,addressbook.phone,addressbook.email);
    fclose(AddressBook);

    printf("Press y/Y to Execute the Program Again \n");
    scanf("%c",&choice1);
  }
  while(choice1=='y'||choice1=='Y');
  main();
}

void Del_info()
{
  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 : ");
    gets(Target);

    while(!feof(AddressBook))
    {
      fscanf(AddressBook,"%s %s %s %s",addressbook.name,addressbook.address,addressbook.phone,addressbook.email);
      if(feof(AddressBook))
        break;
      if(strcmp(Target,addressbook.name)!=0)
        fprintf(rectemp,"%s %s %s %s \n",addressbook.name,addressbook.address,addressbook.phone,addressbook.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');
    }
  }
}

void Display()
{
  char choice3;

  AddressBook = fopen("AddressBook.txt","a+");
  do
  {
    fgets(info,SIZE,AddressBook);
    printf("%s\n",info);
  }while(!feof(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(!feof(AddressBook)&& Found==0)
    {
      fscanf(AddressBook,"%s %s %s %s ",addressbook.name,addressbook.address,addressbook.phone,addressbook.email);
      if(strcmp(Target,addressbook.name)==0)
        Found=1;
    }
    if(Found)
    {
      printf("The Name is:               %s\n",addressbook.name);
      printf("The Address is:            %s\n",addressbook.address);
      printf("The phone number is:       %s\n",addressbook.phone);
      printf("The e-mail is:             %s\n",addressbook.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');
}


void Exit()
{
  exit(0);
}
jonsca commented: Good job with code tags +2

Recommended Answers

All 10 Replies

lines 81 and 84. I see no object named "addressbook". There is of course a structure by that name, but no object. Create an object of that type, (don't use the same name as the structure to prevent confusion)

You have not made an instance of your struct like:

struct addressbook
{
  char name [NAME] ;
  char address [ADDRESS];
  char phone [PHONE];
  char email [EMAIL] ;
  char Target[NAME];
} addresses;

In fact you probably want an array of them so #define the number of addresses (say NUMAD) you need and change addresses as above to addresses[NUMAD] or the like. You'll have to keep track of the current count of addresses somewhere so you don't overwrite the information. This way you can have more than one entry.

Another option would be to declare an instance of your struct in main and pass that into your functions.

Ok, so there's going to be some rewriting to do but it shouldn't be too bad. Post back with any questions.

EDIT: AD beat me to it ^^^^^^^

lines 81 and 84. I see no object named "addressbook". There is of course a structure by that name, but no object. Create an object of that type, (don't use the same name as the structure to prevent confusion)

by creating an object do you mean:

int personaladdressbook[50];

or

class personaladdressbook{
put in the methods used/properties}

if you could just show an example based on my code please.

Some thing of this sort

struct test
{
     int x;
}t1;

t1.x=20

>>int personaladdressbook[50];


Almost struct addressbook personaladdressbook[50];

These are the improvements I've made with the following suggestions, hopefully I've created an object and my struct is now right. If there are any other problems you see please your suggestions are welcomed. Thank you so far.

#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
#define NUMAD 10

void Insert();
void Deleteinfo();
void Display();
void Search();
void Exit();

char info[SIZE];



struct addressbook personaladdressbook[50]
{
  char name [NAME];
  char address [ADDRESS];
  char phone [PHONE];
  char email [EMAIL];
  char Target[NAME];
} addresses[NUMAD];


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);

    switch (choice)
    {	
    case 1:
      Insert(); 
    break;
    case 2:
      Del_info();
    break;
    case 3:	
      Display();
    break;
    case 4:
      Search();	
    break;
    case 5:
      exit(0);
    break;
	case 6: break;
    default: printf("**Invalid Input.**\n");
 }
 return (0);
 }
  
void Insert()
{
  char choice1;
  do
  {		//opening the AddressBook file
    AddressBook = fopen("AddressBook.txt","a+");

    printf("Enter Person's Name\n");
    scanf("%s",addressbook.name);

    printf("Enter Person's Address\n");
    scanf("%s",addressbook.address);

    printf("Enter Person's Phone no.\n");
    scanf("%s",addressbook.phone);

    printf("Enter Person's E-mail\n");
    scanf("%s",addressbook.email);

    fprintf(AddressBook,"%s %s %s %s  \n",addressbook.name,addressbook.address,addressbook.phone,addressbook.email);
    fclose(AddressBook);

    printf("Press y/Y to Execute the Program Again \n");
    scanf("%c",&choice1);
  }
  while(choice1=='y'||choice1=='Y');
 
}

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",addressbook.name,addressbook.address,addressbook.phone,addressbook.email);
      if(feof(AddressBook))
        break;
      if(strcmp(Target,addressbook.name)!=0)
        fprintf(rectemp,"%s %s %s %s \n",addressbook.name,addressbook.address,addressbook.phone,addressbook.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');
    }
  }
}

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');

}

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 ",addressbook.name,addressbook.address,addressbook.phone,addressbook.email);
      if(strcmp(Target,addressbook.name)==0)
        Found=1;
    }
    if(Found)
    {
      printf("The Name is:               %s\n",addressbook.name);
      printf("The Address is:            %s\n",addressbook.address);
      printf("The phone number is:       %s\n",addressbook.phone);
      printf("The e-mail is:             %s\n",addressbook.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');
}


void Exit()
{
  exit(0);
}
struct addressbook personaladdressbook[50]
{
  char name [NAME];
  char address [ADDRESS];
  char phone [PHONE];
  char email [EMAIL];
  char Target[NAME];
} addresses[NUMAD];

With this we've confused you a bit, remove both additions from this block. To instantiate a struct do either:

struct mystructname{
//members here - left out for clarity
} mystructarray[50];
and nothing in main

-or-


struct mystructname{
//members here
};
and then in main()
struct mystructname mystructarray[50];

If you make it in main, you're going to have to pass a copy of the struct into your methods (and keep track of the number of entries out in main). If you make it in the first way the array of structs is global and you can access it directly. The first way is probably neater and preferable.

However: I'm looking back over your code and noticing that you aren't really trying to keep an array of the names and addresses at all. It seems like you might not even need the struct. If you need to keep it as part of your requirements just put:

struct addressbook{
//other members here
}myaddressbook;

and rename all of your references to addressbook in Insert() etc to myaddressbook. Apologies we got you going on this array route. I just didn't understand that all you were doing was populating the struct and then writing it out as raw strings anyway.

By any chance are you studying at Brookes university???

I go to the universty of sussex studying ComSci ther mate year 2 ooh yeee, why dya ask? No offence btw but Isn't brookes a failed excuse compared to oxford university?

I go to the universty of sussex studying ComSci ther mate year 2 ooh yeee, why dya ask? No offence btw but Isn't brookes a failed excuse compared to oxford university?

Off-Topic: According to this, Cambridge is the best university in UK. Oxford ranks 5th in the world, which is still a great ranking.

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.