Hello. I am new to the forums and have a homework question. I hope these are allowed here. If they are not, please let me know.
Ok, heres the thing. I have to write this loan amortization program for my C++ class. I have about 85% of it working: all the math and calculations work. However, as part of the assignment, I have to format the output into a chart. The chart has to display the account balance, the interest, monthly payment, and new account balance for each month the loan is out. It also has to have all numbers to 2 decimal places.
These two requirments are the only two things I cannot get to work. I have tired using setw(), cout.width for creating the chart but they do not seem to work. All the numbers are still bunched together.
As for the two decimal places, I have tried using setprecision() and showpoint but they do not work. Heres was happens - if the number is 8000, and I use the showpoint or setprecision(4), it still doesn't show the 8000.00. The same thing happens for numbers like 75.50. (It displays 75.5). Also, if I use setprecision(2), all the numbers are displayed in scientific notation.

Not sure why though.
I have been working on this formating problem for two days now and I cannot seem to get it to work. Any help would be greatly appreciate.
Thanks.
Robert
Code:
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
int main (void)
{
//Declare Variables
double principal, rate, mrate, mpay, factor, interest, balance;
int month, cnt;
//Program Title
cout << endl;
cout << "Loan Amortization Program\n";
cout << endl;
//Input from User
cout << "What is the value of the loan? ";
cin >> principal;
cout << "What is the loan duration (in months)? ";
cin >> month;
cout << "What is the APR (Annual Percentage Rate)? ";
cin >> rate;
cout << endl;
//Calculation of Monthly Payment
mrate = (rate/1200);
factor = pow( (mrate+1), (month));
factor = (factor-1);
mpay = (mrate+(mrate/factor))*principal;
//Display Output to User
cout << "Loan Amortization Information:\n";
cout << endl;
cout << "Intial Loan Value: $" << principal << "\n";
cout << "Loan Duration: " << month << " months\n";
cout << "Annual Percentage Rate: " << rate <<"%\n";
cout << "Monthly Payment: $" << mpay << "\n";
cout << endl;
//Up to this point, everything works and is formatted correctly.
//Loop for Displaying Monthly Information
cout << "Month\t" << "Starting Balance\t" << "Interest\t" << "Payment\t"
<< "New Balance\t" << endl;
cout << endl;
cnt = 1;
while (cnt <= (month-1))
{
interest = ((principal*(rate/100))/month);
cout << setw(3);
cout << cnt << " ";
cout << setprecision(6);
cout << principal << " ";
cout << setprecision(4);
cout << interest << " ";
cout << setprecision(6);
cout << mpay << " ";
cout << (principal = ((principal+interest)-mpay)) << endl;
cnt++;
}
//Problems to Correct:
//Need to figure out why its not going to zero but very small decimal on last value of last month.
//Decimal Points on the last one of the loop above go to 3 places,
//and some only do one (i.e. 7.5 instead of 7.50),
//while others do none (i.e. 8000 instead of 8000.00)
interest = ((principal*(rate/100))/month);
cout << setprecision(6);
cout << month << " ";
cout << principal << " ";
cout << setprecision(4);
cout << interest << " ";
cout << mpay <<" ";
cout << (principal+interest) << " ";
cout << ((principal+interest)-mpay) << endl;
cout << endl;
return 0;
}