• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

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

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
fourweex
Asked:
fourweex
  • 2
1 Solution
 
jkrCommented:
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
 
jkrCommented:
>>My 'Quit' code is not working either

Make that

} while (quit!=2);

;o)
0
 
fourweexAuthor Commented:
Thank you for your help, both solutions work perfectly.
0
 
Infinity08Commented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now