How to correct the code below to reflect the following results: Enter an input f
ID: 666254 • Letter: H
Question
How to correct the code below to reflect the following results:
Enter an input filename: a1.txt
Enter an output filename: a2.txt
Enter an interest rate (%): 25
Current account status:
Name Savings Credit
Jim $23.40 $0.00
Bob $58.32 $0.00
Sally $-1.34 $0.00
New account status:
Name Savings Credit
Jim $29.25 $0.00
Bob $72.90 $0.00
Sally $0.00 $1.68
Press ENTER
-------------------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
// Function prototypes
void SetInputStreamPos(std::ifstream &str, int pos);
void ProcessAccounts(std::ifstream &ifile, std::ofstream &ofile, double rate);
void PrintFileContents(std::ifstream &str);
std::ifstream GetInputFile(std::string filename);
std::ofstream GetOutputFile(std::string filename);
void SetOutputStreamPos(std::ofstream &str, int pos);
//[BEGIN MAIN]
int main()
{
std::string inputFileName;
std::string outputFileName;
double interestRate;
std::cout << "Enter an input filename: ";
std::cin >> inputFileName;
std::cout << "Enter an output filename: ";
std::cin >> outputFileName;
std::cout << "Enter an interest rate (%): ";
std::cin >> interestRate;
std::cout << std::endl;
std::cout << std::endl;
std::ifstream ifile = GetInputFile(inputFileName);
std::ofstream ofile = GetOutputFile(outputFileName);
std::cout << "Current account status:" << std::endl;
PrintFileContents(ifile);
ProcessAccounts(ifile, ofile, interestRate);
ifile.close();
ofile.close();
std::cout << std::endl;
std::cout << "New account status:" << std::endl;
ifile = GetInputFile(outputFileName);
PrintFileContents(ifile);
ifile.close();
std::cout << "Press ENTER";
std::cin.ignore();
std::cin.get();
return 0;
}
//[END MAIN]
void ProcessAccounts(std::ifstream &ifile, std::ofstream &ofile, double rate)
{
// Everyday the bank checks the status of the accounts and processes them
// If the customer has:
// 1) a positive savings balance, pay them interest
// 2) a negative savings balance, transfer it to credit (credit is positive)
// 3) a credit balance, charge them interest
// Note: Unlike normal banks, the interest rate for savings and credit is the same
// Read the info from the input file
// Put the new info into the output file
// The format for the input file and output file should be
// [NAME] [SAVINGS BALANCE] [CREDIT BALANCE]
// There is a single space between each value and a newline at the end
// Put your code here
// Put your code here
std::string customer;
double savingsBal;
double creditBal;
//read input file - name, savings balance, and credit balance
ifile >> customer >> savingsBal >> creditBal;
//read file content (until eof)
while(!ifile.eof())
{
double savingAcct=0;
double creditAcct=0;
if(savingsBal > 0) // if there is a positive savings balance, pay them interest
{
savingAcct = savingsBal + savingsBal*rate;
}
else if(savingsBal < 0) // if there is a negative savings balance, transfer it to credit
{
creditAcct = creditBal - savingsBal;
}
if(creditBal > 0) // if there is a credit balance, charge them interest
{
creditAcct = creditAcct + creditAcct*rate;
}
//read name, savings balance, and credit balance
ifile >> customer >> savingsBal >> creditBal;
//write name, savings balance, and credit balance to outputfile
ofile << customer << savingsBal << creditBal;
}
}
void PrintFileContents(std::ifstream &str)
{
// Print the contents of the file
// First, print the file headers
// Then, print each line.
// Make sure the text is properly formatted
// Remember the functions available in iomanip?
// EXAMPLE:
// Name Savings Credit
// Bob $23.56 $0.00
// Joe $43.52 $0.00
// Sally -$1.58 $0.00
// Put your code here
std::string customer;
double savingsBal;
double creditBal;
//print column headers
std::cout << "Name Savings Credit" << std::endl;
// read account, name, savings balance, and credit balance from file
str >> customer >> savingsBal >> creditBal;
//display file content until end of file
while(!str.eof())
{
std::cout << std::left << std::setw(7) << customer << std::setw (7) << " $" << std::setprecision(2)
<< savingsBal << std::setw(7) << " $" << std::setprecision(2) << std::right << creditBal << std::endl;
} //end while
}
std::ifstream GetInputFile(std::string filename)
{
// Open the file named filename
// Make sure it exists
// Return it as an input file stream
// Put your code here
std::ifstream ifile(filename);
return ifile;
}
std::ofstream GetOutputFile(std::string filename)
{
// Open the file named filename
// Make sure it exists
// Return it as an output file stream
// Put your code here
std::ofstream ofile;
ofile.open(filename);
return ofile;
}
void SetInputStreamPos(std::ifstream &str, int pos)
{
// Set the 'g'et cursor to the desired byte in the stream
// Use the beginning of the file as the reference point
// Put your code here
str.clear();
str.seekg(pos, std::ios::beg);
}
void SetOutputStreamPos(std::ofstream &str, int pos)
{
// Set the 'p'ut cursor to the desired byte in the stream
// Use the beginning of the file as the reference point
// Put your code here
str.clear();
str.seekp(3, std::ios::beg);
}
Explanation / Answer
Header File used in the Project
#include<fstream.h>
#include<ctype.h>
#include<iomanip.h>
#include<conio.h>
#include<stdio.h>
// Function prototypes
Void SetInputStream Pos(std::ifstream &str, int pos);
void ProcessAccounts(std::ifstream &ifile, std::ofstream &ofile, double rate);
void PrintFileContents(std::ifstream &str);
std::ifstream GetInput File(std::string filename);
std::ofstream GetOutpu tFile(std::string filename);
void SetOutputStream Pos(std::ofstream &str, int pos);
//[BEGIN MAIN]
int main()
{
std::string inputFileName;
std::string outputFileName;
double interestRate;
std::cout << "Enter an input filename: ";
std::cin >> inputFileName;
std::cout << "Enter an output filename: ";
std::cin >> outputFileName;
std::cout << "Enter an interest rate (%): ";
std::cin >> interestRate;
std::cout << std::endl;
std::cout << std::endl;
std::ifstream ifile = GetInputFile(inputFileName);
std::ofstream ofile = GetOutputFile(outputFileName);
std::cout << "Current account status:" << std::endl;
PrintFileContents(ifile);
ProcessAccounts(ifile, ofile, interestRate);
ifile.close();
ofile.close();
std::cout << std::endl;
std::cout << "New account status:" << std::endl;
ifile = GetInputFile(outputFileName);
PrintFileContents(ifile);
ifile.close();
std::cout << "Press ENTER";
std::cin.ignore();
std::cin.get();
return 0;
}
//[END MAIN]
void ProcessAccounts(std::ifstream &ifile, std::ofstream &ofile, double rate)
{
std::string customer;
double savingsBal;
double creditBal;
//read input file - name, savings balance, and credit balance
ifile >> customer >> savingsBal >> creditBal;
//read file content (until eof)
while(!ifile.eof())
{
double savingAcct=0;
double creditAcct=0;
if(savingsBal > 0) // if there is a positive savings balance, pay them interest
{
savingAcct = savingsBal + savingsBal*rate;
}
else if(savingsBal < 0) // if there is a negative savings balance, transfer it to credit
{
creditAcct = creditBal - savingsBal;
}
if(creditBal > 0) // if there is a credit balance, charge them interest
{
creditAcct = creditAcct + creditAcct*rate;
}
//read name, savings balance, and credit balance
ifile >> customer >> savingsBal >> creditBal;
//write name, savings balance, and credit balance to outputfile
ofile << customer << savingsBal << creditBal;
}
}
void PrintFileContents(std::ifstream &str)
{
std::string customer;
double savingsBal;
double creditBal;
//print column headers
std::cout << "Name Savings Credit" << std::endl;
// read account, name, savings balance, and credit balance from file
str >> customer >> savingsBal >> creditBal;
//display file content until end of file
while(!str.eof())
{
std::cout << std::left << std::setw(7) << customer << std::setw (7) << " $" << std::setprecision(2)
<< savingsBal << std::setw(7) << " $" << std::setprecision(2) << std::right << creditBal << std::endl;
} //end while
}
std::ifstream GetInputFile(std::string filename)
{
std::ifstream ifile(filename);
return ifile;
}
std::ofstream GetOutputFile(std::string filename)
{
std::ofstream ofile;
ofile.open(filename);
return ofile;
}
void SetInputStreamPos(std::ifstream &str, int pos)
{
str.clear();
str.seekg(pos, std::ios::beg);
}
void SetOutputStreamPos(std::ofstream &str, int pos)
str.clear();
str.seekp(3, std::ios::beg);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.