Jump to content

Project Bank System: but Command prompt wont come on

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
hallinan

hallinan

    Newbie

  • Members
  • Pip
  • 4 posts
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.

[HIGHLIGHT="C"]#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);


}





[/HIGHLIGHT]

#2
hallinan

hallinan

    Newbie

  • Members
  • Pip
  • 4 posts
Ok did a bit of debugging and came up with this,
in the load() method here->
[HIGHLIGHT="C"]
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)

{
//The Program runs to here once wraps around then fails
printf("Error reading catalog data->\n");

exit(1);

}

}

[/HIGHLIGHT]




Anyone any Ideas I need to have this submitted by monday?

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Put a System("PAUSE") before the Exit(1).

Also, what I usually do is open a command window manually to run programs created with Dev-C++. That eliminates the need worry about the System("pause") commands entirely.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog