Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 28th, 2005, 4:45 PM   #1
robert2513
Newbie
 
Join Date: Feb 2005
Posts: 9
Rep Power: 0 robert2513 is on a distinguished road
Question Loan Amortization Problem

Hello.

I hope this is allowed here: homework help . If not, please let me know.

I got to write this loan amortization program for my programming class. I got the basic math down but now, heres the thing, I need to format the output so that it forms a table and displays two decminal points. An example of what the output should look like is below:

Month......Starting Balance......Interest......Payment......New Balance
...1..............8000.00...............80.00........710.79...........7369.21
etc. etc.
(I put those periods in so that when I posted the question, everything would stay lined up)

However, I do not understand how to do this. I know you have to use the setw() and setprecision() commands but I cannot seem to get them to work.

When I put in:

cout << setw(5) << "Month " << "Starting Balance " <<
"Interest " << "Payment " << "New Balance " << endl;

the output is:

Month Starting Balance Interest Payment New Balance

When I put in:

cout << setprecision(2);
cout << month << "\n" << principal << "\n" << interest << "\n" << mpay <<
"\n" << (principal+interest) << "\n" << ((principal+interest)-mpay) <<
endl;

I get the output:

12
7e+02
7
7.1e+02
7.1e+02

Can someone please point me in the right direction? Thanks for any help. It is greatly appreciated. Sorry for the long post.

Thanks,
Robert

Here is my code, if anybody needs it:

#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";


//Up to this point, everything works and is formatted correctly.

//Loop for Displaying Payment Information

cout << setw(5) << "Month" << setw(5) << "Starting Balance" <<
"Interest" << "Payment" << "New Balance" << endl;

cout << endl;

cnt = 1;
while (cnt <= (month-1))
{
setprecision(2);
interest = ((principal*(rate/100))/month);
cout << cnt << "\n" << principal << "\n" << interest << "\n" <<
mpay << "\n" << (principal = ((principal+interest)-mpay)) << endl;

cnt++;

}

interest = ((principal*(rate/100))/month);
cout << setprecision(2);
cout << month << "\n" << principal << "\n" << interest << "\n" << mpay <<
"\n" << (principal+interest) << "\n" << ((principal+interest)-mpay) <<
endl;


cout << endl;
return 0;

}
robert2513 is offline   Reply With Quote
Old Feb 28th, 2005, 7:03 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Homework problems are allowed, but you don't usually get much help unless you show some signs of attempting it yourself. Obviously, you have tried it yourself, so I'm willing to bet you'll get some help with this. I'm not great with C++, but if you can use the C function printf() I can help you very easily with the decimal format problem.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Feb 28th, 2005, 8:42 PM   #3
robert2513
Newbie
 
Join Date: Feb 2005
Posts: 9
Rep Power: 0 robert2513 is on a distinguished road
Thanks for the reply.

I don't mean to sound like I am trying to get free answers but what does printf() do and how do you use it? I looked in both my text books but could not find anything.
robert2513 is offline   Reply With Quote
Old Feb 28th, 2005, 9:22 PM   #4
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
printf() is a function in the stdio.h library. It basically prints a string. You can add variables that you want to print. It looks something like this:

 printf("This is an example of printf"); // will print the quoted string to the screen.
 int x = 6;
 printf("this is an example using an integer: %d", x); // this will print the value 6 in place of '%d'
 float f = 1.0238534;
 printf("this is an example using a float: %f", f); // this will print the value of f in place of %f
 printf("another example with float: %.2f", f); //this should print 1.02 in place of %f

It looks something like that. I can give you a more detailed description if you want, but probably not tonite. I have homework piled on top of me.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Mar 1st, 2005, 9:24 AM   #5
robert2513
Newbie
 
Join Date: Feb 2005
Posts: 9
Rep Power: 0 robert2513 is on a distinguished road
Got to be leaving for class soon, but just wanted to update this before I left.

I changed the following in my code so that the decimals work, for the most part.

cnt = 1;
while (cnt <= (month-1))
{

interest = ((principal*(rate/100))/month);
cout << setw(5);
cout << cnt << " ";
cout << setprecision(6);
cout << principal << " ";
cout << setprecision(4);
cout << interest << " ";
cout << setprecision(6);
cout << mpay << " ";
cout << (principal = ((principal+interest)-mpay)) << endl;

cnt++;

}

I could not get the printf() command to work so I played around with the setprecision() command. It works for the most part except for a few things: values that are even, such as 8000, only display 8000, not 8000.00. and for some reason, for the last value in the loop, it calculates for new balance and displays it with 4 decimal points.

Well,got to leave now. Thanks.
robert2513 is offline   Reply With Quote
Old Mar 1st, 2005, 4:02 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Good luck.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 1st, 2005, 4:55 PM   #7
robert2513
Newbie
 
Join Date: Feb 2005
Posts: 9
Rep Power: 0 robert2513 is on a distinguished road
Well, after MUCH trial and error, I think I figured out what to do.

In order for the table to be created, I need, before the setpercision(), the statement:

cout.setf(ios::fixed)

Everything seems to be coming along a lot smoother now. I got some final changes to make. If I run into anymore problems, I'll be sure to post them. LOL

Thanks for replies. They are much appreciated.
robert2513 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:53 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC