C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 1st, 2005, 02:30 PM
robert2513 robert2513 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 2 robert2513 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 3 sec
Reputation Power: 0
HWK Help: Loan Amortization

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;
   
   }
   
   

Reply With Quote
  #2  
Old March 1st, 2005, 05:56 PM
robert2513 robert2513 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 2 robert2513 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 3 sec
Reputation Power: 0
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > HWK Help: Loan Amortization


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT