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

New Topic/Question
Reply




MultiQuote





|