I am having some errors with my coding. Below is my code, if someone could pleas
ID: 3559981 • Letter: I
Question
I am having some errors with my coding. Below is my code, if someone could please correct it I would really appreciate it!
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <iomanip>
using namespace std;
// Function prototypes
void GetInputFile(std::string, std::ifstream& ifile);
void GetOutputFile(std::string, std::ofstream& ofile);
void PrintFileContents(std::ifstream& ifile);
void ProcessAccounts(std::ifstream& ifile, std::ofstream& ofile, double);
void SetInputStreamPos(std::ifstream &str, int pos);
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::ifstream ifile = GetInputFile(inputFileName);
std::ofstream ofile = GetOutputFile(outputFileName);
std::cout << "Current account status:" << std::endl;
PrintFileContents(ifile);
SetInputStreamPos(ifile, 0);
ProcessAccounts(ifile, ofile, interestRate / 100);
ifile.close();
ofile.close();
std::cout << std::endl;
std::cout << "New account status:" << std::endl;
GetInputFile(outputFileName, ifile);
SetInputStreamPos(ifile, 0);
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
std::string name;
double sav_balance;
double credit_balance;
ifile >> name >> sav_balance >> credit_balance;
while (!ifile.eof())
{
//std::cout << name << " " << sav_balance << " " << credit_balance << std::endl;
double sav_final = 0;
double credit_final = 0;
if (sav_balance > 0) // 1) a positive savings balance, pay them interest
sav_final = sav_balance + sav_balance*rate;
else if (sav_balance < 0) // 2) a negative savings balance, transfer it to credit (credit is positive)
credit_final = credit_balance - sav_balance;
if (credit_balance>0) // 3) a credit balance, charge them interest
credit_final = credit_final + credit_balance*rate;
ofile << name << " " << sav_final << " " << credit_final << std::endl;
//std::cout << name << " " << sav_final << " " << credit_final << std::endl;
ifile >> name >> sav_balance >> credit_balance;
}
}
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 name;
double sav_balance;
double credit_balance;
std::cout << left << setw(10) << "Name" << setw(11) << "Savings" << setw(7) << "Credit"<< std::endl;
str >> name >> sav_balance >> credit_balance;
while (!str.eof())
{
if (sav_balance < 0)
{
std::cout << left << setw(10) << name << setw(1) << "-$" << setw(9) << fixed << std::setprecision(2) << sav_balance*-1 << setw(1) << "$" << setw(7) << fixed << std::setprecision(2) << credit_balance << std::endl;
str >> name >> sav_balance >> credit_balance;
}
else
{
std::cout << left << setw(11) << name << setw(1) << "$" << setw(9) << fixed << setprecision(2) << sav_balance << setw(1) << "$" << setw(7) << fixed << setprecision(2) << credit_balance << std::endl;
str >> name >> sav_balance >> credit_balance;
}
}
}
void GetInputFile(std::string filename, std::ifstream& str)
{
// Open the file named filename
// Make sure it exists
// Return it as an input file stream
// Put your code here
str.open(filename.c_str());
if (!str.is_open())
{
std::cout << "File could not be opened" << std::endl; // File 1 could not be opened
return;
}
}
void GetOutputFile(std::string filename, std::ofstream& str)
{
// Open the file named filename
// Make sure it exists
// Return it as an output file stream
// Put your code here
str.open(filename.c_str());
if (!str.is_open())
{
std::cout << "File could not be opened" << std::endl; // File 2 could not be opened
return;
}
}
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(pos, std::ios::beg);
}
Explanation / Answer
/*At calling time function argument is missing*/
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <iomanip>
using namespace std;
// Function prototypes
void GetInputFile(std::string, std::ifstream& ifile);
void GetOutputFile(std::string, std::ofstream& ofile);
void PrintFileContents(std::ifstream& ifile);
void ProcessAccounts(std::ifstream& ifile, std::ofstream& ofile, double);
void SetInputStreamPos(std::ifstream &str, int pos);
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::ifstream ifile;
GetInputFile(inputFileName, ifile);
std::ofstream ofile;
GetOutputFile(outputFileName,ofile);
std::cout << "Current account status:" << std::endl;
PrintFileContents(ifile);
SetInputStreamPos(ifile, 0);
ProcessAccounts(ifile, ofile, interestRate / 100);
ifile.close();
ofile.close();
std::cout << std::endl;
std::cout << "New account status:" << std::endl;
GetInputFile(outputFileName, ifile);
SetInputStreamPos(ifile, 0);
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
std::string name;
double sav_balance;
double credit_balance;
ifile >> name >> sav_balance >> credit_balance;
while (!ifile.eof())
{
//std::cout << name << " " << sav_balance << " " << credit_balance << std::endl;
double sav_final = 0;
double credit_final = 0;
if (sav_balance > 0) // 1) a positive savings balance, pay them interest
sav_final = sav_balance + sav_balance*rate;
else if (sav_balance < 0) // 2) a negative savings balance, transfer it to credit (credit is positive)
credit_final = credit_balance - sav_balance;
if (credit_balance>0) // 3) a credit balance, charge them interest
credit_final = credit_final + credit_balance*rate;
ofile << name << " " << sav_final << " " << credit_final << std::endl;
//std::cout << name << " " << sav_final << " " << credit_final << std::endl;
ifile >> name >> sav_balance >> credit_balance;
}
}
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 name;
double sav_balance;
double credit_balance;
std::cout << left << setw(10) << "Name" << setw(11) << "Savings" << setw(7) << "Credit"<< std::endl;
str >> name >> sav_balance >> credit_balance;
while (!str.eof())
{
if (sav_balance < 0)
{
std::cout << left << setw(10) << name << setw(1) << "-$" << setw(9) << fixed << std::setprecision(2) << sav_balance*-1 << setw(1) << "$" << setw(7) << fixed << std::setprecision(2) << credit_balance << std::endl;
str >> name >> sav_balance >> credit_balance;
}
else
{
std::cout << left << setw(11) << name << setw(1) << "$" << setw(9) << fixed << setprecision(2) << sav_balance << setw(1) << "$" << setw(7) << fixed << setprecision(2) << credit_balance << std::endl;
str >> name >> sav_balance >> credit_balance;
}
}
}
void GetInputFile(std::string filename, std::ifstream& str)
{
// Open the file named filename
// Make sure it exists
// Return it as an input file stream
// Put your code here
str.open(filename.c_str());
if (!str.is_open())
{
std::cout << "File could not be opened" << std::endl; // File 1 could not be opened
return;
}
}
void GetOutputFile(std::string filename, std::ofstream& str)
{
// Open the file named filename
// Make sure it exists
// Return it as an output file stream
// Put your code here
str.open(filename.c_str());
if (!str.is_open())
{
std::cout << "File could not be opened" << std::endl; // File 2 could not be opened
return;
}
}
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(pos, std::ios::beg);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.