hi dear fellow
i have tried to add structure array in my program and i had a few issue to add. please can anyone tell me how can i add a structure array in the following program

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

struct books{
    char name[22], book[25],date1[10], date2[10],id[7],bid[7];

} el;

void data();

int main(void)
{
    void transaction(),modify(),view(),search(),delet();
    int num;

  do
     {
       system("cls");  
         printf("\n\t\t***************************************");
         printf("\n\t\t\t\t\t\t\t\t\t\t\t\t\t Welcome To BOOK MENU ");
         printf("\n\t\t***************************************\n");
         printf("\t\t1. Append Record and Book");
         printf("\n\t\t2. Modify record");
         printf("\n\t\t3. Search Book");
         printf("\n\t\t4. Delete Record");  
         printf("\n\t\t5. Dispaly All");
         printf("\n\t\t6. Exit");
         printf("\n\t\t***************************************");
         printf("\n\t\t Choice:");
         scanf("%d", &num);

         switch(num)
         {
        case 1:
                transaction();
                break;
           case 2:
                modify();
                break;
           case 3:search();
               break;
           case 4: delet();
               break;
            case 5:
                view();
                break;
            case 6:
                return 0;   
                break;
             default :
                printf(" \n\t\t wrong value !!!");
                break;
         }

      getch();
   }while(num!=6);



    }
                                  //function for laon transaction
//-------------------------------------------------------------------------------------
void data()
{


    fflush(stdin);

    printf("\n\t\tUsername Name: ");
       gets(el.name);fflush(stdin);

      printf("\t\t User Id: ");
     gets(el.id);fflush(stdin);

     printf("\t\tBook Name: ");fflush(stdin);
     gets(el.book);

     printf("\t\tBOOK Id: ");
     gets(el.bid);fflush(stdin);

    printf("\t\tToday Date: ");     
    gets(el.date1);fflush(stdin);

    printf("\t\tDate of return: ");
    gets(el.date2);  
     fflush(stdin);



}
void transaction()
 {


    system("cls");

    FILE *fp;
    fp = fopen("data.txt","a");
     fflush(stdin);

      printf("\n\t\t\t************************\n");
     printf("\n\t\t\t    Loan Transaction");
     printf("\n\t\t\t************************\n");
     data();

   fwrite(&el,sizeof(el),1,fp);
          printf("\n\t\t\tSuccessful Added");

    fclose(fp);
}
                                   //function for modification
//----------------------------------------------------------------------------
void modify()
{
    FILE*fp,*fp1;
    fp=fopen("data.txt","r");
    fp1=fopen("temp.txt","w");

    char name[22];
    int found=0;
    fflush(stdin);

    printf("\n\t\tUsername: ");
    gets(name);fflush(stdin);

    while(1)
    {
    fread(&el,sizeof(el),1,fp);
      if(feof(fp))
    {
    break;
    }
    if(strcmp(name,el.name)==0)
    {
    found=1;

   data();
     printf("\n\t\tmodification done!!");
     fwrite(&el,sizeof(el),1,fp1);


    }else
    {
        fwrite(&el,sizeof(el),1,fp1);
    }

    }
    fclose(fp);
    fclose(fp1);
    if(found==0)
    {
    printf("\n\t\tSorry no record found!!");
    }
    else
    {
    fp=fopen("data.txt","w");
    fp1=fopen("temp.txt","r");
    while(1)
    {
         fread(&el,sizeof(el),1,fp1);
      if(feof(fp1))
     {
    break;
     }
    fwrite(&el,sizeof(el),1,fp);

   }
    }
    fclose(fp);
    fclose(fp1);
}
                           // function for dispaly the content of the file 
//--------------------------------------------------------------------------------------
void view()
{
    FILE *fp;

    fp = fopen("data.txt","r");

    system("cls");

    struct books el;

    printf("\n\t\t\t**************************");
    printf("\n\t\t\t  The content of the file");
    printf("\n\t\t\t**************************\n");
    printf("\n%-13s %-13s %-13s %-13s %-13s %-13s\n","Username","UserID","BookName","BookID","TodayDate","IssueDay");

    while(1)
    {
      fread(&el,sizeof(el),1,fp);
      if(feof(fp))
    {
    break;
    }
    printf("\n%-13s %-13s %-13s %-13s %-13s %-13s",el.name,el.id,el.book,el.bid,el.date1,el.date2);

    }



    fclose(fp);


}
                           // function for search a special book
//-------------------------------------------------------------------------------
void search()
{
    FILE *fp;

    fp = fopen("data.txt","r");


     char name[22];
    int found=0;
    system("cls");
    printf("\n\t\t\t**************************");
    printf("\n\t\t\t  Search");
    printf("\n\t\t\t**************************\n");

    fflush(stdin);
    printf("\n\t\tBook Name: ");
   gets(name);fflush(stdin);

    while(1)
    {
      fread(&el,sizeof(el),1,fp);
      if(feof(fp))
    {
    break;
    }
      if(strcmp(name,el.book)==0)
      {
          found=1;
          printf("\n%-13s %-13s %-13s %-13s %-13s %-13s\n","Username","UserID","BookName","BookID","TodayDate","IssueDay");

    printf("\n%-13s %-13s %-13s %-13s %-13s %-13s",el.name,el.id,el.book,el.bid,el.date1,el.date2);
    break;
      }
    }

    if(found==0)
    {

    printf("\n\t\t Sorry record no found!!");
    }

    fclose(fp);

}
                        //function for delete record
//------------------------------------------------------------------------
void delet()
{

    FILE*fp,*fp1;
    fp=fopen("data.txt","r");
    fp1=fopen("temp.txt","w");

    char name[22];
    int found=0;
    fflush(stdin);

    printf("\n\t\tUsername: ");
    gets(name);fflush(stdin);

    while(1)
    {
    fread(&el,sizeof(el),1,fp);
      if(feof(fp))
    {
    break;
    }
    if(strcmp(name,el.name)==0)
    {
    found=1;
    }else
    {
        fwrite(&el,sizeof(el),1,fp1);
    }

    }
    fclose(fp);
    fclose(fp1);
    if(found==0)
    {
    printf("\n\t\tSorry no record found!!");
    }
    else
    {
    fp=fopen("data.txt","w");
    fp1=fopen("temp.txt","r");
    while(1)
    {
         fread(&el,sizeof(el),1,fp1);
      if(feof(fp1))
     {
    break;
     }
    fwrite(&el,sizeof(el),1,fp);

   }
     printf("\n\t\t Deleting done!!");


    }
    fclose(fp);
    fclose(fp1);

}

The same way you make an array of anything.

   char                 nameOfStruct[20]; // This makes an array of 20 char objects
   struct exampleObject nameOfStruct[40]; // This makes an array of 40 exampleObjects

Edited 2 Years Ago by Moschops

ok let said for example struct books eli[20]; so here i have made a structure array of 20 now my question is how am i going to manage of the structure array like first then second ect.

struct books eli[20]; // create the array

eli[0] = something; // set value of first element in array
eli[1] = something else; // set value of second element in array
function(eli[19]); // do something using last element in array

If you're going to do the same thing to a lot of elements, use a loop.

Edited 2 Years Ago by Moschops

can u please give an exa;ple for loops looking to my program

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