file i/o

I'm working on a problem that is introducing reading and writing data to and from files. I'm new to doing this in c++ so any help would be greatly appreciated. This is my first post on this forum, so feel free to give me suggestions not only to solve my problem, but also to become a better forum member.

Here is the problem code (it is a mortgage payment calculator):

#include <iostream>
#include <fstream> // Access ifstream, ofstream
#include <cmath> // Access pow()
#include <iomanip> // Access setprecision()

using namespace std;

int main()
{
// Input variables
float loanAmount; // Amount of the loan
float yearlyInterest; // Yearly interest rate
int numberOfYears; // Number of years
ifstream inData; // Input stream variable
ofstream outData; // Output stream variable

// Local variables (Used in calculations)
float monthlyInterest; // Monthly interest rate
float payment; // Monthly payment
int numberOfPayments; // Total number of payments

// Open files
inData.open("loan.in");
outData.open("loan.out");

// Read values
inData >> loanAmount >> yearlyInterest >> numberOfYears;

// Calculate values
monthlyInterest = yearlyInterest / 12;
numberOfPayments = numberOfYears * 12;
payment = (loanAmount * pow(1 + monthlyInterest, numberOfPayments)
* monthlyInterest) /
(pow(1 + monthlyInterest, numberOfPayments) - 1);

// Output results
outData << fixed << setprecision(2) << "Loan Amount: "
<< loanAmount << endl << setprecision(4) << "Interest Rate: "
<< yearlyInterest << endl << "Number of Years: "
<< numberOfYears << endl;
outData << fixed << setprecision(2) << "Monthly Payment: "
<< payment << endl;

return 0;
}

The problem instructions say a file named loan.in should first be created and saved. Its contents are as follows:

50000.00 0.0524 7


The resulting output file named loan.out should produce the following output:

Loan Amount: 50000.00
Interest Rate: 0.0524
Number of Years: 7
Monthly Payment: 712.35


The problem I'm having is that my textbook gives no detail as to how to create the loan.in file or where to save it. I use Bloodshed's C++ IDE and tried to add a file to the project in the IDE. I also tried using Notepad to create the file, but it adds a .txt to the filename. The compiler automatically creates a loan OUTfile, but when I compile and run the program and then open the load.out file I get the following output:


Loan Amount: 0.00

Writing to a file

Steps:
1. Declare an ofstream var.
2. Open a file with it.
3. Write to the file (there are a couple of ways.)
4. Close it.

#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile ("example.txt");
if (myfile.is_open()) {
myfile << "This is a line.\n";
myfile << "This is another.\n";
myfile.close();
} else cout << "Unable to open file";
return 0;
}


Reading from a file (Almost the same)

Steps:
1. Declare an ifstream var.
2. Open a file with it.
3. Read from the file (there are a couple of ways.)
4. Close it.

#include <iostream>
#include <fstream>
using namespace std;
string line;
ifstream myfile ("example.txt");
if (myfile.is_open()) {
while (! myfile.eof()) {
getline (myfile,line);
cout << line << endl;
}
myfile.close();
} else cout << "Unable to open file";
return 0;
}

http://codewall.blogspot.com
http://www.daniweb.com/forums/thread6542.html#
http://www.cplusplus.com/doc/tutorial/files/
Last edited on
Thank you for your help with this problem.
Topic archived. No new replies allowed.