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

Posted on 2007-08-06
Last Modified: 2010-05-18
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;

Question by:fourweex
    LVL 86

    Accepted Solution

    Simple error, big cause:


    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.
    LVL 86

    Expert Comment

    >>My 'Quit' code is not working either

    Make that

    } while (quit!=2);


    Author Comment

    Thank you for your help, both solutions work perfectly.
    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 ...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    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 (…
    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++.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now