USING VISUAL STUDIO copy data: ( transactions.txt) 123 d45.10 d50.45 d198.56 w45
ID: 3770372 • Letter: U
Question
USING VISUAL STUDIO
copy data: (transactions.txt)
123 d45.10 d50.45 d198.56 w45.67
345 w34.00 d4.56 w45.13 d23.23 w23.12
639 d1000.34 d1234.56 w34.33 w345.87 w22.13
890 d345.67 d123.67 d45.99 d45.99 w34.77
666 d66.60 d666.66 d6.66 d66.6 d6666.66
copy data:(accounts.txt)
123 daffy 34.67
345 goofy 123.89
639 sneezy 1945.76
890 dopey 12345667.90
666 grumpy 666.66
Explanation / Answer
working c++ code compiled on ideone.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
struct PersonAcct // struct account holds customer info
{
int acct_num; // customer account number
string name; // customers name
int acct_bal; // customers account balance
};
void Accounts_update(PersonAcct &accountStructure, ifstream &account);
void Transactions_update(PersonAcct &accountStructure, ifstream &transaction , double &withdrawls, double &deposits, int &num_of_deposits, int &num_of_withdrawls);
void Print_Report(PersonAcct &accountStructure, ofstream &monthlyReport, int num_of_withdrawls, int num_of_deposits, double withdrawls, double deposits);
string get_month();
int get_account_num();
int main()
{
ifstream account; //the accounts.txt file stream
ifstream transaction; //the transactions.txt file stream
ofstream monthlyReport; //the output report
string month; //the month
PersonAcct accountStructure;//structure, the current account
int prog = 'y';
double w, d; //the value of each
int withdrawls, deposits; //number of each
while(prog == 'y')
{
accountStructure.acct_num = get_account_num();
month = get_month(); //input to be taken
account.open("accounts.txt"); //files to open
transaction.open("transactions.txt");
monthlyReport.open("account_report_" + month + ".txt");
if( !account || !transaction ) // test for unsuccessful file opening
{
cerr << "Cannot open file: " << "accounts.txt or transactions.txt" << endl << endl;
exit (0);
}
Accounts_update(accountStructure, account); // from account.txt
Transactions_update(accountStructure, transaction, w, d, withdrawls, deposits);
cout << "NO PROBLEM BEFORE OUTREPORT";
Print_Report(accountStructure, monthlyReport, withdrawls, deposits, w, d);
account.close(); //files to open
transaction.close();
monthlyReport.close();
cout << "Would you like to run the program again? y or n ";
cin >> prog;
cout << endl << endl;
}
return 0;
}
string get_month()
{
string month;
cout << "What month are we making a report file for? ";
cin >> month;
cout << endl << endl;
return month;
}
int get_account_num()
{
int accountNum;
cout << "What is the account number? ";
cin >> accountNum;
cout << endl << endl;
return accountNum;
}
void Accounts_update(PersonAcct &accountStructure, ifstream &account)
{
int placeHolder1;
while(!account.eof())
{
account >> placeHolder1;
if(placeHolder1 == accountStructure.acct_num)
{
account >> accountStructure.name;
account >> accountStructure.acct_bal;
}
cout << "HI!";
}
}
void Transactions_update(PersonAcct &accountStructure, ifstream &transaction, double &withdrawls, double &deposits, int &num_of_deposits, int &num_of_withdrawls)
{
int placeHolder2;
char ch = 'k'; //to set a default value
double value = 0; //for adding/subtracting values
deposits = 0.0; //initializations
withdrawls = 0.0;
num_of_deposits = 0;
num_of_withdrawls = 0;
while(!transaction.eof())
{
transaction >> placeHolder2;
if(placeHolder2 == accountStructure.acct_num)
{
while(ch != ' ')
{
transaction.get(ch);
if(ch == 'd')
{
transaction >> value;
deposits += value;
num_of_deposits++;
value = 0;
}
if(ch == 'w')
{
transaction >> value;
withdrawls += value;
num_of_withdrawls++;
value = 0;
}
}
}
}
}
void Print_Report(PersonAcct &accountStructure, ofstream &monthlyReport, int num_of_withdrawls, int num_of_deposits, double withdrawls, double deposits)
{
double endBalance = accountStructure.acct_bal - withdrawls + deposits;
monthlyReport << "HI!";
monthlyReport.width(26);
monthlyReport << "Account Number : " << right << accountStructure.acct_num << endl;
monthlyReport.width(26);
monthlyReport << "Name : " << right << accountStructure.name << endl;
monthlyReport.width(26);
monthlyReport << "Beginning Balance : " << right << accountStructure.acct_bal << endl;
monthlyReport.width(26);
monthlyReport << "Ending Balance : " << right << endBalance << endl;
monthlyReport.width(26);
monthlyReport << "Amount Deposited : " << right << deposits << endl;
monthlyReport.width(26);
monthlyReport << "Number of Deposits : " << right << num_of_deposits << endl;
monthlyReport.width(26);
monthlyReport << "Amount Withdrawn : " << right << withdrawls << endl;
monthlyReport.width(26);
monthlyReport << "Number of Withdraws : " << right << num_of_withdrawls << endl;
}
I have used the struct as mentioned by you in the program. The required functions are added to help and do the transactions as required in the code.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.