Hello I'm newbie in C programming. Iwant to make a simple contact book that have add, delete, edit, and display function. 2 out of 4 i've done which is add and delete. So I need some help from all of you in edit and list function. Here is my function for add and delete. I refer this from acidburn code and changed it a little bit and i don't want to use ctype.h because not familiar with it yet.

void Add()
{
char opt1;
do
{
Contactbook = fopen("Contactbook.txt","a+");
printf("Enter his/her Name:\n");
scanf("%s", contactbook.name);
printf("Enter his/her Birthday:\n");
scanf("%s", contactbook.birthday);
printf("Enter his/her Handphone No:\n");
scanf("%s", contactbook.hp);
printf("Enter his/her Address:\n");
scanf("%s", contactbook.address);
printf("Enter his/her Occupation\n");
scanf("%s", contactbook.occupation);
fprintf(Contactbook, "%s %s %s %s %s\n", contactbook.name, 
contactbook.birthday, contactbook.hp, contactbook.address, 
contactbook.occupation);
fclose(Contactbook);
 
printf("To Continue using this program type Y/y");
scanf("%c", &opt1);
}
}
 
void Del()
{
char opt2;
char Tar [SIZE];
int match = 0;
temp = fopen("temp.txt", "w");
if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
printf("----The file is empty----\n");
else
printf("Enter his/her name:");
gets(Tar);
 
while (!feof(Contactbook))
{
fscanf(Contactbook,"%s %s %s %s %s", 
contactbook.name, contactbook.birthday, contactbook.hp, 
contactbook.address, contactbook.occupation);
if(feof(Contactbook))
break;
if(strcmp(Tar, contactbook.name) !=0)
fprintf(temp,"%s %s %s %s %s\n", contactbook.name, 
contactbook.birthday, contactbook.hp, contactbook.address, 
contactbook.occupation);
else
{
match = 1;
}
if (!match)
{
printf("The name does not exist\n");
printf("or it might had been deleted\n");
}
 
fclose(Contactbook);
fclose(temp);
remove("Contactbook.txt");
rename("temp.txt","Contacbook.txt");
 
}

So i need some help on edit and list function. At 1st i think i want to use the same code as delete but change remove to rename but it can't be done that's way. So your helps are highly appreciated. Ok. Thanks

Sorry 4 da mistake. Anyway, anybody can give me some ideas please.......

I'd say first brush up on user input.

scanf("%s", contactbook.name);
gets(Tar);

User Input: Strings and Numbers [C]

while (!feof(Contactbook))

Avoid Loop Control Using eof()

And it might be helpful to post a program that attempts to use/test these functions rather than leaving that up to others. Presumeably you're writing test code that uses them? Share your "secret".

Use good formatting. Use correct spelling, punctuation, and grammar. Use code tags correctly. In short, make your post as easy to answer as you can -- or else get used to longer waits.

Thanks Dave for your advice. Sorry 4 not posting my whole code. Here's my full code. When run it, it didn't seems to run well.
I will seriously have a look your points and try to fix it. Anyway I'm still newbie, there's a lot of things to be learn. Your guidance will highly be appreciated. Actually this thing is my homework my teacher gave it to me and my friend, kind of group work. But I stucked at edit function. Thank You.

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

#define NAME 500
#define BIRTHDAY 500
#define HP 500
#define ADDRESS 500
#define OCCUPATION 500
#define SIZE 500

void Add();
void Del();
void Search();
void Edit();
void List();
void Exit();
char info[SIZE];

struct contactbook
{
 char name [NAME];
 char birthday [BIRTHDAY];
 char hp [HP];
 char address [ADDRESS];
 char occupation [OCCUPATION];
 char Tar [NAME];
}
contactbook;

FILE *Contactbook;
FILE *temp;
 
int main()
{
 int a;
 printf("Welcome to BRONTOK X Simple Contact Book\n");
 printf("Please Choose your option\n");
 printf("1: Adding The Contact\n");
 printf("2: Delete The Contact\n");
 printf("3: Search The Contact\n");
 printf("4: Edit The Contact\n");
 printf("5: List The Contact\n");
 printf("6: Exit this Program\n");
 scanf("%d", &a);
 switch(a)
 {
  case 1:
  {
   Add();
   break;
  }
  case 2:
  {
   Del();
   break;
  }
  case 3:
  {
   Search();
   break;
  }
  case 4:
  {
   Edit();
   break;
  }
  case 5:
  {
   List();
   break;
  }
  case 6:
  {
   Exit();
   break;
  }
  default:
  {
   printf("Invalid Input");
  }
 }
}
void Add()
{
 char opt1;
 do
 {
 Contactbook = fopen("Contactbook.txt","a+");
 printf("Enter his/her Name:\n");
 scanf("%s", contactbook.name);
 printf("Enter his/her Birthday:\n");
 scanf("%s", contactbook.birthday);
 printf("Enter his/her Handphone No:\n");
 scanf("%s", contactbook.hp);
 printf("Enter his/her Address:\n");
 scanf("%s", contactbook.address);
 printf("Enter his/her Occupation\n");
 scanf("%s", contactbook.occupation);
 fprintf(Contactbook, "%s %s %s %s %s\n", contactbook.name, 
contactbook.birthday, contactbook.hp, contactbook.address, 
contactbook.occupation);
 fclose(Contactbook);

 printf("To Continue using this program type Y/y");
 scanf("%c", &opt1);
 }
 if(opt1 =='y' || opt1 == 'Y')
 main();
}

void Del()
{
 char opt2;
 char Tar [SIZE];
 int match = 0;
 temp = fopen("temp.txt", "w");
 if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
  printf("----The file is empty----\n");
 else
  printf("Enter his/her name:");
  gets(Tar);

 while (!feof(Contactbook))
 {
  fscanf(Contactbook,"%s %s %s %s %s", 
contactbook.name, contactbook.birthday, contactbook.hp, 
contactbook.address, contactbook.occupation);
 if(feof(Contactbook))
 break;
 if(strcmp(Tar, contactbook.name) !=0)
  fprintf(temp,"%s %s %s %s %s\n", contactbook.name, 
contactbook.birthday, contactbook.hp, contactbook.address, 
contactbook.occupation);
 else
 {
  match = 1;
 }
 if (!match)
 {
  printf("The name does not exist\n");
  printf("or it might had been deleted\n");
 }

 fclose(Contactbook);
 fclose(temp);
 remove("Contactbook.txt");
 rename("temp.txt","Contacbook.txt");

 do
 {
  printf("To Continue using this program type Y/y");
  scanf("%c", &opt2);
 }
 while (opt2 =='y' || opt2 == 'Y');
 main();
 }
}
void Search()
{
 char opt3;
 char Tar [SIZE];
 int match = 0;
 if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
  printf("----The File is Empty----\n");
 else
 {
  printf("Enter his/her name:\n");
  scanf("%s", Tar);
  while(!feof(Contactbook) && match ==0)
  {
   fscanf(Contactbook,"%s %s %s %s 
%s",contactbook.name, contactbook.birthday, contactbook.hp, 
contactbook.address, contactbook.occupation);
  if(strcmp(Tar, contactbook.name) == 0)
   match = 1;
  }
  if(match)
  {
  printf("His/Her Name is %s\n",contactbook.name);
  printf("His/Her Birthday is s\n", 
contactbook.birthday);
  printf("His/Her Handphone No is %s\n", 
contactbook.hp);
  printf("His/Her Address is %s\n",  
contactbook.address);
  printf("His/Her Occupation is %s\n",  
contactbook.occupation);
  main();
  }
  else if (!match)
   printf("----There is no such entry----");
  fclose(Contactbook);
  }
  do
 {
  printf("To Continue using this program type Y/y");
  scanf("%c", &opt3);
 }
 while (opt3 =='y' || opt3 == 'Y');
 main();
}
void Edit()
{
 
void List()
{
 int opt5;
 Contactbook = fopen("Contactbook.txt","a+");
 do
 {
  fgets(info,SIZE,Contactbook);
  printf("%s\n", info);
 }
 while(!feof(Contactbook));
 fclose(Contactbook);
 do
 {
  printf("Enter y or Y to run the program again\n");
  scanf("%c", &opt5);
 }
 while (opt5 == 'y' ||opt5 == 'Y');
 main();
}

void Exit()
{
 printf("Thank You For Using my Contactbook. Have a Nice 
Day");
}

I would encourage you to do just one function at a time.

Since your first function is Add(), I would comment all the other functions bodies and just have a statement like printf("in Del()"); etc. until you get there. Within Add() I would note:

1) each keyword do needs a keyword while.

2) Never call main() from within your program. Use loops to accomplish what you want. In this case you should use a loop in main() to cycle throught the menu and a loop in Add() to allow sequential Add()s without going through the menu, if that is your intent.

Check your Edit function. Where it ends?

Check your Edit function. Where it ends?

The Edit function is now become my major problem because i have no idea on how to make it.

I would encourage you to do just one function at a time.

Since your first function is Add(), I would comment all the other functions bodies and just have a statement like printf("in Del()"); etc. until you get there. Within Add() I would note:

OIC. I will change it to loop. But for the above statement could u please explain more. With examples if u don't mind

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 500

void Add();
void Del();
//void Search();
//void Edit();
//void List();
//void Exit();
char info[SIZE];

typedef struct contactboo
{
 char name [SIZE];
 char birthday [SIZE];
}contactbook;

FILE *Contactbook;
FILE *temp;
 
int main()
{
 int a;
 char flag = 'y';
 while(flag != 'n')
 {
   printf("Welcome to BRONTOK X Simple Contact Book\n");
   printf("Please Choose your option\n");
   printf("1: Adding The Contact\n");
   printf("2: Delete The Contact\n");
   printf("6: Exit this Program\n");
   scanf("%d", &a);
   switch(a)
   {
    case 1:
      Add();
      break;
    case 2:
      Del();
      break;
/*  
  case 3:
  {
   Search();
   break;
  }
  case 4:
  {
   Edit();
   break;
  }
  case 5:
  {
   List();
   break;
  }
*/
  case 6:
    flag = 'n';
    break;
  default:
    printf("Invalid Input");
 }

 printf("Good bye!");
 getch();
 return 0;
}

void Add()
{
 char opt1;
 do
 {
   Contactbook = fopen("Contactbook.txt","a+");
   printf("Enter his/her Name:\n");
   scanf("%s", contactbook.name);
   printf("Enter his/her Birthday:\n");
   scanf("%s", contactbook.birthday);
   fprintf(Contactbook, "%s %s\n", contactbook.name, 
   contactbook.birthday);
   fclose(Contactbook);

   printf("To Continue using this program type Y/y");
   scanf("%c", &opt1);
 }while(opt1 =='y' || opt1 == 'Y')
}

void Del()
{
  printf("in Del()");
  getch();
/*
 char opt2;
 char Tar [SIZE];
 int match = 0;
 temp = fopen("temp.txt", "w");
 if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
  printf("----The file is empty----\n");
 else
  printf("Enter his/her name:");
  gets(Tar);

 while (!feof(Contactbook))
 {
  fscanf(Contactbook,"%s %s %s %s %s", 
contactbook.name, contactbook.birthday);
 if(feof(Contactbook))
 break;
 if(strcmp(Tar, contactbook.name) !=0)
  fprintf(temp,"%s %s %s %s %s\n", contactbook.name, 
contactbook.birthday, contactbook.hp, contactbook.address, 
contactbook.occupation);
 else
 {
  match = 1;
 }
 if (!match)
 {
  printf("The name does not exist\n");
  printf("or it might had been deleted\n");
 }

 fclose(Contactbook);
 fclose(temp);
 remove("Contactbook.txt");
 rename("temp.txt","Contacbook.txt");

 do
 {
  printf("To Continue using this program type Y/y");
  scanf("%c", &opt2);
 }
 while (opt2 =='y' || opt2 == 'Y');
 main();
 }
*/
}

/*
void Search()
{
 char opt3;
 char Tar [SIZE];
 int match = 0;
 if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
  printf("----The File is Empty----\n");
 else
 {
  printf("Enter his/her name:\n");
  scanf("%s", Tar);
  while(!feof(Contactbook) && match ==0)
  {
   fscanf(Contactbook,"%s %s %s %s 
%s",contactbook.name, contactbook.birthday, contactbook.hp, 
contactbook.address, contactbook.occupation);
  if(strcmp(Tar, contactbook.name) == 0)
   match = 1;
  }
  if(match)
  {
  printf("His/Her Name is %s\n",contactbook.name);
  printf("His/Her Birthday is s\n", 
contactbook.birthday);
  printf("His/Her Handphone No is %s\n", 
contactbook.hp);
  printf("His/Her Address is %s\n",  
contactbook.address);
  printf("His/Her Occupation is %s\n",  
contactbook.occupation);
  main();
  }
  else if (!match)
   printf("----There is no such entry----");
  fclose(Contactbook);
  }
  do
 {
  printf("To Continue using this program type Y/y");
  scanf("%c", &opt3);
 }
 while (opt3 =='y' || opt3 == 'Y');
 main();
}
void Edit()
{
 
void List()
{
 int opt5;
 Contactbook = fopen("Contactbook.txt","a+");
 do
 {
  fgets(info,SIZE,Contactbook);
  printf("%s\n", info);
 }
 while(!feof(Contactbook));
 fclose(Contactbook);
 do
 {
  printf("Enter y or Y to run the program again\n");
  scanf("%c", &opt5);
 }
 while (opt5 == 'y' ||opt5 == 'Y');
 main();
}

void Exit()
{
 printf("Thank You For Using my Contactbook. Have a Nice 
Day");
}
*/

It would really help readability if you'd indent more. 1 space is hard to follow -- it's too much like none. Generally the recommendation is 3-4 spaces. Not a hard and fast rule, but a general rule. Readability is the key.

I've made a new coding here. But it seems to be a little error.
The two questions seems to come out together.
the output
Enter his/her name: <---- The problem **
**Enter his/her birthday:<--- is here

Enter his/her handphone:

Enter his/her Address:

Enter his/her Occupation:

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

#define NAMES 80
#define BIRTHDAY 30
#define HANDPHONE 15
#define ADDRESS 120
#define OCCUPATION 50
#define SIZE 200


void Add();
void Del();
void Search();
void Edit();
void List();
void Exit();
char info[SIZE];


struct contactbook
{
   char names [NAMES];
   char birthday [BIRTHDAY];
   char hp [HANDPHONE];
   char address [ADDRESS];
   char occupation [OCCUPATION];
   char Target [SIZE];
}
contactbook;


FILE *Contactbook;
FILE *temp;



int main()
{
   int option;
   system("clear");

   printf("\n            WELCOME TO BRONTOK-X SIMPLE CONTACT BOOK\n");
   printf("\n                 This program enables you to:\n");
   printf("\n                  ADD, DELETE, EDIT, SEARCH, and LIST\n");
   printf("\n                   your contact info =D\n\n");
       printf("\n    To start running this program, you have to choose an option from 1 to 6\n");
   printf("\n            1: Adding Contact Info\n");
   printf("            2: Delete Your Contact Info\n");
   printf("            3: Search Your Contact Info\n");
   printf("            4: Edit Your Contact Info\n");
   printf("            5: List Down All Your Contact Info\n");
   printf("            6: Exit This Program\n\n");
       printf("            Please Enter Your Choice: ");

   scanf("%d", &option);

  switch(option)
   {
       case 1:
       {
           system("clear");
           Add();
           break;
       }
       case 2:
       {
           system("clear");
           Del();
           break;
       }
       case 3:
       {    
           system("clear");
           Search();
           break;
       }
       case 4:
       {
           system("clear");
           Edit();
           break;
       }
       case 5:
       {
           system("clear");
           List();
           break;
       }
       case 6:
       {
           system("clear");
           Exit();
           break;
       }
       default:
       {
           system("clear");
           printf("Invalid Input");
           break;
       }
   }
}

void Add()
{

  Contactbook = fopen("Contactbook.txt","a+");

   printf("Enter his/her Name:\n");
   fflush(stdin);
   fgets(contactbook.names, sizeof contactbook.names, stdin);

   printf("Enter his/her Birthday:\n");
   fflush(stdin);
   fgets(contactbook.birthday, sizeof contactbook.birthday, stdin);

   printf("Enter his/her Handphone No:\n");
   fflush(stdin);
   fgets(contactbook.hp, sizeof contactbook.hp, stdin);

   printf("Enter his/her Address:\n");
   fflush(stdin);
   fgets(contactbook.address, sizeof contactbook.address, stdin);

   printf("Enter his/her Occupation\n");
   fflush(stdin);
   fgets(contactbook.occupation, sizeof contactbook.occupation, stdin);

   fprintf(Contactbook, "%s %s %s %s %s\n", contactbook.names,contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);

   fflush(stdin);
   fclose(Contactbook);
   main();
}

void Del()
{
   main();
}

void Search()
{
   main();
}

void Edit()
{
   main();
}

void List()
{
   main();
}

void Exit()
{
   system("clear");
}

Take another look at the "user input" link I posted.
Don't use fflush(stdin) .
Don't call main recursively.

Take another look at the "user input" link I posted.
Don't use fflush(stdin) .
Don't call main recursively.

I've take another look at the "user input" but still didn't get what you mean. Removing fflush(stdin) will still give me the same result.
The weird thing is, this code worked nicely in windows but using knoppix it seems to be a little bit screw with the add() function, imean as i said previously the tqo question seems don't want to be separated. That's the only problem i have for now.One more thing, if i can't call main() recursively, can u suggest me any other way to call the main just after finish other function.

> One more thing, if i can't call main() recursively, can u suggest me any other way to call the main just after finish other function.

You don't need to call main; when your function finishes, it returns the calling function. If you need the code to repeat, use a loop within main.

>The weird thing is, this code worked nicely in windows but using knoppix it seems to be a little bit screw with the add()

Because what you were doing was undefined. It might work, or it might blow up your computer. Maybe this will help.

> One more thing, if i can't call main() recursively, can u suggest me any other way to call the main just after finish other function.

You don't need to call main; when your function finishes, it returns the calling function. If you need the code to repeat, use a loop within main.

>The weird thing is, this code worked nicely in windows but using knoppix it seems to be a little bit screw with the add()

Because what you were doing was undefined. It might work, or it might blow up your computer. Maybe this will help.

hmmm........can u pls explain it in details bcoz i'm still newbie in C programming. and this is group work, so have to do some research on the topic. I still can't imagine how to use loop to recall a function. examples plz if u don't mind.

hmmm........can u pls explain it in details bcoz i'm still newbie in C programming. and this is group work, so have to do some research on the topic. I still can't imagine how to use loop to recall a function. examples plz if u don't mind.

Lerner already showed you here, about 6 posts ago.

Also, don't PM members to personally solve your problem for you. Ask questions here, read what they say, and attempt what they tell you.

Lerner already showed you here, about 6 posts ago.

Also, don't PM members to personally solve your problem for you. Ask questions here, read what they say, and attempt what they tell you.

i already tried the code that was given by Lerner, but it seems to be have many error that i can't figure out . "parse error before "contactbook" " and other two more error which i don't remember. And i don't used and don't understand getch() function.

Sorry for my bad manner in using PM.

i already tried the code that was given by Lerner, but it seems to be have many error that i can't figure out . "parse error before "contactbook" " and other two more error which i don't remember. And i don't used and don't understand getch() function.

You don't need to compile and run his code. Look at what he did in main() to set up a loop. Then you can stop calling main() from your functions.

in the previous post Dave had stated not to use infinite loop !feof but the example given is in C++, is there any example in C ? what should i replace !feof with?

in the previous post Dave had stated not to use infinite loop !feof but the example given is in C++, is there any example in C ? what should i replace !feof with?

Check this link

I've adjusted my code but it didn't wat to work. Can anybody check where the codes go wrong.

//delete function
void Del()
{
    int match = 0;
    char Find[200];
    system("clear");
    temp = fopen("temp.txt", "w+");
    if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
         printf("----The file is empty----\n");
    else
   {
     printf("Enter his/her name that you want to delete:\n");
     fgets(Find, 200, stdin);
     strip(Find, 200);
   }
 
    while(fgets(Find, 200, stdin) != NULL)
    {
       fscanf(Contactbook, "%s %s %s %s %s",    contactbook.names,contactbook.birthday, contactbook.hp,  contactbook.address, contactbook.occupation);
    }  
 if(strcmp(Find, contactbook.names) !=0)
  fprintf(temp,"%s %s %s %s %s\n", contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);
 else
 {
  match = 1;
  printf("\nHis/Her Name     : %s\n", contactbook.names);
  printf("\nBirth of Date    : %s\n", contactbook.birthday);
  printf("\nHandphone Number : %s\n", contactbook.hp);
  printf("\nAddress          : %s\n", contactbook.address);
  printf("\nOccupation       : %s\n", contactbook.occupation);
  system("clear");
 }
  if (!match)
 {
    printf("The name does not exist\n");
    printf("or it might had been deleted\n");
    system("clear");
 }
 fclose(Contactbook);
 fclose(temp);
 remove("Contactbook.txt");
 rename("temp.txt","Contacbook.txt");
}
 
//search function
void Search()
{
 
    char Find[200];
 int Found=0;
 if((Contactbook=fopen("Contactbook.txt","r"))==NULL)
  printf("NO RECORDS THE FILE EMPTY!!!\n\n");
 else
 {
  system("clear");
  printf("\tEnter Name to search:");
  fflush(stdin);
  fgets(Find, 200, Contactbook);
  while(fgets(Find, 200, Contactbook)&& Found==0)
  {
    fscanf(Contactbook,"%s %s %s %s %s ",contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);
    if(strcmp(Find,contactbook.names)==0)
    Found=1;
  }
  if(Found)
  {
     system("clear");
    printf("Name: %s\n",contactbook.names);
    printf("Birthday: %s\n",contactbook.birthday);
    printf("Handphone: %s\n",contactbook.hp);
    printf("Address : %s\n",contactbook.address);
    printf("Occupation: %s\n",contactbook.occupation);
 
  } 
  else if(!Found)
  {
    printf("SORRY FILE NOT FOUND\n");
   }
    fclose(Contactbook);
  }
}

Is this the whole code? Becouse there's no main.

Is this the whole code? Becouse there's no main.

here's the full code

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

void Add();
void Del();
void Search();
void Edit();
void List();
void Exit();
void strip(char *str, int size);
char info[300];

struct contactbook
{
 char names [80];
 char birthday [60];
 char hp [40];
 char address [300];
 char occupation [50];
 char Target [120];
 char garbage [200];
}
contactbook;

FILE *Contactbook;
FILE *temp;
 
int main()
{
 int option;
 system("clear");
 printf("\n      WELCOME TO BRONTOK-X SIMPLE CONTACT BOOK\n");
 printf("\n           This program enables you to:\n");
 printf("\n               ADD, DELETE, EDIT, SEARCH, and LIST\n");
 printf("\n             your contact info =D\n\n");
        printf("\n    To start running this program, you have to choose an option from 1 to 6\n");
 printf("\n   1: Adding Contact Info\n");
 printf("   2: Delete Your Contact Info\n");
 printf("   3: Search Your Contact Info\n");
 printf("   4: Edit Your Contact Info\n");
 printf("   5: List Down All Your Contact Info\n");
 printf("   6: Exit This Program\n\n");
        printf("   Please Enter Your Choice: ");
 scanf("%d", &option);
 switch(option)
 {
  case 1:
  {
   system("clear");
   Add();
   break;
  }
  case 2:
  {
   system("clear");
   Del();
   break;
  }
  case 3:
  { 
   system("clear");
   Search();
   break;
  }
  case 4:
  {
   system("clear");
   Edit();
   break;
  }
  case 5:
  {
   system("clear");
   List();
   break;
  }
  case 6:
  {
   system("clear");
   Exit();
   break;
  }
  default:
  {
   system("clear");
   printf("Invalid Input");
   break;
  }
 }
 return 0;
}
void Add()
{
 Contactbook = fopen("Contactbook.txt","a+");
 printf("Enter his/her Name:\n");
    fflush(stdin);
 fgets(contactbook.names,  80, stdin);
 strip(contactbook.names, 80);
  
 printf("Enter his/her Birthday:\n");
    fflush(stdin);
 fgets(contactbook.birthday,  60, stdin);
    strip(contactbook.birthday, 60);
    
 printf("Enter his/her Handphone No:\n");
 fflush(stdin);
    fgets(contactbook.hp,  60, stdin);
 strip(contactbook.hp, 60);
 
 printf("Enter his/her Address:\n");
 fflush(stdin);
 fgets(contactbook.address,  300, stdin);
 strip(contactbook.address, 300);
 
 printf("Enter his/her Occupation\n");
 fflush(stdin);
 fgets(contactbook.occupation,  50, stdin);
 strip(contactbook.occupation, 50);
 
 fprintf(Contactbook, "%s\n", contactbook.names);
    fprintf(Contactbook, "%s\n", contactbook.birthday);
    fprintf(Contactbook, "%s\n", contactbook.hp);
    fprintf(Contactbook, "%s\n", contactbook.address);
    fprintf(Contactbook, "%s\n", contactbook.occupation);    
    
 fflush(stdin);
 fclose(Contactbook);
}

void Del()
{
 int match = 0;
    char Find[200];
 system("clear");
 temp = fopen("temp.txt", "w+");
 if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
  printf("----The file is empty----\n");
 else
 {
  printf("Enter his/her name that you want to delete:\n");
  fgets(Find, 200, stdin);
  strip(Find, 200);
 }
 
 while(fgets(Find, 200, stdin) != NULL)
    {
      fscanf(Contactbook, "%s %s %s %s %s", contactbook.names,contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);
    }  
 if(strcmp(Find, contactbook.names) !=0)
  fprintf(temp,"%s %s %s %s %s\n", contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);
 else
 {
  match = 1;
  printf("\nHis/Her Name     : %s\n", contactbook.names);
  printf("\nBirth of Date    : %s\n", contactbook.birthday);
  printf("\nHandphone Number : %s\n", contactbook.hp);
  printf("\nAddress          : %s\n", contactbook.address);
  printf("\nOccupation       : %s\n", contactbook.occupation);
  system("clear");
  main();
 }
 if (!match)
 {
  printf("The name does not exist\n");
  printf("or it might had been deleted\n");
  system("clear");
 }
 fclose(Contactbook);
 fclose(temp);
 remove("Contactbook.txt");
 rename("temp.txt","Contacbook.txt");
}
void Search()
{
  
    char Find[200];
 int Found=0;
 if((Contactbook=fopen("Contactbook.txt","r"))==NULL)
  printf("----The File Is Empty!!!----\n\n");
 else
 {
  system("clear");
  printf("Enter Name to search:\n");
  fflush(stdin);
  fgets(Find, 200, Contactbook);
  while(fgets(Find, 200, Contactbook)&& Found==0)
  {
    fscanf(Contactbook,"%s %s %s %s %s ",contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);
    if(strcmp(Find,contactbook.names)==0)
    Found=1;
  }
  if(Found)
  {
     system("clear");
    printf("Name: %s\n",contactbook.names);
    printf("Birthday: %s\n",contactbook.birthday);
    printf("Handphone: %s\n",contactbook.hp);
    printf("Address : %s\n",contactbook.address);
    printf("Occupation: %s\n",contactbook.occupation);
      
  } 
  else if(!Found)
  {
    printf("SORRY FILE NOT FOUND\n");
          
  }
  fclose(Contactbook);
     }
}

void Edit()
{
 main();
}
void List()
{
 Contactbook = fopen("Contactbook.txt","a+");
 
 if(Contactbook != NULL)
      {
         system("clear"); 
      printf("Current Entries: \n");
         while(fgets(contactbook.names, 80, stdin) != NULL)
         {
            printf("%s", contactbook.names);
            printf("\n");
         }
         printf("End of File \n");
         system("pause");
  fclose(Contactbook);
      }
}

void Exit()
{
 printf("Thank You For Using our Contactbook. Have a Nice Day\n");
 system("clear");
}
void strip(char *str, int size)
{
    int i;
    for (  i = 0; i < size; ++i )
    {
        if ( str[i] == '\n' )
        {
            str[i] = '\0';
            return;   
        }
    }
}

fflush dosn't work for input streams and I think it's system("cls"); instead of system("clear");

First of all delete all fflush(stdin) in your code. Instead of first fflush(stdin) in Add func type

while ((ch = getchar()) != '\n' && ch != EOF);

declare ch as int . Read this

commented: Nice--[Grunt] +1

fflush dosn't work for input streams and I think it's system("cls"); instead of system("clear");

system("cls") for windows
system("clear") for unix(knoppix)

the problem right now that i want to cocentrate at is Search function and Delete function. Anybody know. Pls help me. Urgent.......

commented: oh ok...I didn't knew that-[Grunt] +1

First implement the change which andor told you.

First implement the change which andor told you.

Done. But search and delete function still didn't work.

Done. But search and delete function still didn't work.

Done. I rectified it. But I can not show you the code since you didn't showed your changed code.

Done. I rectified it. But I can not show you the code since you didn't showed your changed code.

//initializing the header files
#include <stdio.h>
#include <string.h>

//declaring the functions
void Add(), Del(), Search(), Edit(), List(), Exit();
char info[100];

//using stuctures that represent the fields in the program
struct contactbook
{
 char names [100];
 char birthday [100];
 char hp [100];
 char address [100];
 char occupation [100];
 char Target [100];
 char garbage [100];
}
contactbook;

//initializing the files
FILE *Contactbook;
FILE *temp;


//initializing of the main function
int main()
{
 //declaring the option variable
 int option;
 system("clear");

 
 printf("\n      WELCOME TO BRONTOK-X SIMPLE CONTACT BOOK\n");
 printf("\n           This program enables you to:\n");
 printf("\n               ADD, DELETE, EDIT, SEARCH, and LIST\n");
 printf("\n             your contact info =D\n\n");
    printf("\n    To start running this program, you have to choose an option from 1 to 6\n");
 printf("\n   1: Adding Contact Info\n");
 printf("   2: Delete Your Contact Info\n");
 printf("   3: Search Your Contact Info\n");
 printf("   4: Edit Your Contact Info\n");
 printf("   5: List Down All Your Contact Info\n");
 printf("   6: Exit This Program\n\n");
    printf("   Please Enter Your Choice: ");

 scanf("%d", &option);

//using the switch statement to give an option to initialize the functions
 switch(option)
 {
  case 1:
  {
   system("clear");
   Add();
   break;
  }
  case 2:
  {
   system("clear");
   Del();
   break;
  }
  case 3:
  { 
   system("clear");
   Search();
   break;
  }
  case 4:
  {
   system("clear");
   Edit();
   break;
  }
  case 5:
  {
   system("clear");
   List();
   break;
  }
  case 6:
  {
   system("clear");
   Exit();
   break;
  }
  default:
  {
   system("clear");
   printf("Invalid Input\n");
   break;
  }
 }
 return 0;
}

//initializing the Add function
void Add()
{
     char menu;
     int ch;
       
       //Open "Contactbook.txt" file
        Contactbook = fopen("Contactbook.txt","w+");

           
     printf("\nName: ");
            while ((ch = getchar()) != '\n' && ch != EOF);
     fgets(contactbook.names, 100, stdin);
            printf("\nBirthday: ");
            fgets(contactbook.birthday,100, stdin);
            printf("\nHandphone: ");
            fgets(contactbook.hp, 100, stdin);
            printf("\nAddress: ");
            fgets(contactbook.address,100, stdin);
            printf("\nOccupation: ");
            fgets(contactbook.occupation,100, stdin);
 
     fprintf(Contactbook, "%s %s %s %s %s\n", contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);    
    
     fclose(Contactbook);
}

//initializing the Delete function
void Del()
{
 int match = 0, ch;
        char Find[100];

 system("clear");
 temp = fopen("temp.txt", "w");

 if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
  printf("----The file is empty----\n");
 else
 {
  printf("Enter his/her name that you want to delete:\n");
  while ((ch = getchar()) != '\n' && ch != EOF);
         fgets(Find, 100, stdin);
  
 }
 
 while(fgets(Find, 100, Contactbook) != NULL)
     {
         fscanf(Contactbook, "%s %s %s %s %s\n", contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);  
     }  

 if(strcmp(Find, contactbook.names) !=0)
  fprintf(temp,"%s %s %s %s %s\n", contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);

 else
 {
  match = 1;

  printf("\nHis/Her Name     : %s\n", contactbook.names);
  printf("\nBirth of Date    : %s\n", contactbook.birthday);
  printf("\nHandphone Number : %s\n", contactbook.hp);
  printf("\nAddress          : %s\n", contactbook.address);
  printf("\nOccupation       : %s\n", contactbook.occupation);

  system("clear");
  
 }

 if (!match)
 {
  printf("The name does not exist\n");
  printf("or it might had been deleted\n");
  system("clear");
 }
   
 fclose(Contactbook);
 fclose(temp);
 remove("Contactbook.txt");
 rename("temp.txt","Contacbook.txt");
 
}

//initializing the Search function
void Search()
{
  
        char Find[100];
 int Found=0, ch;
 if((Contactbook=fopen("Contactbook.txt","r"))==NULL)
  printf("----The File Is Empty----\n");
 else
 {
  system("clear");
  printf("Enter Name to search:\n");
  while ((ch = getchar()) != '\n' && ch != EOF);
  fgets(Find, 100, Contactbook);
  while(fgets(Find, 100, Contactbook)&& Found==0)
  {
    fscanf(Contactbook,"%s %s %s %s %s ",contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);
    if(strcmp(Find,contactbook.names)==0)
    Found=1;
  }
  if(Found)
  {
     system("clear");
    printf("Name: %s\n",contactbook.names);
    printf("Birthday: %s\n",contactbook.birthday);
    printf("Handphone: %s\n",contactbook.hp);
    printf("Address : %s\n",contactbook.address);
    printf("Occupation: %s\n",contactbook.occupation);
      
  } 
  else if(!Found)
  {
    printf("SORRY FILE NOT FOUND\n");
          
  }
  fclose(Contactbook);
       
     }
}

//initializing the Edit function
void Edit()
{
 int match = 0, ch;
        char Find[100];

 system("clear");
 temp = fopen("temp.txt", "w+");

 if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
  printf("----The file is empty----\n");
 else
 {
  printf("Enter his/her name that you want to edit:\n");
  while ((ch = getchar()) != '\n' && ch != EOF);
  fgets(Find, 100, Contactbook);
  
 }
 
 while(fgets(Find, 100, Contactbook) != NULL)
     {
      fscanf(Contactbook, "%s %s %s %s %s\n", contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);  
     }  

 if(strcmp(Find, contactbook.names) !=0)
  fprintf(temp,"%s %s %s %s %s\n", contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);

 else
 {
  match = 1;

  printf("\nHis/Her Name     : %s\n", contactbook.names);
  printf("\nBirth of Date    : %s\n", contactbook.birthday);
  printf("\nHandphone Number : %s\n", contactbook.hp);
  printf("\nAddress          : %s\n", contactbook.address);
  printf("\nOccupation       : %s\n", contactbook.occupation);

  system("clear");
  
 }

 if (!match)
 {
  printf("The name does not exist\n");
  printf("or it might had been deleted\n");
  system("clear");
 }
   
 fclose(Contactbook);
 fclose(temp);
 remove("Contactbook.txt");
 rename("temp.txt","Contacbook.txt");

 Contactbook = fopen("Contactbook.txt","w+");

        printf("");
 fgets(contactbook.garbage,100,stdin);
 printf("Enter the properties to edit\n");
 printf("\nName:");
        fflush(stdin);
 fgets(contactbook.names, 100, stdin);
        printf("\nBirthday:");
        fgets(contactbook.birthday,100, stdin);
        printf("\nHandphone: ");
        fgets(contactbook.hp, 100, stdin);
        printf("\nAddress: ");
        fgets(contactbook.address,100, stdin);
        printf("\nOccupation: ");
        fgets(contactbook.occupation,100, stdin);
 
 fprintf(Contactbook, "%s %s %s %s %s\n", contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);    
    
    
 fclose(Contactbook);
}

//initializing the List function
void List()
{
 Contactbook = fopen("Contactbook.txt","a+");
 
 if(Contactbook != NULL)
      {
         system("clear"); 
      printf("Current Entries: \n");
         while(fgets(contactbook.names, 100, Contactbook) != NULL)
         {
            printf("%s", contactbook.names);
            printf("\n");
         }
         printf("End of File \n");
         fclose(Contactbook);
        
      }
}

//initializing the Exit function
void Exit()
{
 printf("Thank You For Using our Contactbook. Have a Nice Day\n");
 system("clear");
}

To amano.

When you are posting bulk code, please use only [CODE][/CODE] tags. Thank you.

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.