943,778 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1036
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 14th, 2009
0

Mortgage Calculator problems

Expand Post »
Hello everyone....

I have gotten this far with my mortgage calculator....the problem is the while loop and the actual calculation isn't working correctly. The while loop really doesnt do what i was hoping for it to do...and being pretty amature at this...i am not sure where to go. i built this program in the first week of class with hard coded varibles in the code, and it worked just fine....now that the user needs to enter the code...it doesnt do so well....and help would be great. Thank you to everyone!!


c++ Syntax (Toggle Plain Text)
  1.  
  2. /*
  3.  Write the program as a procedural C++ program. Calculate
  4.  and display the mortgage payment amount using the amount
  5.  of the mortgage, the term of the mortgage, and the interest
  6.  rate of the mortgage as input by the user. Allow the user to
  7.  loop back and enter new data or quit. Insert comments in the
  8.  program to document the program.
  9.  */
  10.  
  11.  
  12. #include <iostream>
  13. #include <cmath>
  14.  
  15.  
  16. using namespace std;
  17.  
  18. int main () {
  19.  
  20. double principle = 0;
  21. double interest = 5.7;
  22. int term = 30 * 12;
  23. double total;
  24. double monthlyInterest = interest * 12;
  25. bool validNum = false;
  26.  
  27. cout << "Please enter principle amount: ";
  28.  
  29. cin >> principle;
  30.  
  31. cout << "You entered: " << principle << endl << endl;
  32.  
  33.  
  34.  
  35. while (validNum == false) {
  36.  
  37. cout << "Please enter a positive number: ";
  38.  
  39. cin >> principle;
  40.  
  41. cout << "You entered: " << principle << endl << endl;
  42.  
  43.  
  44.  
  45. if (principle <= 0) {
  46.  
  47. cout << "You did not input a correct number"
  48.  
  49. << endl;
  50.  
  51. cout << "Please re-enter the number!" << endl << endl;
  52.  
  53. }
  54. else
  55. validNum = true;
  56.  
  57.  
  58.  
  59. }
  60.  
  61. cout << "Enter desired Interest Rate: " << endl;
  62. cin >> interest;
  63.  
  64. while (validNum == false) {
  65.  
  66. cout << "Please enter a positive number: ";
  67.  
  68. cin >> interest;
  69.  
  70. cout << "You entered: " << interest << endl << endl;
  71.  
  72.  
  73.  
  74. if (interest <= 0) {
  75.  
  76. cout << "You did not input a correct number"
  77.  
  78. << endl;
  79.  
  80. cout << "Please re-enter the number!" << endl << endl;
  81.  
  82. }
  83. else
  84. validNum = true;
  85.  
  86.  
  87.  
  88. }
  89.  
  90.  
  91. //cout << "Thank you for entering a valid number!" << endl;
  92.  
  93. total = (principle * monthlyInterest)/(1-pow(1 + monthlyInterest, -term));
  94.  
  95.  
  96. cout << "Your priniple is " << principle << endl;
  97. cout << "Your Interste rate is: " << interest << endl;
  98. cout << "Term Length: " << term << " Months" << endl;
  99. cout << "Your total monthly payment is: " << total << endl;
  100.  
  101.  
  102. return 0;
  103.  
  104. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hollywoood is offline Offline
10 posts
since Aug 2009
Aug 14th, 2009
0

Re: Mortgage Calculator problems

> double monthlyInterest = interest * 12;
Not divide by 12 ?

> double interest = 5.7;
I think this is 570%
Seems high to me.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 14th, 2009
0

Re: Mortgage Calculator problems

In order to allow the user to enter new data or quit, you will want to place all of your code after your variable declarations in a while loop. That way, the user can go through the program as many times as he or she wants:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4.  
  5. using namespace std; int main ()
  6. {
  7. double principle = 0;
  8. double interest = 5.7;
  9. int term = 30 * 12;
  10. double total;
  11. double monthlyInterest = interest * 12;
  12. bool validNum = false;
  13. //create a yesNo variable to allow the user to select
  14. //if her or she wants to keep using the program
  15. string yesNo = "y"
  16.  
  17. while(yesNo == "y")
  18. {
  19. //contents of the program, then add question at the
  20. //end asking user if they would like to use it again
  21. }

Hope that helps!

-D
Reputation Points: 55
Solved Threads: 7
Junior Poster in Training
dgr231 is offline Offline
76 posts
since Aug 2009
Aug 14th, 2009
0

Re: Mortgage Calculator problems

Okay, I understand the the yes no part. But when i run the current loop i have it doesnt seem to work right. What is it i am doing wrong with it? I have tried so many different things, but i just cant seem to get it to work.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hollywoood is offline Offline
10 posts
since Aug 2009
Aug 14th, 2009
0

Re: Mortgage Calculator problems

Click to Expand / Collapse  Quote originally posted by hollywoood ...
Okay, I understand the the yes no part. But when i run the current loop i have it doesnt seem to work right. What is it i am doing wrong with it? I have tried so many different things, but i just cant seem to get it to work.
What exactly is the program doing that you don't want it to?

-D
Reputation Points: 55
Solved Threads: 7
Junior Poster in Training
dgr231 is offline Offline
76 posts
since Aug 2009
Aug 14th, 2009
0

Re: Mortgage Calculator problems

You haven't said anything about the desired output of your program, the total you were expecting, what testsdid you do , if you ae using compound interest, then the formula looks wrong to me.

Whether the interest rates are given directly or you are supposed to divide by 100 and then store those values, whether the compounding is done monthly or annually is still not clear going by
C++ Syntax (Toggle Plain Text)
  1. int term = 30 * 12; //30 years right?
  2. double monthlyInterest = interest * 12; //what exactly for and
  3. //it should be annualInterest

Did you read your own thread
http://www.daniweb.com/forums/thread210871.html
Last edited by zalezog; Aug 14th, 2009 at 12:43 pm.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Aug 14th, 2009
0

Re: Mortgage Calculator problems

Hey everyone. Thank you for the help so far i really am very grateful for all of it. I think i have the main part of the program working. The part I am having a problem with is the calculation for the total loan. I need to calculate the total per month of the loan. When i do the calculation at the end it gives me a number like 1.368e+07 and i am not sure how to correct this. Can someone help me on this thanks.

c++ Syntax (Toggle Plain Text)
  1.  
  2.  
  3. #include <math.h>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7.  
  8. int main () {
  9. double principle = 0;
  10. double interest = 5.7;
  11. int term = 30 * 12;
  12. double total;
  13. //double interest = i * 100;
  14. double monthlyInterest = interest * 12;
  15.  
  16. int choice;
  17.  
  18. do {
  19. cout << "Please enter total loan amount" << endl;
  20. cin >> principle;
  21. cout << "Your entered: " << principle << endl;
  22. cout << "Please enter interest rate " << endl;
  23. cin >> interest;
  24. cout << "Your entered: " << interest << endl;
  25. cout << "Please enter the term in years: " << endl;
  26. cin >> term;
  27. cout << "Your entered: " << term << endl;
  28.  
  29.  
  30.  
  31.  
  32. total = (principle * monthlyInterest)/(1-pow(1 + monthlyInterest, -term)); //total for mortgage
  33.  
  34.  
  35.  
  36. cout << "Your total monthly payment is: " << total << endl;
  37.  
  38. cout << "Would you liek to play again?" << endl;
  39. cout << "1 for yes" << endl; //gives user choice
  40. cout << "2 for no" << endl; //gives user choice
  41. cin >> choice;
  42.  
  43.  
  44.  
  45.  
  46. }while (choice==1);
  47.  
  48. return 0;
  49. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hollywoood is offline Offline
10 posts
since Aug 2009
Aug 14th, 2009
0

Re: Mortgage Calculator problems

If you want fixed point then user
C++ Syntax (Toggle Plain Text)
  1. cout.setf(std::ios_base::fixed,std::ios_base::floatfield);

This will output number such as 1.2000000000 instead of
1.2e10
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 14th, 2009
0

Re: Mortgage Calculator problems

Okay i can put that in there, but the answer is still not correct. In the equation what am i doing wrong?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hollywoood is offline Offline
10 posts
since Aug 2009
Aug 14th, 2009
0

Re: Mortgage Calculator problems

Give me an input values and what the output value supposed to be.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Converting uppercase words to lowercase
Next Thread in C++ Forum Timeline: Starting out with C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC