Construct a C program, check.c, that reads the details of a monthly checking acc
ID: 3676804 • Letter: C
Question
Construct a C program, check.c, that reads the details of a monthly checking account and outputs a bank statement summarizing these transactions. An input file, account.txt, contains a list of transactions for the checking account for one month. Each line of the input file consists of a one character transaction code along with a float containing the amount of the transaction. Valid transaction codes are as follows:
• I – initial balance brought forth from previous month
• D - deposit
• C - check cashed
As each transaction is entered, you should output on a single line the type of transaction, the amount of the transaction (separate columns for deposits and checks cashed), and the balance after the transaction has been processed:
Transaction :
Initial Balance 478.83
Transaction :
Deposit 127.45
Balance: 606.28
Transaction :
Deposit 619.84
Balance: 1226.12
Transaction:
Check Cashed 945.12
Balance: 281.00 ...
The banks service charges (which should be deducted from the balance at the end of the month) are as follows:
• $3.00 per month to maintain the account.
• $0.06 for each check cashed.
• $0.03 for each deposit made.
• $5.00 overdraft whenever a check cashed brings the balance below $0.00.
Note: Do not assess the overdraft service charge when the transaction is a deposit into an overdrawn account that does not bring the balance above zero.
The bank statement should also include the following summary information:
-Total number of deposits:
-Total amount of deposits:
-Total number of checks:
-Total amount of checks:
-Total service charge:
-Opening balance:
-Closing balance:
Shown below is the main() function that you should use for your program. Please do NOT modify the main() function.
int main(void) {
char code;
double amount, service, balance;
double amtCheck, amtDeposit, openBalance, closeBalance;
int numCheck, numDeposit;
if (!(fpIn = fopen("acct.txt", "r"))) {
printf("account.txt could not be opened for input.");
fprintf(fp, "account.txt could not be opened for input.");
exit(1);
}
if (! (fpOut = fopen("csis.txt", "w"))) {
printf("csis.txt could not be opened for output.");
fprintf(fp, "csis.txt could not be opened for output.");
exit(1);
}
amount = 0.0;
service = 0.0;
balance = 0.0;
amtCheck = 0.0;
amtDeposit = 0.0;
openBalance = 0.0;
closeBalance = 0.0;
numCheck = 0;
numDeposit = 0;
outputHeaders();
while (!feof(fpIn)) {
fscanf(fpIn, "%c %lf ", &code, &amount);
if (code == 'I') {
initialBalance(amount, &balance, &service, &openBalance);
}
else if (code == 'D') {
deposit(amount, &balance, &service, &numDeposit, &amtDeposit);
}
else {
check(amount, &balance, &service, &numCheck, &amtCheck);
}
}
closeBalance = balance - service;
outputSummary(numDeposit, amtDeposit, numCheck, amtCheck, openBalance, service, closeBalance);
fclose(fpIn);
fclose(fpOut);
return 0;
}
You will need to write five functions and their prototypes for your program:
-outputHeader()
-initialBalance()
-deposit()
-check()
-outputSummary()
These functions will use counters and summations to keep track of the different components of the checking account as well as output specific information relating to the bank statement. Note that some of the function parameters are passed as input parameters and some are passed as input-output parameters. Be sure to use the indirection operator (*) correctly in your functions.
Remember that the main() function should appear as the first function in the program. Be sure to use function prototypes for each of the functions that are used in your program.
The contents of account.txt are shown below:
I 478.83
D 127.45
D 619.84
C 945.12
C 4.76
D 32.81
C 1.06
D 184.32
C 495.18
C 141.81
C 255.10
D 250.00
D 123.88
D 245.05
D 873.25
C 981.12
D 317.84
C 812.73
D 606.12
You should not be using any global variables, other than declarations for file pointers, in your program other than file pointers. A global variable is a variable declared outside of main().
Explanation / Answer
The below program is successfully compiled. Can you please try compiling it again ?
Program Name - check.c
Compiler: Xcode 5
#include <stdio.h>
FILE *fp, *csis;
void outputHeaders();
void initialBalance(double amount, double *balance, double *service, double *openBalance);
void deposit(double amount, double *balance, double *service, int *numDeposit, double *amtDeposit);
void check(double amount, double *balance, double *service, int *numCheck, double *amtCheck);
void outputSummary(int numDeposit, double amtDeposit, int numCheck, double amtCheck, double openBalance, double service, double closeBalance);
int main(void)
{
char code;
double amount, service, balance;
double amtCheck, amtDeposit, openBalance, closeBalance;
int numCheck, numDeposit;
if (!(fp = fopen("account.txt", "r"))) {
printf("account.txt could not be opened for input.");
exit(1);
}
if (!(csis = fopen("csis.txt", "w"))) {
printf("csis.txt could not be opened for output.");
exit(1);
}
amount = 0;
service = 0;
balance = 0;
amtCheck = 0;
amtDeposit = 0;
openBalance = 0;
closeBalance = 0;
numCheck = 0;
numDeposit = 0;
outputHeaders();
while (!feof(fp)) {
fscanf(fp, "%c %lf ", &code, &amount);
if (code == 'I') {
initialBalance(amount, &balance, &service, &openBalance);
}
else if (code == 'D') {
deposit(amount, &balance, &service, &numDeposit, &amtDeposit);
}
else {
check(amount, &balance, &service, &numCheck, &amtCheck);
}
}
closeBalance = balance - service;
outputSummary(numDeposit, amtDeposit, numCheck, amtCheck, openBalance, service, closeBalance);
fclose(csis);
fclose(fp);
return 0;
}
void outputHeaders()
{
printf("Transaction Deposit Check Balance ");
printf("----------- ------- ----- ------- ");
fprintf(csis, "Transaction Deposit Check Balance ");
fprintf(csis, "----------- ------- ----- ------- ");
}
void initialBalance(double amount, double *balance, double *service, double *openBalance)
{
*openBalance = amount;
*balance = *openBalance;
*service = 3.00;
printf("Initial Balance %8.2lf ", *openBalance);
fprintf(csis, "Initial Balance %8.2lf ", *openBalance);
}
void deposit(double amount, double *balance, double *service, int *numDeposit, double *amtDeposit)
{
*balance += amount;
*amtDeposit += amount;
*numDeposit += 1;
*service += 0.03;
printf("Deposit %8.2lf %8.2lf ", *amtDeposit, *balance);
fprintf(csis, "Deposit %8.2lf %8.2lf ", *amtDeposit, *balance);
}
void check(double amount, double *balance, double *service, int *numCheck, double *amtCheck)
{
*balance -= amount;
*amtCheck += amount;
*numCheck += 1;
if (*balance < 0.00) {
*service += 5.00;
}
else {
*service += 0.06;
}
printf("Check %9.2lf %8.2lf ", *amtCheck, *balance);
fprintf(csis, "Check %9.2lf %8.2lf ", *amtCheck, *balance);
}
void outputSummary(int numDeposit, double amtDeposit, int numCheck, double amtCheck, double openBalance, double service, double closeBalance)
{
printf(" Total number deposits: %d ", numDeposit);
printf("Total amount deposits: $%.2lf ", amtDeposit);
printf(" Total number checks: %d ", numCheck);
printf(" Total amount checks: $%.2lf ", amtCheck);
printf("Total service charge: $%.2lf ", service);
printf(" Opening balance: $%.2lf ", openBalance);
printf(" Closing balance: $%.2lf ", closeBalance);
fprintf(csis, " Total number deposits: %d ", numDeposit);
fprintf(csis, "Total amount deposits: $%.2lf ", amtDeposit);
fprintf(csis, " Total number checks: %d ", numCheck);
fprintf(csis, " Total amount checks: $%.2lf ", amtCheck);
fprintf(csis, "Total service charge: $%.2lf ", service);
fprintf(csis, " Opening balance: $%.2lf ", openBalance);
fprintf(csis, " Closing balance: $%.2lf ", closeBalance);
}
account.txt :-
I 478.83
D 127.45
D 619.84
C 945.12
C 4.76
D 32.81
C 1.06
D 184.32
C 495.18
C 141.81
C 255.10
D 250.00
D 123.88
D 245.05
D 873.25
C 981.12
D 317.84
C 812.73
D 606.12
csis.txt :-
Transaction Deposit Check Balancecsis.txt
----------- ------- ----- -------
Initial Balance 478.83
Deposit 127.45 606.28
Deposit 747.29 1226.12
Check 945.12 281.00
Check 949.88 276.24
Deposit 780.10 309.05
Check 950.94 307.99
Deposit 964.42 492.31
Check 1446.12 -2.87
Check 1587.93 -144.68
Check 1843.03 -399.78
Deposit 1214.42 -149.78
Deposit 1338.30 -25.90
Deposit 1583.35 219.15
Deposit 2456.60 1092.40
Check 2824.15 111.28
Deposit 2774.44 429.12
Check 3636.88 -383.61
Deposit 3380.56 222.51
Total number deposits: 10
Total amount deposits: $3380.56
Total number checks: 8
Total amount checks: $3636.88
Total service charge: $23.54
Opening balance: $478.83
Closing balance: $198.97