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