Problem statement: You are tasked with writing a program which will write a mont
ID: 3773492 • Letter: P
Question
Problem statement:
You are tasked with writing a program which will write a monthly account summary of customers' transactions at the bank at which you are employed. The monthly transactions are stored in a file called transactions.txt in the follow form:
123 d45.10 d50.45 d198.56 w45.67
The first entry is the account number; the remainder of the line, variable in length, contains all the transactions of the particular customer for the month:
If the entry begins with a d, the number that follows is the amount of deposi
If it begins with a w, the number that follows is the amount of withdraw.
The customer "database" is stored in another file, accounts.txt which has entries of the form
123 daffy 34.67
where each line contains the customer's account number then their name, followed by the current account balance at the beginning of the month.
Your program will write report in the following form ( given the above data )
Account Number : 123
Name : daffy
Beginning Balance : $34.67
Ending Balance : $283.11
Amount Deposited : $294.11
Number of Deposits : 3
Amount Withdrawn : $45.67
Number of Withdraws : 1
The report is to be written to a file called account_report_MONTH.txt, where MONTH is a user entered month name
CODE:
Your program will read from the following files:
a file containing the listing of all transactions for the month;
a file containing a list of all customers, along with their account ids.
You must use the following structure to hold the data of each customer:
struct PersonAcct // struct account holds customer info
{
int acct_num; // customer account number
string name; // customers name
int acct_bal; // customers account balance
};
Note: Feel free to construct your own files to test/debug you program
Please provide a screenshot of where the file is located on the directory.
Here are the two text files:
accounts.txt
123 daffy 34.67
345 goofy 123.89
639 sneezy 1945.76
890 dopey 12345667.90
666 grumpy 666.66
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
--------------------------------------------------------------------------------------
Also note to anyhelp out there if you do not have screen shot of your code output.
USE VISUAL STUDIO C++
THANKS
Explanation / Answer
Complete Program:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
struct PersonAcct
{
int acct_num;
string name;
double acct_bal;
};
int main()
{
const int MAX_ACCOUNTS = 100;
const int TRANSACTIONS_PER_MONTH = 5;
PersonAcct persons[MAX_ACCOUNTS];
int count = 0;
string month_name;
string line;
int account_number;
cout << "Enter the month's name of transactions: ";
getline(cin, month_name);
string accounts_file = "accounts.txt";
string transactions_file = "transactions.txt";
string account_report_file = "account_report_" + month_name + ".txt";
ifstream infile_accounts;
ifstream infile_transactions;
ofstream outfile_account_report;
infile_accounts.open(accounts_file);
infile_transactions.open(transactions_file);
outfile_account_report.open(account_report_file);
if(!infile_accounts)
{
cout << accounts_file << " file cannot be opened" << endl;
system("pause"); // for visual studio
exit(EXIT_FAILURE);
}
if(!infile_transactions)
{
cout << transactions_file << " file cannot be opened" << endl;
system("pause"); // for visual studio
exit(EXIT_FAILURE);
}
infile_accounts >> account_number;
while(infile_accounts && count < MAX_ACCOUNTS)
{
persons[count].acct_num = account_number;
infile_accounts >> persons[count].name;
infile_accounts >> persons[count].acct_bal;
count++;
infile_accounts >> account_number;
}
outfile_account_report << setprecision(2) << fixed;
getline(infile_transactions, line);
while(infile_transactions)
{
int index = line.find(" ");
account_number = stoi(line.substr(0, index));
bool found = false;
int i = 0;
while(!found && i < count)
{
if(persons[i].acct_num == account_number)
found = true;
else
i++;
}
if(!found)
cout << "No account holder in the bank with the account number " << account_number << endl;
else
{
double deposit_amount = 0;
double withdraw_amount = 0;
int deposits = 0;
int withdraws = 0;
string trans;
line = line.substr(index + 1);
index = line.find(" ");
while(index > 0)
{
trans = line.substr(0, index);
if(found && trans.at(0) == 'd')
{
deposit_amount = deposit_amount + stof(trans.substr(1));
deposits++;
}
else if(found && trans.at(0) == 'w')
{
withdraw_amount = withdraw_amount + stof(trans.substr(1));
withdraws++;
}
else
cout << "'" << trans.at(0) << "' is an invalid trasnaction symbol" << endl;
line = line.substr(index + 1);
index = line.find(" ");
}
if(line.length() > 0)
{
trans = line;
if(found && trans.at(0) == 'd')
{
deposit_amount += stof(trans.substr(1));
deposits++;
}
else if(found && trans.at(0) == 'w')
{
withdraw_amount += stof(trans.substr(1));
withdraws++;
}
else
cout << "'" << trans.at(0) << "' is an invalid trasnaction symbol" << endl;
}
outfile_account_report << left << setw(25) << "Account Number: " << persons[i].acct_num << endl;
outfile_account_report << left << setw(25) << "Name: " << right << persons[i].name << endl;
outfile_account_report << left << setw(25) << "Beginning Balance: " << "$" << persons[i].acct_bal << endl;
persons[i].acct_bal = persons[i].acct_bal + deposit_amount - withdraw_amount;
outfile_account_report << left << setw(25) << "Ending Balance: " << "$" << persons[i].acct_bal << endl;
outfile_account_report << left << setw(25) << "Amount Deposited: " << "$" << deposit_amount << endl;
outfile_account_report << left << setw(25) << "Number of Deposits: " << deposits << endl;
outfile_account_report << left << setw(25) << "Amount Withdrawn: " << "$" << withdraw_amount << endl;
outfile_account_report << left << setw(25) << "Number of Withdraws: " << withdraws << endl << endl;
}
getline(infile_transactions, line);
}
infile_accounts.close();
infile_transactions.close();
outfile_account_report.close();
cout << endl;
system("pause"); // for visual studio
return 0;
}
Input file: accounts.txt
123 daffy 34.67
345 goofy 123.89
639 sneezy 1945.76
890 dopey 12345667.90
666 grumpy 666.66
Input file: 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
Output on console:
Enter the month's name of transactions: December
Output file: account_report_December.txt
Account Number: 123
Name: daffy
Beginning Balance: $34.67
Ending Balance: $283.11
Amount Deposited: $294.11
Number of Deposits: 3
Amount Withdrawn: $45.67
Number of Withdraws: 1
Account Number: 345
Name: goofy
Beginning Balance: $123.89
Ending Balance: $49.43
Amount Deposited: $27.79
Number of Deposits: 2
Amount Withdrawn: $102.25
Number of Withdraws: 3
Account Number: 639
Name: sneezy
Beginning Balance: $1945.76
Ending Balance: $3778.33
Amount Deposited: $2234.90
Number of Deposits: 2
Amount Withdrawn: $402.33
Number of Withdraws: 3
Account Number: 890
Name: dopey
Beginning Balance: $12345667.90
Ending Balance: $12346194.45
Amount Deposited: $561.32
Number of Deposits: 4
Amount Withdrawn: $34.77
Number of Withdraws: 1
Account Number: 666
Name: grumpy
Beginning Balance: $666.66
Ending Balance: $8139.84
Amount Deposited: $7473.18
Number of Deposits: 5
Amount Withdrawn: $0.00
Number of Withdraws: 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.