I have been trying to get this C++ program to work for my class on a mortgage calculator. I am suppose to let the user enter the amounts for the principal, the term, and the interest. Also, I am suppose to let the user decide to do another mortgage or quit the program. I am able to do the part about the loop. Yet, I am getting a weird result for the monthly payment as $-1.#HD. Here is my code so may be some one can find the mistake I made:

// Mortgage Calculator: This program will calculate the monthly payment for a mortgage based upon the
// values for the loan amount, interest rate, and term.
// David Raymore
// POS 440
// Assignment 2
// June 22, 2006
// Barbara Hecker
// header files
#include <iostream>
#include <cmath>

using namespace std;

// declaring variables
float p = 0; /* Loan Amount */
float i = 0; /* Interest Rate APR */
int l = 0; /* Term */
float j = i/(12 * 100); /* Calculate Interest */
int n = l*12; /* Number of months */
double m = 0; /* Monthly Payment */
int choice = 0;

int main ()
{
do
{
cout << "Enter the loan amount: " << endl;
cin >> p;
cout << "Enter term of the loan: " << endl;
cin >> l;
cout << "The interest rate is: " << endl;
cin >> i;

// Calculating the mortgage using this formula
m = (p*j)/(1-pow((1+j),-n));

cout << "The monthly payment is: $" << m << endl;
cout << "Enter 1 to enter new values for another calculation or enter 2 to quit" << endl;
cin >> choice;
}while (choice==1);
return 0;
}

Sincerely,
David Raymore

you can not initialize variables using calculations as shown at the beginning of your program (globals). Variables are initiailized only once when the program starts. If you want the program to do the calculations then you have to do them at the appropriate time, such as set the value of j only after the value of i is known.

Also, use meaningful variable names -- one letter variables, although the compiler will not complain, they are not very comprehensible to human eye. Instead of variable i rename it interest -- only takes a little more typing but makes your program a great deal more professional looking and easier to understand.

Adding .0 to the constants as shown below will tell the compiler to use floating point arithametic, not integer arithmetic.

cin >> i;
 j = i/(12.0 * 100.0);
m = (p*j)/(1-pow((1+j),-n));

you can not initialize variables using calculations as shown at the beginning of your program (globals). Variables are initiailized only once when the program starts. If you want the program to do the calculations then you have to do them at the appropriate time, such as set the value of j only after the value of i is known.

Also, use meaningful variable names -- one letter variables, although the compiler will not complain, they are not very comprehensible to human eye. Instead of variable i rename it interest -- only takes a little more typing but makes your program a great deal more professional looking and easier to understand.

Adding .0 to the constants as shown below will tell the compiler to use floating point arithametic, not integer arithmetic.

cin >> i;
 j = i/(12.0 * 100.0);
m = (p*j)/(1-pow((1+j),-n));

Thanks, Ancient Dragon. I will try that to see if it works. I will post again after I make some modifications.

David

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.