C programming. The following is my code: float getBalance(float bal); float with
ID: 3758344 • Letter: C
Question
C programming. The following is my code:
float getBalance(float bal);
float withdraw(float bal,float amount);
float deposit(float bal, float amount);
float update(float bal);
int main(){
//initializing variables
int count = 0;
int i = 0;
int j = 1;
int n = 0;
int k = 0;
int acctNum[30]= {0};
float ogAccts[30] = {0};
float newAccts[30] = {0};
float bal = 0;
float amount = 0;
char action, B, W, U, D, Z;
int actNumber=0;
//opening files
FILE *myFile;
myFile = fopen("bankfile.txt","r");
//error checking that file opened correctly
if (myFile == NULL)
{
printf("Can't open input file! ");
exit(1);
}
else
{
fscanf(myFile, "%d", &n); //read first number to determine number of accounts
}
//taking the remaining lines and checking them against
//the activities available as well as the amount to use
for(i = 1; i <= n; i++)
{
fscanf(myFile, "%d %f", &acctNum[i], &ogAccts[i]);
printf("Account number : %d, Balance: %.2lf ", acctNum[i], ogAccts[i]);
}
while(fscanf(myFile," %c %d %f", &action, &acctNum[i], &amount)!=EOF)
{
if((action == 'B' ) || (action == 'U') || (action == 'Z'))
{
amount = 0 ;
printf("Action: %c Account Number: %d Amount: %.2lf ", action, acctNum[i], amount);
}
//printf("Action: %c Account Number: %d Amount: %.2lf ", action, acctNum[i], amount);
switch(action){
case 'B':
case 'b':
bal=ogAccts[j];
printf("test b ");
printf("Account : %d ", acctNum[i]);
printf("Balance : %.2lf ", ogAccts[j]);
ogAccts[j]= getBalance(bal);
printf("New Balance: %.2lf ", ogAccts[j]);
break;
case 'W':
case 'w':
printf("test w ");
bal=ogAccts[j];
printf("Account : %d ", acctNum[i]);
printf("Balance : %.2lf ", ogAccts[j]);
ogAccts[j]= withdraw(bal,amount);
printf("New Balance: %.2lf ", ogAccts[j]);
break;
case 'D':
case 'd':
bal=ogAccts[j];
printf("test d ");
printf("Account : %d ", acctNum[i]);
printf("Balance : %.2lf ", ogAccts[j]);
ogAccts[j]= deposit(bal,amount);
printf("New Balance: %.2lf ", ogAccts[j]);
break;
case 'U':
case 'u':
bal=ogAccts[j];
printf("test u ");
printf("Account : %d ", acctNum[i]);
printf("Balance : %.2lf ", ogAccts[j]);
ogAccts[j]= update(bal);
printf("New Balance: %.2lf ", ogAccts[j]);
break;
case 'Z':
case 'z':
printf("pause ");
}
j++;
i++;
}
}
//getBalance function
float getBalance(float bal){
return bal;
}
//withdrawl function
float withdraw(float bal,float amount){
if(bal > amount){
return bal -= amount;
}//check to make sure you can't overdraw account
else{
printf("Sorry, you can't withdraw that much ");
return bal;
}
}
//deposit function
float deposit(float bal,float amount){
return bal += amount;
}
//update function
float update(float bal)
{
return bal*= INTEREST;
}
//printf("Action: %c Account Number: %d Amount: %.2lf ", action, acctNum[i], amount);
****END OF MY CODE******
This program takes an input from text file, then stores account numbers and dollar amounts in an array.
bankfile.txt :
3
123 20000.00
467 10000.00
499 2500.00
W 467 2000.00
D 123 50.00
B 123
U 467
W 499 125.00
Z
4128003447789564
02/17
017
I can't get the program to associate the account number with a dollar amount. When it will do it for the first 3 operations, but after that the account balances show 0. Any help would be greatly appreciated.
Explanation / Answer
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
struct acc_type
{
char bank_name[20];
char bank_branch[20];
char acc_holder_name[30];
int acc_number;
char acc_holder_address[100];
float available_balance;
};
struct acc_type account[20];
int num_acc;
void Create_new_account();
void Cash_Deposit();
void Cash_withdrawl();
void Account_information();
void Log_out();
void display_options();
int main()
{
char option;
num_acc=0;
while(1)
{
display_options();
printf("Please enter any options (1/2/3/4/5/6) ");
printf("to continue : ");
option = getch();
printf("%c ", option);
switch(option)
{
case '1': Create_new_account();
break;
case '2': Cash_Deposit();
break;
case '3': Cash_withdrawl();
break;
case '4': Account_information();
break;
case '5': return 0;
case '6': system("cls");
break;
default : system("cls");
printf("Please enter one of the options");
printf("(1/2/3/4/5/6) to continue ");
break;
}
}
return 0;
}
void display_options()
{
printf(" 1. Create new account ");
printf("2. Cash Deposit ");
printf("3. Cash withdrawl ");
printf("4. Account information ");
printf("5. Log out ");
printf("6. Clear the screen and display available ");
printf("options ");
}
void Create_new_account()
{
char bank_name[20];
char bank_branch[20];
char acc_holder_name[30];
int acc_number;
char acc_holder_address[100];
float available_balance = 0;
fflush(stdin);
printf(" Enter the bank name : ");
scanf("%s", &bank_name);
printf(" Enter the bank branch : ");
scanf("%s", &bank_branch);
printf(" Enter the account holder name : ");
scanf("%s", &acc_holder_name);
printf(" Enter the account number(1 to 10): ");
scanf("%d", &acc_number);
printf(" Enter the account holder address : ");
scanf("%s", &acc_holder_address);
strcpy(account[acc_number-1].bank_name,bank_name);
strcpy(account[acc_number-1].bank_branch,bank_branch);
strcpy(account[acc_number-1].acc_holder_name,
acc_holder_name);
account[acc_number-1].acc_number=acc_number;
strcpy(account[acc_number-1].acc_holder_address,
acc_holder_address);
account[acc_number-1].available_balance=available_balance;
printf(" Account has been created successfully ");
printf("Bank name : %s " ,
account[acc_number-1].bank_name);
printf("Bank branch : %s " ,
account[acc_number-1].bank_branch);
printf("Account holder name : %s " ,
account[acc_number-1].acc_holder_name);
printf("Account number : %d " ,
account[acc_number-1].acc_number);
printf("Account holder address : %s " ,
account[acc_number-1].acc_holder_address);
printf("Available balance : %f " ,
account[acc_number-1].available_balance);
}
void Account_information()
{
register int num_acc = 0;
while(strlen(account[num_acc].bank_name)>0)
{
printf(" Bank name : %s " ,
account[num_acc].bank_name);
printf("Bank branch : %s " ,
account[num_acc].bank_branch);
printf("Account holder name : %s " ,
account[num_acc].acc_holder_name);
printf("Account number : %d " ,
account[num_acc].acc_number);
printf("Account holder address : %s " ,
account[num_acc].acc_holder_address);
printf("Available balance : %f " ,
account[num_acc].available_balance);
num_acc++;
}
}
void Cash_Deposit()
{
auto int acc_no;
float add_money;
printf("Enter account number you want to deposit money:");
scanf("%d",&acc_no);
printf(" The current balance for account %d is %f ",
acc_no, account[acc_no-1].available_balance);
printf(" Enter money you want to deposit : ");
scanf("%f",&add_money);
while (acc_no=account[acc_no-1].acc_number)
{
account[acc_no-1].available_balance=
account[acc_no-1].available_balance+add_money;
printf(" The New balance for account %d is %f ",
acc_no, account[acc_no-1].available_balance);
break;
}acc_no++;
}
void Cash_withdrawl()
{
auto int acc_no;
float withdraw_money;
printf("Enter account number you want to withdraw money:");
scanf("%d",&acc_no);
printf(" The current balance for account %d is %f ",
acc_no, account[acc_no-1].available_balance);
printf(" Enter money you want to withdraw from account ");
scanf("%f",&withdraw_money);
while (acc_no=account[acc_no-1].acc_number)
{
account[acc_no-1].available_balance=
account[acc_no-1].available_balance-withdraw_money;
printf(" The New balance for account %d is %f ",
acc_no, account[acc_no-1].available_balance);
break;
}acc_no++;
}
This above code is reading data from console instead of txt file. it may help to solve your problem.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.