C programming. The following is my code: float getBalance(float bal); float with
ID: 3758312 • 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
Program:
#include<stdio.h.
#include<conio.h>
void min()
{
float getBalance(float bal);
float withdraw(float bal,float amount);
float deposit(float bal, float amount);
float update(float bal);
{
int i;
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;
FILE *myFile;
myFile = fopen("bankfile.txt","r");
if (myFile == NULL)
{
printf("Can't open input file! ");
exit(1);
}
else
{
fscanf(myFile, "%d", &n); //read first number to determine number of accounts
}
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);
}
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++;
}
}
float getBalance(float bal)
{
return bal;
}
float withdraw(float bal,float amount)
{
if(bal > amount)
{
return bal -= amount;
}
else
{
printf("Sorry, you can't withdraw that much ");
return bal;
}
}
float deposit(float bal,float amount)
{
return bal += amount;
}
float update(float bal)
{
return bal*= INTEREST;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.