Im having a few problems with completeing a adress book program in c (not c++ jus regular c), it requires a minimun of 5 functions so i have decided to use add, search, veiw now in order to complete i wud like to keep the add function but make a new function tht shows that the data is saved to a file, also i wud like to divide my veiw function into 2 seperate function, one being a veiw all and the other being a veiwing a specific name...... Any help wud be greatly appreciated and the sooner is the better...... thanks in advance
(attached is the current state of my problem)

Attachments
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#define addRecords 4;



void printMenu(void);
void Add(void);
void inputStuff(void);
void readAllRecords(void);
void Search(void);
void ShowRecordList(void);
void Saverecord(void);




struct Ladies{
       
       char fName[30];
       char lName[30];
       int Age;
       char Address[20];
       char phone[13];
       
       //struct LadiesAddress address;
       
};


       

struct Ladies ladies1[2];//Global declaration
int i;
int    index;
int choice;
char enterAnother = 'y';
char searchName[30];
struct Ladies lady;



int main()
{
    do{
    
    printMenu();
    scanf("%d", &choice);
    fflush(stdin);
    switch(choice)
    {
       case 1:
            Add(); /*Add New Records*/
       break;
       case 2:
            readAllRecords();
       break;
       case 3:
            Search();
       break;
       case 4:
            ShowRecordList();
              break;     
       case 5:
            ShowRecordList();
            break;
       case 6:
            exit(0);
       break;
    }
    system("pause");
    system("CLS"); 
}while(choice !=6);
    system("pause");
    return 0;
}

void printMenu(void)
{
     
            printf("+=============================================+\n");
             printf("+           MY BLACK BOOK                     +\n");
              printf("+=============================================+\n");
    
     printf("Select choice number and press enter\n");
     printf("1. Add\n");
     printf("2. View\n");     
     printf("3. Search\n");
     printf("4. Show Records\n");
     printf("6. Exit\n");
     
     }
     
void Add(void)
{
     FILE *pAppend;

     pAppend = fopen("names.txt","a");
     
     if(pAppend == NULL)
        printf("No File\n");
     else{
         while(enterAnother == 'y')
         {
    
           printf("First Name: \n");
           gets(ladies1[i].fName);
    
           printf("Last Name:\n");
           gets(ladies1[i].lName);
    
           printf("Age: \n");
           scanf("%d", &ladies1[i].Age);
                               
           printf("Phone Number:\n");
           scanf("%d", ladies1[i].phone);
           
           printf("Address:\n");
           scanf("%s", ladies1[i].Address);
           fflush(stdin);
             
           
          /* fprintf(pAppend, "%30s\t%30s\t%d\t%d\t%s\n"
           , ladies1[i].fName
           , ladies1[i].lName 
           , ladies1[i].Age
           , ladies1[i].phone 
           , ladies1[i].Address);*/
           
           printf("Do You want to enter another\n");
           scanf("%c",&enterAnother);
           fflush(stdin);
         }
}
fclose(pAppend);
}

void Saverecord(void)
{
     FILE *pAppend;

     pAppend = fopen("names.txt","a");
     
if(pAppend == NULL)
        printf("No File\n");
     else{
         while(enterAnother == 'y')
         {
    
           printf("First Name: \n");
           gets(ladies1[i].fName);
    
           printf("Last Name:\n");
           gets(ladies1[i].lName);
    
           printf("Age: \n");
           scanf("%d", &ladies1[i].Age);
                               
           printf("Phone Number:\n");
           scanf("%d", ladies1[i].phone);
           
           printf("Address:\n");
           scanf("%s", ladies1[i].Address);
           fflush(stdin);
             
           
           fprintf(pAppend, "%30s\t%30s\t%d\t%d\t%s\n"
           , ladies1[i].fName
           , ladies1[i].lName 
           , ladies1[i].Age
           , ladies1[i].phone 
           , ladies1[i].Address);
           
           printf("Do You want to enter another\n");
           scanf("%c",&enterAnother);
           fflush(stdin);
         }
}
fclose(pAppend);
}
//Function to output results    
void readAllRecords(void)
{
     FILE *pRead;
     
     pRead = fopen("names.txt","r");
     
     if(pRead == NULL)
              printf("No File exist for Reading\n");
     else{
     index = 1;
     //
     while(!feof(pRead))
     {
     fscanf(pRead,"%s", &ladies1[i].fName);     
     //
     fscanf(pRead,"%s", &ladies1[i].lName);
     
     //
     fscanf(pRead,"%d", &ladies1[i].Age);
     
     fscanf(pRead,"%d", &ladies1[i].phone);
     
     fscanf(pRead, "%s\n", & ladies1[i].Address);
     
     //
     printf("++===============++=================++\n");     
     printf("Record Number:\t%d\n",index);
     printf("First Name: ");
     printf("%s\n", ladies1[i].fName);
     
     printf("Last Name: ");
     printf("%s\n", ladies1[i].lName);
     
     printf("Age: ");
     printf("%d\n", ladies1[i].Age);
     
     printf("Phone Number:");
     printf("%d\n", ladies1[i].phone);
     
     printf ("Address:");
     printf ("%s\n", ladies1[i].Address);
     index++;
     }
     }
     fclose(pRead);
}

void Search(void)
{
     FILE *pSearch;
     
     pSearch = fopen("names.txt","r");
    
     if(pSearch == NULL)
              printf("No File exist for Reading\n");
     else{
     
    printf("--Search By Name--\n");
    printf("Enter First Name in which the record is recorded:\t");
    gets(searchName);
    index =0;
    while(!feof(pSearch))
    {
        fscanf(pSearch,"%s%s%d%d%s\n"
                             , &lady.fName 
                             , &lady.lName
                             , &lady.Age
                             , &lady.phone
                             , &lady.Address
        );
        
        
        if(strcmp(lady.fName, searchName)==0)
        {
                index++;
                printf("%i\t%s\t%s\t%d\t%d\t%s\n",index
                             , lady.fName 
                             , lady.lName
                             , lady.Age
                             , lady.phone
                             , lady.Address);
        }                
      }
    } 
    if (index==0) printf("\nNO RECORDS WERE FOUND\n");
fclose(pSearch);
}


           

void ShowRecordList(void)
{
     FILE *pSearch;
     
     pSearch = fopen("names.txt","r");
    
     if(pSearch == NULL)
              printf("No File exist for Reading\n");
     else{
     
    printf("--Record List--\n");
    printf("%ID\tFName\tLname\tAge\t# No\tAddress\n");
    printf("--------------------------------------------------------\n");
    index =0;
    while(!feof(pSearch))
    {
        fscanf(pSearch,"%s%s%d%d%s\n"
                             , &lady.fName 
                             , &lady.lName
                             , &lady.Age
                             , &lady.phone
                             , &lady.Address
        );
        
     
                index++;
                printf("%i\t%s\t%s\t%d\t%d\t%s\n",index
                             , lady.fName 
                             , lady.lName
                             , lady.Age
                             , lady.phone
                             , lady.Address);
     } 
    }                     
    
fclose(pSearch);                    
}

You need to change the file name extension, right off!

Recommendations:

1) Dot cpp is the default file extension for C++, so it can cause much confusion and wasted time. Change it to just dot c, and then we're at least running on the same language compiler

2) gets() is very unsafe, since it makes no check on the length of the string it is getting from the user. Use
fgets(bufferName, sizeof(bufferName), stdin), instead.

3) flushing stdin is non-standard and doesn't always work. Use getchar() to pull the newline off the keyboard buffer, as needed.

4) feof() doesn't act as you suspect or hope.
while((fgets(buff, sizeof(buff), fp)) != NULL) { is much better.

where buff is the name of a char array big enough to receive the line of text (all of it).

You haven't said what your problems with coding up these changes are, so I don't know what else to advise, right now.

Once you know how to find and display one record from the file, there's no difficulty in displaying a page full of records at once, prompting the user to hit enter to continue, and displaying the next page of data, until either the end of the file, or the user presses q to quit the display of records.

Programs involve very explicit code and use very exact logic (good or bad, the program uses it). So the more specific your questions are, the better replies you will receive.

Just posting up a program and saying "I want to change this and that and something else", is not going to come off well. You need to put some "skin" into the post, and put up your attempt to write these changes, and when you're stuck, THEN post up specific questions about fixing it.

That's how the forum work best - we can give the best advice because we know the specifics, and it takes far less time and effort. You get the best advice for the problem, because you gave the best description or example of it.

Comments
Yes
This article has been dead for over six months. Start a new discussion instead.