My current homework assignment is to code a mortgage calculator that will allow the user to input the appropriate information and return the monthly payment, the interest paid and the principal paid. An amortization schedule for the entire lenghth of the loan is also required.
I have managed to get my loop counter to step through each month for the entire life of the loan. My problem is that instead of the program also stepping through each payment, it repeats the same payment information and balance for the complete loan. The balance is never decreased by the amount of each montly payment. I have traced the problem down to something in my loop counter. My problem is that I cannot find what in my loop counter is wrong. all my math calcuations are correct and the montly payment is accurate. So I believe the problem has to be in my loop. I just need some help finding it. I have enclosed my code. Please bear in mind that even though i have placed my code in the proper code tags some of the formating may still be off.

// simple mortgage calculator
// programmer - Ronald Scurry
// class - PRG410
// facilitator - Jane Wu

#include <iostream>
#include <cmath>           // must include for pow function

using std::cout;
using std::cin;
using std::endl;

int main ()
{
    // defines varibles
    double loanAmt = 0.0;
    double interest = 0.0;
    double term = 0.0;
    char quit;

    do
    {
            // user input
            cout << "Enter interest rate:" << endl;
            cin >> interest;
            cout << endl;

            cout << "Enter term in years:" << endl;
            cin >> term;
            cout << endl;

            cout << "Enter Loan amount:"<< endl;
            cin >> loanAmt;
               cout << endl;
            cout<<"\n";

   double monthlyPmt = loanAmt*(interest/1200)/(1-pow(1+(interest/1200),-1*term*12)); // amoritization formula
   double loanAmnt = loanAmt;   
   cout << "For a "<<loanAmnt<<" loan your payment is " <<monthlyPmt<<endl<<endl;

   double monthlyInt =  loanAmt * (interest/1200);
   double principalPaid = monthlyPmt - monthlyInt;
   double balance = loanAmt-monthlyPmt;
   for (double i=0; i<term*12;i++)
 {
  double monthlyInt=balance*(interest/1200);
  double principalPaid=monthlyPmt-monthlyInt;
    if (balance<1.00)balance=0.0;
    cout<<" New loan balance for month "<<i+1<<" is "<<balance<<endl<<endl;
    cout<<" Your interest paid for this month is " <<monthlyInt<<endl<<endl;
    cout<<" Your Principal paid for this month is " <<principalPaid<<endl;
    cout<< "\n";
    getchar();                   // pauses the program and allows you to view the output

 }




         // user can continue in a loop or quit
            //output
            cout<<"\n";
            cout<<"If you wish to continue press  C then the enter key on your keyboard.\n";
            cout<<"If you wish to quit press Q then the enter key on your keyboard.\n";

            //user input
            cin>>quit;
            cout<<"\n";
        }

        while((quit!='q') && (quit!='Q'));

        cout<<"Thank you for trying my simple mortgage calculator\n";

}

>> The balance is never decreased by the amount of each montly payment.
That's because you have not written any code/statement that decreases the variable "double balance". :)

My guess is you need to change as I've done (Look for "CHANGED")

int main ()
{
    // defines varibles
    double loanAmt = 0.0;
    double interest = 0.0;
    double term = 0.0;
    char quit;

    do
    {
        // user input
        cout << "Enter interest rate:" << endl;
        cin >> interest;
        cout << endl;

        cout << "Enter term in years:" << endl;
        cin >> term;
        cout << endl;

        cout << "Enter Loan amount:"<< endl;
        cin >> loanAmt;
        cout << endl;
        cout<<"\n";

        double monthlyPmt = loanAmt*(interest/1200)/(1-pow(1+(interest/1200),-1*term*12)); // amoritization formula
        double loanAmnt = loanAmt;
        cout << "For a "<<loanAmnt<<" loan your payment is " <<monthlyPmt<<endl<<endl;

        double monthlyInt =  loanAmt * (interest/1200);
        double principalPaid = monthlyPmt - monthlyInt;
        double balance = loanAmt-monthlyPmt;
        for (double i=0; i<term*12;i++)
        {
            double monthlyInt=balance*(interest/1200);
            double principalPaid=monthlyPmt-monthlyInt;
            if (balance<1.00)
                balance=0.0;
            //CHANGED
            else
                balance -= ( monthlyInt + principalPaid ) ;
            //CHANGED
            cout<<" New loan balance for month "<<i+1<<" is "<<balance<<endl<<endl;
            cout<<" Your interest paid for this month is " <<monthlyInt<<endl<<endl;
            cout<<" Your Principal paid for this month is " <<principalPaid<<endl;
            cout<< "\n";
            getchar();                   // pauses the program and allows you to view the output

        }

        // user can continue in a loop or quit
        //output
        cout<<"\n";
        cout<<"If you wish to continue press  C then the enter key on your keyboard.\n";
        cout<<"If you wish to quit press Q then the enter key on your keyboard.\n";

        //user input
        cin>>quit;
        cout<<"\n";
    }
    while((quit!='q') && (quit!='Q')) ;

    cout<<"Thank you for trying my simple mortgage calculator\n";

    return 0 ;
}

hi gaurav arora here!my problem is that i wanna make a code which reads a string and checks whether the string is palindrome or not.i dont know how exactly to code this program. will someone help me. i would appriciate.

thanx

hey buddy,

Before u get a loud bashing from the moderators of the site regarding ethics , do keep in mind that each new problem is supplosed to be as a new post.

So put up the request again in order to get the answers.

>> The balance is never decreased by the amount of each montly payment.
That's because you have not written any code/statement that decreases the variable "double balance". :)

My guess is you need to change as I've done (Look for "CHANGED")

int main ()
{
    // defines varibles
    double loanAmt = 0.0;
    double interest = 0.0;
    double term = 0.0;
    char quit;

    do
    {
        // user input
        cout << "Enter interest rate:" << endl;
        cin >> interest;
        cout << endl;

        cout << "Enter term in years:" << endl;
        cin >> term;
        cout << endl;

        cout << "Enter Loan amount:"<< endl;
        cin >> loanAmt;
        cout << endl;
        cout<<"\n";

        double monthlyPmt = loanAmt*(interest/1200)/(1-pow(1+(interest/1200),-1*term*12)); // amoritization formula
        double loanAmnt = loanAmt;
        cout << "For a "<<loanAmnt<<" loan your payment is " <<monthlyPmt<<endl<<endl;

        double monthlyInt =  loanAmt * (interest/1200);
        double principalPaid = monthlyPmt - monthlyInt;
        double balance = loanAmt-monthlyPmt;
        for (double i=0; i<term*12;i++)
        {
            double monthlyInt=balance*(interest/1200);
            double principalPaid=monthlyPmt-monthlyInt;
            if (balance<1.00)
                balance=0.0;
            
            else
                balance -= ( monthlyInt + principalPaid ) ;
            
            cout<<" New loan balance for month "<<i+1<<" is "<<balance<<endl<<endl;
            cout<<" Your interest paid for this month is " <<monthlyInt<<endl<<endl;
            cout<<" Your Principal paid for this month is " <<principalPaid<<endl;
            cout<< "\n";
            getchar();                   // pauses the program and allows you to view the output

        }

        // user can continue in a loop or quit
        //output
        cout<<"\n";
        cout<<"If you wish to continue press  C then the enter key on your keyboard.\n";
        cout<<"If you wish to quit press Q then the enter key on your keyboard.\n";

        //user input
        cin>>quit;
        cout<<"\n";
    }
    while((quit!='q') && (quit!='Q')) ;

    cout<<"Thank you for trying my simple mortgage calculator\n";

    return 0 ;
}

I followed your advise. When I re-compiled and ran the code,
the program deincrimented the remaining balance as it should.
I am now looking for an even more elusive gremlin. As the program steps through each montly payment, (I used a 30 year term to test with) the remaining balance gets to zero at around payment 170 and remains at zero through to the last month. I suspected the problem was with the math inside the loop. I manipulated the math inside the loop and all I got was inacurate payments. This leads me to belive the problem is not with the math inside the loop after all, but rather the loop it self.
So I grabed the book and started checking my loop againts what the book says a for loop should be. So far its spot on but still zeros out half way trough the mortgage. So my best guess is either I am misunderstanding the book, or the problem is something I haven't learned yet. Any help would be apreciated.
Once again thank you for the help you have already provided.

>> balance -= ( monthlyInt + principalPaid )

I'd like you for my banker if you are going to deduct my monthly interest payment from my balance in addition to primical payment!

>> balance -= ( monthlyInt + principalPaid )

I'd like you for my banker if you are going to deduct my monthly interest payment from my balance in addition to primical payment!

If i am understanding you correctly, my problem should be solved
if I change thal line to read >>balance -= (monthlyPmt)
I will try it and let you know.

Strike # 2!!

Change the name balance to what it really is: the OutstandingPrincipalBalance. Now, which part of the monthly payment goes toward paying off the OutstandingPrincipalBalance and which part of the monthly payment do you pay the lender for the privilege of borrowing the Principal?

Whether the appropriate correction solves your entire program I haven't the foggiest notion, but it's a step in the right direction.

>> balance -= ( monthlyInt + principalPaid )

I'd like you for my banker if you are going to deduct my monthly interest payment from my balance in addition to primical payment!

If i am understanding you correctly, my problem should be solved
if I change thal line to read >>balance -= (monthlyPmt)
I will try it and let you know.

Strike # 3!!!

Try again maybe you'll hit it out of the park this time.

Just subtract the part of the monthly payment that reduces the outstanding principal balance from balance, not the entire monthly monthly payment!

>> balance -= ( monthlyInt + principalPaid )

I'd like you for my banker if you are going to deduct my monthly interest payment from my balance in addition to primical payment!

Yeah that was stupid.. :mrgreen::mrgreen:
So Banking is not an alternate professions for me.. :)


Anyway, I think "balance -= monthlyPmt ;" is the right thing to do and should solve the problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.