Write a C++ program that prints the opening balance of a checking account follow
ID: 3758488 • Letter: W
Question
Write a C++ program that prints the opening balance of a checking account followed by the
closing balance of the checking account.
a. The input data file contains the opening balance as the first data value, followed by an unknown number of individual transactions.
b. A negative number represents a withdrawal, and a positive number represents a deposit.
c. For example, if the input is:
324.56 420.32 -3.54 -87.56
d. Sample output is:
* BANK STATEMENT *
-------------------------------------------------
Opening Balance = 324.56
-------------------------------------------------
Deposit = 420.32
Withdrawal = 3.54
Withdrawal = 87.56
-------------------------------------------------
Closing Balance = 653.78
-------------------------------------------------
There are 3 transactions in this statement.
e. The last line of the output containing total number of transactions has to be counted from
with the program to determine the total number of transactions in the statement.
THE PROGRAM MUST USE INPUT AND OUTPUT DATA FILES.
Thanks in advance!
Explanation / Answer
input.dat
324.56 420.32 -3.54 -87.56
output.dat
* BANK STATEMENT *
-------------------------------------------------
Opening Balance = 324.56
-------------------------------------------------
Deposit = 420.32
Withdrawal = 3.54
Withdrawal = 87.56
-------------------------------------------------
Closing Balance = 653.78
-------------------------------------------------
There are 3 transactions in this statement.
C++ Program Code:
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main()
{
int num;
float x,sum;
ifstream fin;
fin.open("input.dat", ios::in);
ofstream fout;
fout.open("output.dat", ios::out);
sum=0;
num=0;
while(!fin.eof()){
fin >> x;
num=num+1;
if (num==1) {
fout<<" * BANK STATEMENT *"<<endl;
fout<<"-------------------------------------------------"<<endl;
fout<<"Opening Balance = "<<x<<endl;
fout<<"-------------------------------------------------"<<endl;
}
else{
if (x>0) {
fout<<"Deposit = "<<abs(x)<<endl;
}
else if (x<0){
fout<<"Withdrawal = "<<abs(x)<<endl;
}
}
sum=sum+x;
}
fout<<"-------------------------------------------------"<<endl;
fout<<"Closing Balance = "<<sum<<endl;
fout<<"-------------------------------------------------"<<endl;
fout<<"There are "<<num-1<<" transactions in this statement."<<endl;
fin.close();
fout.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.