Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I ?????

Status
Not open for further replies.

Squirrelly

Programmer
Sep 27, 2001
1
US
I'm trying to add the interest column and the principal columns here is what I have so far:

// This program produces a loan amortization

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;

int main()
{

ofstream outfile(&quot;A:Mort.txt&quot;);
char name[31];
float loan, rate, years, balance, term, payment;

cout << &quot;Enter the name of the mort. recipant. &quot;;
cin.getline(name, 31);
cout << &quot;Loan amount: $&quot;;
cin >> loan;
cout << &quot;Annual Interest Rate: &quot;;
cin >> rate;
cout << &quot;Years of loan: &quot;;
cin >> years;

term = pow((1 + rate / 12.0), 12.0 * years);
payment = (loan * rate / 12.0 * term) / (term - 1.0);

outfile.precision(2);
outfile.setf(ios::fixed | ios::showpoint | ios::left);
outfile << &quot;The loan belongs to : &quot; << name << endl;
outfile << &quot;Monthly payment: $&quot; << payment << endl;

outfile << endl;
outfile << setw(13) << &quot;Pay. No.&quot;;
outfile << setw(13) << &quot;Interest&quot;;
outfile << setw(13) << &quot;Principal&quot;;
outfile << setw(13) << &quot;Balance&quot; << endl;
outfile << &quot;----------------------------------------------\n&quot;;

balance = loan;

for (int month = 0; month < (12 * years); month++)
{
float minterest, principal;


minterest = rate / 12 * balance;
principal = payment - minterest;

outfile << setw(13) << (month + 1);
outfile << setw(13) << minterest;
outfile << setw(13) << principal;
outfile << setw(13) << balance << endl;

balance -= principal;
if (((month + 1) % 6) == 0)
{
outfile << endl;
}
if (((month + 1) % 36)==0)
{

outfile << setw(13) <<&quot;SubTotal &quot; << endl;
outfile << endl;

}
}
outfile.close();
return 0;

}

I want to add the 2 columns and display the total... CAn't figure out how to do it. Any help would be greatful...

TIA
Squirrelly
 
Hi Squirrelly ,

First of all I dont see why you need to add the interest column to the principal since the principal column will always be 0.00 no matter what you input for loan amount.
So instead I added a code next to the last line to multiply payment by total month. Also
did you know you have a digit limit. 999,999 is your limit.
One more added to that and your program wont function correctly.I hope this is what you want.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;

int main()
{

ofstream outfile(&quot;A:Mort.txt&quot;);
char name[31];
float loan, rate, years, balance, term, payment;

cout << &quot;Enter the name of the mort. recipant. &quot;;
cin.getline(name, 31);
cout << &quot;Loan amount: $&quot;;
cin >> loan;
cout << &quot;Annual Interest Rate: &quot;;
cin >> rate;
cout << &quot;Years of loan: &quot;;
cin >> years;

term = pow((1 + rate / 12.0), 12.0 * years);
payment = (loan * rate / 12.0 * term) / (term - 1.0);

outfile.precision(2);
outfile.setf(ios::fixed | ios::showpoint | ios::left);
outfile << &quot;The loan belongs to : &quot; << name << endl;
outfile << &quot;Monthly payment: $&quot; << payment << endl;

outfile << endl;
outfile << setw(13) << &quot;Pay. No.&quot;;
outfile << setw(13) << &quot;Interest&quot;;
outfile << setw(13) << &quot;Principal&quot;;
outfile << setw(13) << &quot;Balance&quot; << endl;
outfile << &quot;----------------------------------------------\n&quot;;

balance = loan;

for (int month = 0; month < (12 * years); month++)
{
float minterest, principal;


minterest = rate / 12 * balance;
principal = payment - minterest;

outfile << setw(13) << (month + 1);
outfile << setw(13) << minterest;
outfile << setw(13) << principal;
outfile << setw(13) << balance << endl;

balance -= principal;
if (((month + 1) % 6) == 0)
{
outfile << endl;
}
if (((month + 1) % 36)==0)
{

outfile << setw(13) <<&quot;SubTotal &quot; << endl;
outfile << endl;

}
} outfile<<&quot;=================================================\n&quot;;
outfile<<&quot;Total is: &quot;<< payment * month;
outfile.close();
return 0;

}


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top