how to do a fund transfer at transaction function?

Here the code,~

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<windows.h>

typedef struct {
int date, month, year, hr, min, sec;
}DATETIME;

typedef struct {
char accNo[6], PIN[6], name[31], gender, address[31], state[11], contact[12];
int balance;
DATETIME lastTxn;
}CUSTOMER;

int atm, choice, txnCode, customerCode;
CUSTOMER temp, customer[10];
FILE *fptr1, *fptr2;
SYSTEMTIME t;
void transaction(int TxnCode);
int withdrawal(int balance, int withdraw);


void transaction(int TxnCode) {
int withdraw, transfer;

switch(TxnCode) {
//Withdrawal
case 3: {
printf("WITHDRAWAL\n\n");

printf("Your Balance is RM%d\n", temp.balance);

printf("Please key in the amount you want to withdraw:\nRM");
scanf("%d", &withdraw);

//Update balance
customer[customerCode].balance = withdrawal(temp.balance, withdraw);

//Update last transaction date & time
GetLocalTime(&t);
customer[customerCode].lastTxn.date = t.wDay;
customer[customerCode].lastTxn.month = t.wMonth;
customer[customerCode].lastTxn.year = t.wYear;
customer[customerCode].lastTxn.hr = t.wHour;
customer[customerCode].lastTxn.min = t.wMinute;
customer[customerCode].lastTxn.sec = t.wSecond;

printf("Withdrawal Successful. Your balance is now RM%d\n", customer[customerCode].balance);

//Rewrite customer's details into a temporary text file
for(int i = 0; i < 10; i++) {
fprintf(fptr2, "%s|%s|%s|%c|%s|%s|%s|%d|%d %d %d %d %d %d\n",
customer[i].accNo, customer[i].PIN, customer[i].name, customer[i].gender,
customer[i].address, customer[i].state, customer[i].contact,
customer[i].balance, customer[i].lastTxn.date, customer[i].lastTxn.month, customer[i].lastTxn.year,
customer[i].lastTxn.hr, customer[i].lastTxn.min, customer[i].lastTxn.sec);
}

break;
}
//Funds Transfer
case 4: {
printf("FUNDS TRANSFER\n\n");
printf("Your Balance is RM%d\n", temp.balance);
printf("Key in the Account Number of the Recipient:\t\t_____\b\b\b\b\b");
scanf("%s", &temp.accNo);
fflush(stdin);
printf("Receiver Name:%s\t\t\tAmount to Transfer:RM%d", temp.name, temp.balance);
printf("Transferred Successfully. Your balamce is now RM%d\n", customer[customerCode].balance);
//Rewrite customer's details into a temporary text file
for(int i = 0; i < 10; i++) {
fprintf(fptr2, "%s|%s|%s|%c|%s|%s|%s|%d|%d %d %d %d %d %d\n",
customer[i].accNo, customer[i].PIN, customer[i].name, customer[i].gender,
customer[i].address, customer[i].state, customer[i].contact,
customer[i].balance, customer[i].lastTxn.date, customer[i].lastTxn.month, customer[i].lastTxn.year,
customer[i].lastTxn.hr, customer[i].lastTxn.min, customer[i].lastTxn.sec);
}

break;
}
//Update last transaction date & time
GetLocalTime(&t);
customer[customerCode].lastTxn.date = t.wDay;
customer[customerCode].lastTxn.month = t.wMonth;
customer[customerCode].lastTxn.year = t.wYear;
customer[customerCode].lastTxn.hr = t.wHour;
customer[customerCode].lastTxn.min = t.wMinute;
customer[customerCode].lastTxn.sec = t.wSecond;

}

//Withdrawal Calculator
int withdrawal(int balance, int withdraw) {
int final;
final = balance - withdraw;
return final;
}
//transfers Calculator
int transfers(int balance, int transfer){
int final;
final = balance - transfer;
return final;
}

void main() {
system("color 8a");

SYSTEMTIME t;
int choice, txnCode;


/*TARBANK Logo
printf("\n\ ========== __ ~~~~> ~~~~>\n");
printf("\n\||===||===|| / \ | @@ > | @@ >\n");
printf("\n\ || / /_ \ | @ > | >\n");
printf("\n\ || / /__\ \ | << | <<\n");
printf("\n\ || / / \ \ | \ \ | @@ >\n");
printf("\n\ || /_/ \_\ | \ \ |___ >\n");
*/

//Display Today's Date and Time
GetLocalTime(&t);
printf("\t\tDate! Today is %d/%d/%d and \n\
it's %d:%d:%d now.\n\n", t.wDay, t.wMonth, t.wYear, t.wHour, t.wMinute, t.wSecond);

printf("\t\tCustomer:Press 1\n");
printf("\t\tManager:Press 2\n\n");
printf("\t\tYour choice: ");
scanf("%d", &choice);
if(choice == 1) {
fptr1 = fopen("Customer.txt", "r");
fptr2 = fopen("Customer_temp.txt", "w");

//Check if customer.txt can be opened or not
if(fptr1 == NULL || fptr2 == NULL) {
printf("ERROR in opening file!Try again.....");
exit(-1);
}

//Randomly generate ATM No.(6-10)
srand(time(NULL));
atm = (rand() % 5) + 6;
//Display ATM No.
printf("ATM No: %d\n", atm);

//Prompt and read user's A/C No.
printf("\t\tPlease enter your A/C No:\t\t_____\b\b\b\b\b");
scanf("%s", &temp.accNo);
fflush(stdin);

//Prompt and read user's PIN No.
printf("\t\tPlease enter your PIN No:\t\t_____\b\b\b\b\b");
scanf("%s",temp.PIN);
fflush(stdin);

//Read customer.txt
while(!feof(fptr1)) {
for(int i = 0; i < 10; i++) {
fscanf(fptr1, "%[^|]|%[^|]|%[^|]|%c|%[^|]|%[^|]|%[^|]|%d|%d%d%d%d%d%d",
&customer[i].accNo, &customer[i].PIN, &customer[i].name, &customer[i].gender,
&customer[i].address, &customer[i].state, &customer[i].contact,
&customer[i].balance, &customer[i].lastTxn.date, &customer[i].lastTxn.month, &customer[i].lastTxn.year,
&customer[i].lastTxn.hr, &customer[i].lastTxn.min, &customer[i].lastTxn.sec);

//Check if the results match(update is pending)
if(strcmp(temp.accNo, customer[i].accNo) == 0 && strcmp(temp.PIN, customer[i].PIN) == 0) {
customerCode = i;

strcpy(temp.name, customer[i].name);
temp.balance = customer[i].balance;

temp.gender = customer[i].gender;
strcpy(temp.address, customer[i].address);
strcpy(temp.state, customer[i].state);
strcpy(temp.contact, customer[i].contact);
temp.lastTxn.year = customer[i].lastTxn.year;
temp.lastTxn.month = customer[i].lastTxn.month;
temp.lastTxn.date = customer[i].lastTxn.date;
temp.lastTxn.hr = customer[i].lastTxn.hr;
temp.lastTxn.min = customer[i].lastTxn.min;
temp.lastTxn.sec = customer[i].lastTxn.sec;
}
}
}

printf("\n");
printf("Name: %s\n\n", temp.name);
printf("Balance: RM%d\n\n", temp.balance);

printf("Last Transaction Date: %d/%d/%d %d:%d:%d\n\n", temp.lastTxn.date, temp.lastTxn.month, temp.lastTxn.year,
temp.lastTxn.hr, temp.lastTxn.min, temp.lastTxn.sec);

printf("\t\t[1] Withdrawal\n");
printf("\t\t[2] Funds Transfer\n\n");

printf("\t\tYour choice: ");
scanf("%d", &choice);

//Cash Deposit(txnCode = 1)
//Cheque Deposit(txnCode = 2)

//withdrawal
if(choice == 1) {
txnCode = 3;
transaction(txnCode);
}

//Transferr
else if(choice == 2) {
txnCode = 4;
transaction(txnCode);
}

//Close all text files
fclose(fptr1);
fclose(fptr2);
}
here my customer text file~

12345|54321|Tao Ka Cheng |M|12 Jalan 3/45 KL |Wilayah P|012-3456789|5000| 3 10 2015 2 30 00
12350|05321|Lim Ke Yuan |F|13 Jln 5/50 Gtown |Penang |013-4567890|4000| 4 10 2015 4 30 00
12355|55321|Lin Lao ba |F|14 Jalan 7/15 KK |Sabah |014-5678901|2000| 4 10 2015 2 00 00
12360|06321|Chau Ah Gua |M|15 Jalan 2/25 Ipoh |Perak |015-6789012|3000| 5 10 2015 4 30 00
12365|56321|Marempit Chong|M|16 Jalan 1/34 Muar |Johor |016-7890123|5000| 7 10 2015 8 30 00
12370|53344|Kang Mei Mei |F|17 Jalan 6/45 SP |Sungai P |012-1234489|6000| 6 10 2015 2 30 00
12375|02543|Lu Ka Jik |M|18 Jln 9/50 Gtown |Penang |013-5456790|8000| 8 10 2015 4 30 00
12380|08333|Wong Ji Ji |F|19 Jalan 1/60 KK |Sabah |014-5754301|1000| 12 10 2015 2 10 00
12385|01232|Chau JohnCena |M|20 Jalan 45/65 Ipoh|Perak |015-6778912|4500| 17 10 2015 4 40 00
12390|33452|Jessyna Lai |F|21 Jalan 12/34 Muar|Johor |016-1232023|7000| 27 10 2015 8 50 00
A transfer is quite simple. Let's say you want to transfer 100 RM from Account A to account B.
First you need to check if account A has sufficient funds.
If yes you withdraw the amount from account A and deposit it in account B.

Topic archived. No new replies allowed.