C. Assignment: Based on the material covered in class on I/O files, and the mate
ID: 3600378 • Letter: C
Question
C. Assignment: Based on the material covered in class on I/O files, and the material in your text, do the following: 1. 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 0.0 d. Sample output is: * THE MCC BANK STATEMENT * ---------------------------------------------------------- Opening Balance = 324.56 ---------------------------------------------------------- New Balance Deposit = 420.32 744.88 Withdrawal = 3.54 741.34 Withdrawal = 87.56 653.78 ---------------------------------------------------------- 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. 2. Save the program as lab5acct.cpp . 3. Create an input file called lab5chek.dat a. Have your program read the input from the file and process the data until end of file. 4. Write the output to an output file, call it lab5acct.rpt i. The input data file is as follows: 1224.56 420.32 -3.54 -87.56 500.00 -87.25 225.75 -72.72 -33.67 -100.10 50.50 -99.99 -19.99 0.0 D. Test Data: Calculate what the ending balance and total number of transactions should be before you execute the program. E. For this program, hand in: 1. A copy of the source code 2. A print out of the input file 3. A print out of the output file.
please follow instruction
Explanation / Answer
Program
------------
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
class Account {
int id;
std::vector<int> numOfTrans;
std::vector<string> transType;
double balance;
public:
void setID(int ID);
int getID();
void setNumOfTrans(int numOfTrans_Lcl);
void setTransType(string transType_Lcl);
void setBalance( double wid );
double getBalance();
std::vector<int> getNumOfTrans();
std::vector<string> getTransType();
};
void Account::setID( int ID ) {
id = ID;
}
int Account::getID( ) {
return id;
}
void Account::setNumOfTrans( int numOfTrans_Lcl) {
numOfTrans.push_back(numOfTrans_Lcl);
}
void Account::setTransType( string transType_Lcl) {
transType.push_back(transType_Lcl);
}
void Account::setBalance( double wid ) {
balance = wid;
}
double Account::getBalance() {
return balance;
}
std::vector<int> Account::getNumOfTrans(){
return numOfTrans;
}
std::vector<string> Account::getTransType(){
return transType;
}
int main()
{
std::ifstream file("lab5chek.dat");
std::string line = "324.56 420.32 -3.54 -87.56 0.0";
std::vector<Account> tokens;
int acountNum = 0;
while(std::getline(file, line)) { // ' ' is the default delimiter
std::istringstream iss(line);
Account *f1 = new Account;
string balance;
f1->setBalance(0.0);
acountNum++;
f1->setID(acountNum);
size_t pos = 0;
std::string token;
while ((pos = line.find(" ")) != std::string::npos) {
token = line.substr(0, pos);
//std::cout << token << std::endl;
int balance = std::atoi(token.c_str());
if(balance > 0 ){
f1->setTransType("New Balance Deposit=");
}else{
f1->setTransType("New Balance Withdrawal =");
}
f1->setBalance(f1->getBalance() + balance);
f1->setNumOfTrans(balance);
line.erase(0, pos + 1);
//std::cout << f1->getBalance() << std::endl;
}
tokens.push_back(*f1);
}
vector<Account>::iterator it1;
vector<string> finalStmt;
finalStmt.push_back("THE MCC BANK STATEMENT ---------------------------------------------------------- ");
finalStmt.push_back("Opening Balance = 0.0 ");
for ( it1 = tokens.begin(); it1 != tokens.end(); ++it1 ) {
std::vector<int> numOfTrans = it1->getNumOfTrans();
vector<int>::iterator it2;
it2 = numOfTrans.begin();
std::vector<string> transType = it1->getTransType();
vector<string>::iterator it3;
it3 = transType.begin();
while(it2 != numOfTrans.end() && it3 != transType.end()){
finalStmt.push_back(*it3 + " " + std::to_string(*it2));
++it2;
++it3;
}
finalStmt.push_back(" ");
finalStmt.push_back("Closing Balance = "+ std::to_string(it1->getBalance()));
}
ofstream outputFile;
outputFile.open("lab5acct.rpt");
vector<string>::iterator finalItr;
for ( finalItr = finalStmt.begin(); finalItr != finalStmt.end(); ++finalItr ) {
std::cout << *finalItr << std::endl;
outputFile << *finalItr << std::endl;
}
outputFile.close();
return 0;
}
Output
---------------------
$g++ -o main *.cpp $main THE MCC BANK STATEMENT ---------------------------------------------------------- Opening Balance = 0.0 New Balance Deposit= 324 New Balance Deposit= 420 New Balance Withdrawal = -3 New Balance Withdrawal = -87 Closing Balance = 654.000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.