yo. i made a program which is supposed to simulate a bank account. it prompts the user to input the account number and pin code; and then matches the input with that of the file "accounts.dat". It verifies the username and password, but the file doesn't update. when you deposit or withdraw, it does the arithmetics correctly, but the file content remains the same. i tried changing the "r" in the fopen tag to "w+" and also tried "r+" tag but it still doesn't write anything. the "w+" even erases everything in the accounts.dat file.
i wish you could find time to help. thanks!
Code:
#include<stdio.h>
#include<string.h>
#include<conio.h>
struct acct{
char acn[6];
char pin[5];
float bal;};
void main()
{
struct acct n,x[100];
int i1,i2,q,i=0,g,h;
float amt;
FILE *fp,*fp1.*fp2;
fp=fopen("ACCOUNTS.DAT","r+");
clrscr();
printf("ACCOUNT NUMBER: ");
scanf("%s",&n.acn);
printf("PIN CODE: ");
scanf("%s",&n.pin);
while(fscanf(fp,"%s\n%f\n%s\n",&x[i].acn,&x[i].bal,&x[i].pin)!=EOF)
{
i1=strcmp(n.acn,x[i].acn);
i2=strcmp(n.pin,x[i].pin);
if(i1==0&&i2==0)
{
g=i;
};
i++;
};
if(strcmp(n.acn,x[g].acn)==0&&strcmp(n.pin,x[g].pin)==0)
{
printf("Access permitted.");
printf("\n\nEnter the number which corresponds to the transaction that you want to make:\n\t1. Transaction: Deposit\n\t2. Transaction: Withdraw\n\t3. Balance Inquiry\n\tOption number: ");
scanf("%d",&q);
if(q==1)
{
clrscr();
printf("You chose option number 1.\n\tTRANSACTION: DEPOSIT\n\n\tPlease enter the amount that you would like to deposit: P ");
scanf("%f",&amt);
x[g].bal=x[g].bal+amt;
printf("Thankyou for depositing.\n\tYour new balance is P %10.2f", x[g].bal);
h=i;
i=0;
fp1=fopen("ACCOUNTS.DAT","w");
fp2=fopen("ACCOUNTS.DAT","a");
for(i=0;i<h;i++)
{
fprintf(fp2,"\n%s\n%f\n%s\n",&x[i].acn,&x[i].bal,&x[i].pin);
};
getch();
exit(1);
}
if(q==2)
{
clrscr();
printf("You chose option number 2.\n\tTRANSACTION: WITHDRAW\n\n\tPlease enter the amount that you would like to withdraw: P ");
scanf("%f",&amt);
x[g].bal=x[g].bal-amt;
printf("Thankyou for withdrawing.\n\tYour new balance is P %10.2f", x[g].bal);
h=i;
i=0;
fp1=fopen("ACCOUNTS.DAT","w");
fp2=fopen("ACCOUNTS.DAT","a");
for(i=0;i<h;i++)
{
fprintf(fp2,"\n%s\n%f\n%s\n",&x[i].acn,&x[i].bal,&x[i].pin);
};
getch();
exit(1);
}
if(q==3)
{
clrscr();
printf("You chose option number 3.\n\tBALANCE INQUIRY");
printf("Your account balance is %10.2f.",x[g].bal);
getch();
exit(1);
}
else
{
printf("Thankyou for logging in.");
};
}
else
{
printf("Access denied.");
};
fclose(fp);
getch();
}