I am attempting to write a program for an assignment. I have to use several functions to open and then write some data to an external file. I have to use one function just to open the external file and then another function to write a header with columns to the file. It is a requirement that we allow the user to name the file. Here is my difficulty; I was able to allow the user to name the file and then open it, but it doesn't write the data to the file. Here is my code.
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;
int openOutputFile(char n[]);
void writeHeaderLinesToOutputFile();
int createAndSaveLoanData();
double calculateOneLineOfLoanResults();
double calculateMonthlyPayment(double r, double expm, double yrs);
double calculateTotalPayment(double mPmt);
int writeLineOfResultsToOutputFile();
int main()
{
ofstream outFile;
char outputName[50];
cout << "Please type the name of the output file, including the extension: ";
cin >> outputName;
openOutputFile(outputName);
cin.get();
return 0;
}
int openOutputFile(char name[50])
{
ofstream outFile;
outFile.open(name);
if (outFile.fail())
{
cerr << "Could not open " << name << "!" << endl;
return EXIT_FAILURE;
}
writeHeaderLinesToOutputFile();
return 0;
}
void writeHeaderLinesToOutputFile()
{
ofstream outFile;
outFile << "Loan Amount: $1000" << '\n';
outFile << left << setw(9) << "Interest" << left << setw(9) << "Duration" << left << setw(9) << "Monthly";
outFile << left << setw(9) << "Total" << '\n';
outFile << left << setw(9) << "rate" << left << setw(9) << "(years)" << left << setw(9) << "payment";
outFile << left << setw(9) << "payment" << '\n';
outFile.close();
}
Can someone tell me what I am going wrong? I am pretty sure the problem is occurring because the file is being opened in one function and written to in another, but I have no idea how to get around it.