Hello I'm doing a C project for College.It is a banking system where you take in customer name address etc, Print a statement based on a month by month bases.I need to use binary files I have two versions(well Attempts) of this project the one I'm having biggest trouble with the one to follow.One problem is the the programs stops at a certain point which is noted in the code I can not fix this.It only happens when I and entered and saved one persons details.Can anyone help with the initial problem and maybe Ideas to make this a good project I need to get a good grade or fail PLEASE HELP!!

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


#define MAX 100


int menu(void);
int display(int i);
void customer_search(void);
void AccNo_search(void);
void enter(void);
void save(void);
void load(void);

struct catalog
{
       char name[80];
       char AccNo[6];
       char address[80];
       unsigned date;
       unsigned char month;
      
}*cat[MAX];

struct bank
{
        char BankName[20];
        unsigned LastAccNum;
        unsigned sortCode;
        
}*bank[MAX];

int top = 0;


int main(void)
{
              int choice;
              
              load();
              
              do
                {
                         choice = menu();
                         
                         switch(choice)
                               { 
                                       case 1: enter();
                                            break;
                                       
                                       case 2: customer_search();
                                            break;
                                            
                                       case 3: AccNo_search();
                                            break;
                                            
                                       case 4: save();
                               }
                }
       
       while(choice !=5);
system("PAUSE");
       return 0;

}   
            
            
            /* Return a menu selection*/
            
int menu(void)
{
    int i;
    char str[80];
    
         printf("\t\n\aEircom Billing System\n\n");             
         printf("1->Add Customer\n");
         printf("2->Search by Name\n");
         printf("3->Search by Account Number\n");
         printf("4->Save added Customers\n");
         printf("5->Quit\n");     
              
           do
                {
                                 printf("Make your selection:  ");
                                 gets(str);
                                 i = atoi(str);
                                 printf("\n");                        
                } while(i < 1 || i > 5);   
                
           return i;
}                    
                                      
    /*Enter customer into file */
    
    
void enter(void)
{
     int i;
     char temp[80];
     
     
     for (i = top; i<MAX ; i++)
            {
                                    
                   cat[i] = malloc(sizeof(struct catalog));
                   
                          if(!cat[i])
                                 {
                                                 printf("Out Of Memory\n");
                                                 return;
                                 }
                  
                 printf("Enter customer name (ENTER to Quit):");
                 gets(cat[i] -> name);                
                 
                             if(!*cat[i] -> name)
                                         break;
                 
                 printf("\nEnter AccNo:  ");
                 gets(cat[i] -> AccNo);                
                 
                 printf("\nEnter address:  ");
                 gets(cat[i] -> address);                
                 
                 printf("\nEnter year: ");
                 gets(temp);                
                 cat[i] -> date = (unsigned) atoi(temp);
                 
                 printf("\nEnter month: ");
                 gets(temp);                
                 cat[i] -> month = (unsigned) atoi(temp);
                 
                 
                
                }
             
     top =i ;
                
}
    
/* Search for customer */

void customer_search(void)
{
     char name[80];
     int i , found;
     
         printf("Name:  ");
         gets(name);
         
         found = 0;
         
               for (i=0; i<top; i++)
                   {
                         if (!strcmp(name,cat[i]->name))
                                        {
                                                 display(i);
                                                 found = 1;
                                                 printf("\n");
                                        }
                   }
                   
             if(!found)
                   printf("Not Found\n");

}



void AccNo_search(void)
{
     char AccNo[80];
     int i , found;
     
         printf("AccNo:  ");
         gets(AccNo);
         
         found = 0;
         
               for (i=0; i<top; i++)
                   {
                         if (!strcmp(AccNo,cat[i] -> AccNo))
                                        {
                                                 display(i);
                                                 found = 1;
                                                 printf("\n");
                                        }
                   }
                   
             if(!found)
                   printf("Not Found\n");
}


                       
             /*Display entries*/    
             
int display(int i)
{
                  system("CLS");
                  printf("%s\n",cat[i]->AccNo);
                  printf("Account Holder Name: %s\n",cat[i]->name); 
                  printf("address: %s\n",cat[i]->address);
                  printf("Year and Month: %u %u ",cat[i]->date,cat[i]->month);
}       
    
                        /*load file*/
                        
void load(void)
{
     FILE *fp,*fp2;
     int i;
     
         if((fp =fopen("index.bin","rb"))==NULL )//|| fp2 =fopen("bank.bin","rb"))==NULL)
                {
                                              printf("File does not exist on disk");
                                              return;
                }
                
                
         if(fread(&top,sizeof top,1,fp) !=1)
                              {
                                        printf("Error Reading count");
                                        exit(1);
                              }
                              
         for (i=0 ; i < top ; i++)
                  {
                        cat[i] = malloc(sizeof(struct catalog));
                               
                               if (!cat[i])
                                  {
                                           printf("Out of Memory->\n");
                                           top = i-1;
                                           break;
                                  }
                                  
                               if(fread(cat[i], sizeof(struct catalog),1,fp) !=1)
                                                {
                                                              printf("Error reading catalog data->\n");
                                                              exit(1);
                                                }
                  }
     
     
     
     for (i=0 ; i < top ; i++)
                  {
                        bank[i] = malloc(sizeof(struct bank));
                               
                               if (!bank[i])
                                  {
                                           printf("Out of Memory->\n");
                                           top = i-1;
                                           break;
                                  }
                                  
                               if(fread(bank[i], sizeof(struct bank),1,fp) !=1)
                                                {
                                                              printf("Error reading catalog data->\n");
                                                              exit(1);
                                                }
                  }
     fclose(fp2);  
     fclose(fp);
}


/* Save the catalog file */

void save(void)
{
    FILE *fp;
     int i;
     
           if((fp =fopen("index.bin","w+"))==NULL)
                       {
                                              printf("Cant open file->\n");
                                              exit(1);
                       }
           if(fwrite(&top,sizeof top,1,fp) !=1)
                               {
                                        printf("Error Writting count.\n");
                                        exit(1);
                               }
         
         for(i=0 ; i < top ; i++)
                 {
                       if(fwrite(cat[i], sizeof(struct catalog), 1, fp) != 1)
                                         printf("Error writing count.\n");
                                         exit(1);
                 }
                 
         fclose(fp);
               
               
}

>>One problem is the the programs stops at a certain point which is noted in the code
Please identify the line number. Did you forget to mark it because I don't see it.

Sorry yes I did forget to mark it starting to get frustrated trying to make it work.Its line 242 cant get it to work it will run once but if I enter data and save It stops working?

line 87: never ever use gets() for anything because it can overwrite the input buffer and cause your program to crash. Instead, use fgets() to limit the length of the text.

lines 292 and 293 need braces around that multi-line if statement. That's why your program just exits.

thanks for the advise I'll tell you if it works tomorrow need sleep now

sorry tried your suggestions but I am still encountering the same problems any other Ideas please?If you have any?

worked for me. Here's the code I used. I made a minor change at line 291 but that was only for debugging. There are several other problems that I didn't mention, such as use of gets() function. You should use fgets() instead because gets() can destroy your program if you type more characters then the input buffer can hold. For example: Acct is 6 characters, try typing this number: 123456789012345 <Enter> and see what will happen.

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


#define MAX 100


int menu(void);
int display(int i);
void customer_search(void);
void AccNo_search(void);
void enter(void);
void save(void);
void load(void);

struct catalog
{
       char name[80];
       char AccNo[6];
       char address[80];
       unsigned date;
       unsigned char month;
      
}*cat[MAX];

struct bank
{
        char BankName[20];
        unsigned LastAccNum;
        unsigned sortCode;
        
}*bank[MAX];

int top = 0;


int main(void)
{
              int choice;
              
              load();
              
              do
                {
                         choice = menu();
                         
                         switch(choice)
                               { 
                                       case 1: enter();
                                            break;
                                       
                                       case 2: customer_search();
                                            break;
                                            
                                       case 3: AccNo_search();
                                            break;
                                            
                                       case 4: save();
                               }
                }
       
       while(choice !=5);
system("PAUSE");
       return 0;

}   
            
            
            /* Return a menu selection*/
            
int menu(void)
{
    int i;
    char str[80];
    
         printf("\t\n\aEircom Billing System\n\n");             
         printf("1->Add Customer\n");
         printf("2->Search by Name\n");
         printf("3->Search by Account Number\n");
         printf("4->Save added Customers\n");
         printf("5->Quit\n");     
              
           do
                {
                                 printf("Make your selection:  ");
                                 gets(str);
                                 i = atoi(str);
                                 printf("\n");                        
                } while(i < 1 || i > 5);   
                
           return i;
}                    
                                      
    /*Enter customer into file */
    
    
void enter(void)
{
     int i;
     char temp[80];
     
     
     for (i = top; i<MAX ; i++)
            {
                                    
                   cat[i] = malloc(sizeof(struct catalog));
                   
                          if(!cat[i])
                                 {
                                                 printf("Out Of Memory\n");
                                                 return;
                                 }
                  
                 printf("Enter customer name (ENTER to Quit):");
                 gets(cat[i] -> name);                
                 
                             if(!*cat[i] -> name)
                                         break;
                 
                 printf("\nEnter AccNo:  ");
                 gets(cat[i] -> AccNo);                
                 
                 printf("\nEnter address:  ");
                 gets(cat[i] -> address);                
                 
                 printf("\nEnter year: ");
                 gets(temp);                
                 cat[i] -> date = (unsigned) atoi(temp);
                 
                 printf("\nEnter month: ");
                 gets(temp);                
                 cat[i] -> month = (unsigned) atoi(temp);
                 
                 
                
                }
             
     top =i ;
                
}
    
/* Search for customer */

void customer_search(void)
{
     char name[80];
     int i , found;
     
         printf("Name:  ");
         gets(name);
         
         found = 0;
         
               for (i=0; i<top; i++)
                   {
                         if (!strcmp(name,cat[i]->name))
                                        {
                                                 display(i);
                                                 found = 1;
                                                 printf("\n");
                                        }
                   }
                   
             if(!found)
                   printf("Not Found\n");

}



void AccNo_search(void)
{
     char AccNo[80];
     int i , found;
     
         printf("AccNo:  ");
         gets(AccNo);
         
         found = 0;
         
               for (i=0; i<top; i++)
                   {
                         if (!strcmp(AccNo,cat[i] -> AccNo))
                                        {
                                                 display(i);
                                                 found = 1;
                                                 printf("\n");
                                        }
                   }
                   
             if(!found)
                   printf("Not Found\n");
}


                       
             /*Display entries*/    
             
int display(int i)
{
                  system("CLS");
                  printf("%s\n",cat[i]->AccNo);
                  printf("Account Holder Name: %s\n",cat[i]->name); 
                  printf("address: %s\n",cat[i]->address);
                  printf("Year and Month: %u %u ",cat[i]->date,cat[i]->month);
}       
    
                        /*load file*/
                        
void load(void)
{
     FILE *fp = 0,*fp2 = 0;
     int i;
     
         if((fp =fopen("index.bin","rb"))==NULL )//|| fp2 =fopen("bank.bin","rb"))==NULL)
                {
                                              printf("File does not exist on disk");
                                              return;
                }
                
                
         if(fread(&top,sizeof top,1,fp) !=1)
                              {
                                        printf("Error Reading count");
                                        exit(1);
                              }
                              
         for (i=0 ; i < top ; i++)
                  {
                        cat[i] = malloc(sizeof(struct catalog));
                               
                               if (!cat[i])
                                  {
                                           printf("Out of Memory->\n");
                                           top = i-1;
                                           break;
                                  }
                                  
                               if(fread(cat[i], sizeof(struct catalog),1,fp) !=1)
                                                {
                                                              printf("Error reading catalog data->\n");
                                                              exit(1);
                                                }
                  }
     
     
     
     for (i=0 ; i < top ; i++)
                  {
                        bank[i] = malloc(sizeof(struct bank));
                               
                               if (!bank[i])
                                  {
                                           printf("Out of Memory->\n");
                                           top = i-1;
                                           break;
                                  }
                                  
                               if(fread(bank[i], sizeof(struct bank),1,fp) !=1)
                                                {
                                                              printf("Error reading catalog data->\n");
                                                              exit(1);
                                                }
                  }
     fclose(fp2);  
     fclose(fp);
}


/* Save the catalog file */

void save(void)
{
    FILE *fp;
     int i;
            fp =fopen("index.bin","w+");
           if(fp == NULL)
            {
                printf("Cant open file->\n");
                exit(1);
            }
           if(fwrite(&top,sizeof top,1,fp) !=1)
                               {
                                        printf("Error Writting count.\n");
                                        exit(1);
                               }
         
         for(i=0 ; i < top ; i++)
                 {
                     int x = fwrite(cat[i], sizeof(struct catalog), 1, fp);
                       if(x != 1)
                       {
                                         printf("Error writing count.\n");
                                         exit(1);
                       }
                 }
                 
         fclose(fp);
               
               
}

Ya it's working now again thanks very much for the help no doubt I'll be back to you before I finish this Thanks Very much though!!!!!!!!!!!!!!!!!!!!

This question has already been answered. Start a new discussion instead.