Solved

# C++ Mortgage Calculator - \$1.#INF displays

Posted on 2007-08-06
209 Views
Hi,
I was having a couple of challenges with my C++ Mortgage Calculator.
For some reason, when the Monthly Payment displays it reads, "\$1.#INF"
My 'Quit' code is not working either, but my main concern is the Monthly Payment.
Here is my code:

#include <math.h>
#include <iostream>
using namespace std;
int main ()

{
int myMortgage = 0;
int myTerm = 0;
float myInterest = 0.000;
int months = myTerm * 12;
double monthlyPayment;
int quit = 1; // decides if user wants to quit
{

cout << "Input the total loan amount:\n";
cin >> myMortgage;

cout << "Input your interest rate:\n";
cin >> myInterest;

cout << "Input your number of years:\n";
cin >> myTerm;

monthlyPayment = (myMortgage * (myInterest / 12)) / (1 - 1 / pow ((1 + myInterest / 12), months));

cout << "Your monthly payment is: \$" << monthlyPayment<< "\n";
cout << "To Enter a new Loan Amount press 1 or 2 to quit\n";
cin >> quit;
} while (quit==1);
return 0;

}
0
Question by:fourweex

LVL 86

Accepted Solution

Simple error, big cause:

in

monthlyPayment = (myMortgage * (myInterest / 12)) / (1 - 1 / pow ((1 + myInterest / 12), months));

you forgot to replace "months" with "myTerm", which you are inputting from the user ("months" is effectively 0 then)- make that

monthlyPayment = (myMortgage * (myInterest / 12)) / (1 - 1 / pow ((1 + myInterest / 12), myTerm));

and your code works as it should.
0

LVL 86

Expert Comment

>>My 'Quit' code is not working either

Make that

} while (quit!=2);

;o)
0

Author Comment

Thank you for your help, both solutions work perfectly.
0

LVL 53

Expert Comment

Just for the record, the cause is a division by 0. If you see output like \$1.#INF, that's the first thing you should think about ...
0

## Featured Post

### Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.