I've been taking an accelerated C++ program for 3 weeks now...my current assignment is to write a program that, with user input of a mortgage loan amount, the term of the loan, and the interest rate, display the monthly payment. Then list the loan balance and interest paid for each payment over the term of the loan without letting the list scroll off the screen. Allow user to continue viewing list, enter new data, or quit. My program is a mess (don't laugh, the monthly payment portion of the program worked last week). No compile errors, but the calculations are way wrong and the list doesn't stop scrolling. Any help would be greatly appreciated. :-)

Attached is the source file; I'm using Dev- as the compiler.

Attachments
//PRG 410 Week 4
//Calculate and display mortgage payment
//With user input and option to continue or quit
//List loan balance and interest paid
//Display partial list and allow user to continue list
//Program 3.0 4/11/05

#include <iostream>  //access cout and cin
#include <cmath>     //access power function
#include <stdlib.h>  //access standard library for clear screen
using namespace std;
int main()
{

  double MyAmount;    //define variables
  float MyRate;
  int MyTerm;
  int LengthMonths=12;
  int NumberOfPayments;
  int MonthlyPymtCtr;
  float MyPayment;
  double LoanBalance;
  float MonthlyIntPd;
  float MonthlyPrinPd;
  const char NL='\n';
  const char TAB='\t';
  char listmore;
  int dividelist=0;

  //prompt user to continue or quit
  char quit;         //declaration for character variable
  

  quit = 'C';   //initialize quit variable
  while (quit != 'Q' && quit != 'q')
  {
      //clear screen for next input
      system("cls");
      cout<<"enter mortgage amount, interest rate, and term of loan:"<<".\n"<<endl;
      cin>>MyAmount>>MyRate>>MyTerm;
  
      MyPayment = MyAmount * (MyRate/1200)/(1-pow(1+(MyRate/1200),-1*(MyTerm*12)));
      LoanBalance = MyAmount - MonthlyPrinPd;
      MonthlyPrinPd = MyPayment - (LoanBalance*MyRate);
      //output payment amount
      cout<<"Your mortgage payment per month is $"<<MyPayment<<".\n"<<endl;



      //loan balance and interest paid calculation

      for (MonthlyPymtCtr=1; MonthlyPymtCtr<=NumberOfPayments; ++MonthlyPymtCtr)

      {
      LoanBalance = LoanBalance - MonthlyPrinPd;
      MonthlyIntPd = LoanBalance * MyRate;


      //output loan balance and interest paid
      cout<<"My Loan Balance and Interest Paid\n\n";
      cout<<"Loan Balance"<<TAB<<"Interest Paid"<<NL<<NL;
      cout<<LoanBalance<<TAB<<MonthlyIntPd<<NL;
      ++dividelist;
      }
      while (LoanBalance>0);

      //check if user continues, enters new data, or quits

              if (dividelist==12)
              {
              cout<<"Enter 'C' to continue,"<<"'N' for new data,"
                  <<"'Q' to quit>";
              cin>>listmore;
                            if ((listmore=='C')||(listmore=='c'))
                            dividelist=0;
                            else if ((listmore=='N')||(listmore=='n'))
                            break;
                            else if ((listmore=='Q')||(listmore=='q'))
                            return 0;
               }


        do       //loop for check of valid user input
        {
         cout<<"Enter C to continue, Q to quit>";

         cin>>quit;       //retrieve user input
         cout<<'\n';
         } while ((quit!='q')&&(quit!='Q')&&(quit!='c')&&(quit!='C'));
        //if while statement is true, re-execute do loop
        //if statement is false, loop back to while loop on line 22
       //quit if user picks 'q' or 'Q'


  }
  //user has quit
  cout<<"Start saving your money!\n";
  
  return 0;
}

No compile errors, but the calculations are way wrong

How should the calculations work? what's the formula for calculating it?

and the list doesn't stop scrolling.

There is no break within the for loop, you do have a while loop after it, but it doesn't seem(with your formatting and lack thereof) it is ever going to be used within your printing..

Have you learned about functions yet? You should probably make the while loop into a function and call it to check for screen breaks.

The calculations should show the loan balance and interest paid after each monthly payment. The formulas are:

LoanBalance=Initial Loan Amount - MonthlyPrincipalPaid
Then, the loop should have the same loan balance formula, followed by:
MonthlyInterestPaid=LoanBalance * InterestRate

I'm not sure if these formulas are right.

I'm just beginning to learn about functions, so I will have to do some reading in order to set up the while loop as a function.

How should the calculations work? what's the formula for calculating it?


There is no break within the for loop, you do have a while loop after it, but it doesn't seem(with your formatting and lack thereof) it is ever going to be used within your printing..

Have you learned about functions yet? You should probably make the while loop into a function and call it to check for screen breaks.

Well, after getting a lot of help, I have a working program. I also have a much better understanding of loops now. The source code file is attached.

The calculations should show the loan balance and interest paid after each monthly payment. The formulas are:

LoanBalance=Initial Loan Amount - MonthlyPrincipalPaid
Then, the loop should have the same loan balance formula, followed by:
MonthlyInterestPaid=LoanBalance * InterestRate

I'm not sure if these formulas are right.

I'm just beginning to learn about functions, so I will have to do some reading in order to set up the while loop as a function.

Attachments
//PRG 410 Week 4
//Calculate and display mortgage payment
//With user input and option to continue or quit
//List loan balance and interest paid
//Display partial list and allow user to continue list
//Program 3.0 4/13/05

#include <iostream>  //access cout and cin
#include <cmath>     //access power function
#include <stdlib.h>  //access standard library
using namespace std;
int main()
{

  double MyAmount;    //define variables
  double MyRate;      //change data type from float to double precision
  int MyTerm;         //to cover calculations
  int NumberOfPayments;
  int MonthlyPymtCtr;
  double MyPayment;
  double LoanBalance;
  double MonthlyIntPd;
  double MonthlyPrinPd;
  const char NL='\n';
  const char TAB='\t';
  char listmore;
  int dividelist=0;   //variable used in loop to display partial list

  //prompt user to continue or quit
  char quit;         //declaration for character variable
  

  quit = 'C';   //initialize quit variable
  while (quit != 'Q' && quit != 'q')
  {

      cout<<"enter mortgage amount, interest rate, and term of loan:"<<".\n"<<endl;
      cin>>MyAmount>>MyRate>>MyTerm;
  
      MyPayment = MyAmount * (MyRate/1200)/(1-pow(1+(MyRate/1200),-1*(MyTerm*12)));
      

      //output payment amount
      cout<<"\nYour mortgage payment per month is $"<<MyPayment<<".\n"<<endl;



      //loan balance and interest paid calculation
      

      NumberOfPayments = MyTerm * 12;  //assign value to NumberOfPayments
      dividelist = 0;
      
      for (MonthlyPymtCtr=1; MonthlyPymtCtr<=NumberOfPayments; ++MonthlyPymtCtr)

      {
              //Calculation for MonthlyIntPd to set up value for MonthlyPrinPd
              MonthlyIntPd = MyAmount * (MyRate/1200);
              MonthlyPrinPd = MyPayment - MonthlyIntPd;
              LoanBalance = MyAmount - MonthlyPrinPd;
              if (LoanBalance < 0)
                  LoanBalance = 0;
              MyAmount = LoanBalance;   //end of payments
              


             //output loan balance and interest paid
             //only display heading once for every section
             if (dividelist == 0)
             {
                 //cout<<"My Loan Balance and Interest Paid\n\n";
                 cout<<"Loan Balance"<<TAB<<"Interest Paid"<<NL;
                 cout<<"____________"<<TAB<<"_____________"<<NL<<NL;
             }
             cout<<LoanBalance<<TAB<<TAB<<MonthlyIntPd<<NL;
             ++dividelist;
             
              //check for user to continue
              if (dividelist==12)
              {
                  cout<<"Enter 'C' to continue,"<<"'N' for new data,"
                            <<"'Q' to quit>";
                  cin>>listmore;
                  if ((listmore=='C')||(listmore=='c'))
                      dividelist=0;
                  else if ((listmore=='N')||(listmore=='n'))
                      break;
                  else if ((listmore=='Q')||(listmore=='q'))
                      return 0;
               }
     }
      
            //check if user continues, enters new data, or quits



        do       //loop for check of valid user input
        {
         cout<<"Enter C to continue, Q to quit>";

         cin>>quit;       //retrieve user input
         cout<<'\n';
         } while ((quit!='q')&&(quit!='Q')&&(quit!='c')&&(quit!='C'));
        //if while statement is true, re-execute do loop
        //if statement is false, loop back to while loop on line 35
       //quit if user picks 'q' or 'Q'
  }
  //user has quit
  cout<<"Start saving your money!\n";
  
  return 0;
}
This article has been dead for over six months. Start a new discussion instead.