Mortgage calculator. Please help due soon
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Mortgage calculator. Please help due soon

  1. #1
    Join Date
    Mar 2013
    Posts
    4

    Mortgage calculator. Please help due soon

    I need my code to display Month but it seems my code is skipping month's and not displaying 1, 2 3. It's going 1 , 3 ,5 and so on. This is the code i have for it
    Code:
    int month = 1;
    and later on the code i have month++ That's my first mistake and i'm not sure how to possibly fix it. My second mistake is my Interest Paid output. This is the code i have for it
    Code:
    interestPaid = loanBalance*i;
    This is on the while loop, but i'm not sure why it's not displaying the correct amount. It should display for month 1 = 500.00 , Month 2 = 498.28 and Month 3 = 496.55 and so on... However my code display's something close to it, but not that. i is my monthly interest rate. My code for it seems to be fine. If i can fix this two things i'll be able to finish the project. This is my entire code, someone please help me finish it and tell me how i can fix it so that my month is displayed in 1, 2, 3, etc.... and not 1, 3, 5, etc... And why my interest is not displaying what my professor has on his output screen (i provided the first three months on top in bold) If i can fix those things i'll have the rest. Here's my entire code

    #include <iostream>
    #include <iomanip>


    using namespace std;
    int main ( )

    Code:
    {
    
    	int month = 1;
    	// Holds the user's amount of loan.
    
    	double loanAmount;
    
    	// Holds the user's interest rate.
    	
    	double i;
    
    	// Holds the user's yearly Interest Rate.
    
    	double interestRate;
    
    	// Holds the user's interest paid every month.
    
    	double interestPaid;
    	
    	// "n" means the months it will take the user to pay off the loan.
    
    	double n;
    
    	// Holds the variable for total months.
    
    	double years;
    
    	// Holds payment data.
    
    	double monthlyPayment;
    
    	// Holds the user Principle paid.
    
    	double principlePaid;
    
    	// Holds the user Loan balance.
    
    	double loanBalance;
    
    	// Asks the user to input how much money they took out.
    
    	cout<<"Enter loan amount ->";
    	cin>>loanAmount;
    
    	// Asks the user to input their interest rate.
    
    	cout<<"Enter interest rate -->";
    	cin>>interestRate;
    	i = (interestRate/1200);
    
    	// Asks the user to input the number of years they are taking the loan for.
    
    	cout<<"Enter number of years of the laon ->";
    	cin>>years;
    	n = years*12;
    	
    	// Equation that determines the user's monthly payment.
    
    	monthlyPayment = loanAmount*(i/(1-pow(1+i,-n)));
    
    	// Equation for interest paid every month.
    
    	interestPaid = loanAmount*i;
    
    	// Equation for Principle paid.
    
    	principlePaid = monthlyPayment-interestPaid;
    
    	// Equation for Loan Balance
    
    	loanBalance = loanAmount-principlePaid;
    
    
    
    
    
    
    
    	cout<<"Month"<<"\t"<<"Payment"<<"\t"<<"Interest Paid"<<"\t"<<"Principle Paid"<<"\t"<<"Loan Balance"<<endl;
    	// Beginning of while coding.
    	
    	do
    	{
    	
    
    	cout<<month++<<"\t"<<fixed<<setprecision(2)<<monthlyPayment            // Code used to display monthly Payment.
    		<<"\t"<<fixed<<setprecision(2)<<interestPaid                       // Code used to display Interest Paid.
    	    <<"\t"<<fixed<<setprecision(2)<<principlePaid                      // Code used to display Principle Paid.
    		<<"\t"<<fixed<<setprecision(2)<<loanBalance;                       // Code used to display Loan Balance.
    
    
    	loanBalance = loanBalance-principlePaid;
    
    	interestPaid = loanBalance*i;
    
    	principlePaid = monthlyPayment-interestPaid;
    	}
    
    	while (loanBalance>0);
    
    	cin.get ( ); cin.get ( );
    	return 0;
    }

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,750

    Re: Mortgage calculator. Please help due soon

    1) As you are using pow, you should include <cmath>

    2)
    Code:
    cout<<month++<<"\t"<<fixed<<setprecision(2)<<monthlyPayment            // Code used to display monthly Payment.
    		<<"\t"<<fixed<<setprecision(2)<<interestPaid                       // Code used to display Interest Paid.
    	    <<"\t"<<fixed<<setprecision(2)<<principlePaid                      // Code used to display Principle Paid.
    		<<"\t"<<fixed<<setprecision(2)<<loanBalance;
    The output for principlePaid and loadBalance doesn't line up with the headings. You'll need some extra tabs. Also as the code is, you won't get each months details on a new line. One months details will just start to be displayed straight after the previous months. cout doesn't automatically start a new line when you start a new statement. Unless you specify a new line, cout just starts to output on the screen where the previous cout left off.

    Once I changed these on my system, the output seems OK.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Mar 2013
    Posts
    4

    Re: Mortgage calculator. Please help due soon

    Ahh okay. Thanks i used endl; and yea it set it right like i needed it. yes i still need to fix the out put. I did what you did and the months are now aligned that was really helpful. Thank you so much.

    How about my interest paid output?? I used... For the second month i should be getting 498.28 but what my program gives is 496.56. I know my equation is right, because on my first month i get the same answer but on the second month i'm off by a few dollars. The formula to get monthly interest paid we were given is (loan amount x monthly interest). That's why i told my program

    Code:
    loanBalance = loanBalance-principlePaid; 
    
    interestPaid = loanBalance*i;
    Do you guys have any idea if this is right? because it's not giving me what my professor wants

  4. #4
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Mortgage calculator. Please help due soon

    Quote Originally Posted by hadesfalcon View Post
    How about my interest paid output?? I used... For the second month i should be getting 498.28
    So did you debug your program? If the program gives the wrong answer, it's your job to debug the program to see why the answer is not correct.
    I know my equation is right, because on my first month i get the same answer but on the second month i'm off by a few dollars. The formula to get monthly interest paid we were given is (loan amount x monthly interest). That's why i told my program...
    Again, debug your program. Use the debugger that comes with your compiler to see what the value of those variables are, or at the very least, write output statements to see what values variables are during the processing of the program.
    Do you guys have any idea if this is right? because it's not giving me what my professor wants
    And for us to tell you what is wrong with your program, we have to debug it. So we have to copy your code, compile it, fire up the debugger, step through your code, see the bad results, and then get back to you with our observations and what to fix. Sorry to say this, but these are things that you should really be doing. If you wrote the code, you must know how to debug the code you wrote.

    We are experienced programmers that know how to debug programs quickly. However, us debugging your program for you is cheating -- it is no different than someone giving you the code. Debugging is part and parcel of the learning process of how to write a program. It isn't a voluntary skill, it is a mandatory skill. No one writes perfect programs the first time -- when the program doesn't work or does something contrary to what is believed it should be doing, then it's time to debug the program.

    Once you step through the program and you cannot figure out what's wrong, then tell us what the values of variables are, what you expected them to be, and what they really are. But to be honest, by that time, you've more than likely figured out the problem yourself and what to fix.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 30th, 2013 at 05:49 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center