Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct customer
{
int accno;
char name[30];
float balance;
}cust;
struct trans
{
int accno;
char trans_type;
float amount;
}tran;
int size;
int create();
int list();
int status();
int main()
{
int op;
size=sizeof(cust);
while(1)
{
printf("1.Create\n2.List\n3.Status\n5.Exit\n");
scanf("%d",&op);
switch(op)
{
case 1: create();
break;
case 2: list();
break;
case 3: status();
break;
case 5: exit(1);
default: clrscr();break;
}
}
getchar();
return 0;
}
int status()
{
FILE *fp,*ft;
int iden;
clrscr();
printf("ACCOUNT NUMBER\t\tTRANSACTION\t\tSTATUS\t\tUPDATE\n");
fp=fopen("Customer.dat","rb+");
if(fp==NULL)
{
puts("CANNOT OPEN CUSTOMER FILE!!\n");
return;
}
ft=fopen("Transactions.dat","rb+");
if(ft==NULL)
{
puts("CANNOT OPEN TRANSACTION FILE!!\n");
return;
}
while(1)
{
iden=fread(&cust,size,1,fp);
fread(&tran,sizeof(tran),1,ft);
if(iden==0)
break;
else
{
//printf("%d\t%s\t%f\n",cust.accno,cust.name,cust.balance);
if(tran.trans_type=='D')
{
cust.balance=cust.balance+tran.amount;
fseek(fp,-size,SEEK_CUR);
fwrite(&cust,size,1,fp);
printf("%d\t\tDEPOSIT\t\tSUCCESSFUL\t\t%f\n",cust.accno,cust.balance);
}
else
{
if(cust.balance-tran.amount<100)
printf("%d\t\tWITHDRAWAL\t\tFAILED\t\t%f\n",cust.accno,cust.balance);
else
{
cust.balance=cust.balance-tran.amount;
fseek(fp,-size,SEEK_CUR);
fwrite(&cust,size,1,fp);
printf("%d\t\tWITHDRAWAL\t\tFAILED\t\t%f\n",cust.accno,cust.balance);
}
}
}
}
fclose(fp);fclose(ft);
return 0;
}
int list()
{
FILE *fp,*ft;
int iden=0;
fp=fopen("Customer.dat","rb");
if(fp==NULL)
{
puts("CANNOT OPEN CUSTOMER FILE!!\n");
exit(1);
}
ft=fopen("Transactions.dat","rb");
if(ft==NULL)
{
puts("CANNOT OPEN TRANSACTION FILE!!\n");
exit(2);
}
while(1)
{
iden=fread(&cust,sizeof(cust),1,fp);
fread(&tran,sizeof(tran),1,ft);
if(iden==0)
break;
printf("%s\t%d\t%f\n",cust.name,cust.accno,cust.balance);
printf("%d\t%c\t%f\n\n",tran.accno,tran.trans_type,tran.amount);
}
fclose(ft);fclose(fp);
return 0;
}
int create()
{
FILE *fp,*ft;
int iden;
char another;
fp=fopen("Customer.dat","rb+");
if(fp==NULL)
{
fp=fopen("Customer.dat","wb");
if(fp==NULL)
{
puts("CANNOT OPEN CUSTOMER FILE!!\n");
exit(1);
}
}
ft=fopen("Transactions.dat","rb+");
if(ft==NULL)
{
ft=fopen("Transactions.dat","wb");
if(ft==NULL)
{
puts("CANNOT OPEN TRANSACTION FILE!!\n");
exit(2);
}
}
fseek(fp,0,SEEK_END);
fseek(ft,0,SEEK_END);
another='Y';
while(another=='Y')
{
printf("ENTER Name, ACCOUNT NUMBER AND BALANCE\n");
scanf("%s%d%f",&cust.name,&cust.accno,&cust.balance);
printf("TRANSACTIONS TYPE [D FOR DEPOSIT,W FOR WITHDRAWAL] ,ENTER ACCOUNT NUMBER,AND AMOUNT\n");
scanf("%s%d%f",&tran.trans_type,&tran.accno,&tran.amount);
fwrite(&cust,sizeof(cust),1,fp);
fwrite(&tran,sizeof(tran),1,ft);
fflush(stdin);
printf("DO YOU WANT TO ADD ANOTHER RECORD [Y/N]\n");
scanf("%c",&another);
}
clrscr();
fclose(fp);fclose(ft);
return 0;
}
Thank you guys, but my problem is in the status function it only prints the first account and skips the rest, sorry i didn't mention that earlier..! I just can't figure out what i did wrong with this stupid program!