Hi I am doing a project using binary files.I've wrote a small bit of code with no compile errors.I'm using Dev-C++ compiler(If you know a better free one please tell me)When I run it the command prompt opens and closes straight away and I don't know why This has happened me before but I need this project done VERY SOON!! can anyone help?!?This is the code I've written.
It's planned to be a banking system If anyone has Ideas for this I would also like to hear them.Thanks for listening.
C Code:
#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("2->Search by Name\n");
printf("3->Search by Account Number\n");
printf("4->Save added Customers\n");
do
{
printf("Make your selection: ");
gets(str);
i = atoi(str);
} 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])
{
return;
}
printf("Enter customer name (ENTER to Quit):");
gets(cat[i] -> name);
if(!*cat[i] -> name)
break;
gets(cat[i] -> AccNo);
gets(cat[i] -> address);
gets(temp);
cat[i] -> date = (unsigned) atoi(temp);
gets(temp);
cat[i] -> month = (unsigned) atoi(temp);
}
top =i ;
}
/* Search for customer */
void customer_search(void)
{
char name[80];
int i , found;
gets(name);
found = 0;
for (i=0; i<top; i++)
{
if (!strcmp(name,cat[i]->name))
{
display(i);
found = 1;
}
}
if(!found)
}
void AccNo_search(void)
{
char AccNo[80];
int i , found;
gets(AccNo);
found = 0;
for (i=0; i<top; i++)
{
if (!strcmp(AccNo,cat[i] -> AccNo))
{
display(i);
found = 1;
}
}
if(!found)
}
/*Display entries*/
int display(int i)
{
system("CLS");
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])
{
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])
{
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)
{
exit(1);
}
if(fwrite(&top,sizeof top,1,fp) !=1)
{
printf("Error Writting count.\n");
exit(1);
}
for(i=0 ; i < top ; i++)
{