Hey everyone of the c++ world. I am a little stuck. Here is what i am suppose to be doing.

Write the program as an object-oriented C++ program that calculates and displays the mortgage payment amount from user input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. Allow the user to loop back and enter new data or quit. Insert comments in the program to document the program.

Now, with this here are the instructions from the instructor:

"The following instructions tell you how I want your programs submitted for grade. Because we are doing the beloved mortgage calculator as an Object Oriented Program in C++, you will be submitting 3 files for each assignment. 1 header file, (.h), 1 source file, that defines the methods you declare in your header file, (.cpp), and 1 main source file, which will have your main function in it. (also .cpp).

The header file is where your class declaration appears. As a rule, it should have the same name as the class, but in our case, I'll also want your name incorporated as part of the file name. (makes it easier for me to get everything in the right place).
The source file that defines your class should have the same name as the header file, but will have a .cpp extension.
The main file, (sometimes called the driver) should have the word main and your name incorporated as part of the file name, and be followed by the .cpp extension also."

So i have wrote my code, but for some reason i keep getting a "0" answer when i run the program. Can anyone help with this. Here is the code:

The first is the ".h" file.

class mortgagePayment {
public:
	void Mortgage();
	
private:
	double tPayment(double principle, double term, double interest);
};

here is the paymentCalc.cpp file

#include "mortgagePayment.h"
#include <iostream>
#include <math.h>

using namespace std;

double mortgagePayment::tPayment(double principle, double term, double interest) {
	term = 0;
	int years = term *12;
	double monthlyInterest = interest/12/100;
	double total = (principle * monthlyInterest)/(1-pow(1 + monthlyInterest, -years)); //total for mortgage
	return(total);
}

void mortgagePayment::Mortgage() {
	double principle = 0;
	double rate = 0;
	int term = 0;
	double total = 0;
	
	cout << "Please enter Loan amount: ";
	cin >> principle;
	cout << "Please enter desired interest rate: ";
	cin >> rate;
	cout << "Please enter term: ";
	cin >> term;
	cout << "Your total monthly payment is: $" <<total;
	cout << "\n";
};

here is the mainMortgageCalculator.cpp

#include <iostream>
#include "mortgagePayment.h"

using namespace std;

int main() {
	
	bool Exit = false;
	char ch;
	mortgagePayment calculator;  // A mortgage calculator object
	
	while (!Exit) {
		
		calculator.Mortgage();
		
		cout << "Would you like to enter another loan? (y/n):";
		cin >> ch;

		if (ch != 'y') {
			Exit = true;
		}
	}
}

I called it in the second set of code.

No, you don't. There is correct code

void mortgagePayment::Mortgage() 
{
	double principle = 0;
	double rate = 0;
	int term = 0;
	double total = 0;

	cout << "Please enter Loan amount: ";
	cin >> principle;
	cout << "Please enter desired interest rate: ";
	cin >> rate;
	cout << "Please enter term: ";
	cin >> term;
	total = tPayment(principle, term, rate); //just call your tPayment method
	cout << "Your total monthly payment is: $" <<total;
	cout << "\n";
}

void

Okay, i changed that, now i am getting the answer of inf??? Not sure what is wrong, or what that means? Any help now?

Edited 7 Years Ago by hollywoood69: n/a

So i have been at this program for hours....and still no luck. I have made the changes that were suggested...but i get an answer of inf.....not sure what to do from here. any help

So i have been at this program for hours....and still no luck. I have made the changes that were suggested...but i get an answer of inf.....not sure what to do from here. any help

double mortgagePayment::tPayment(double principle, double term, double interest) {
	term = 0;
	int years = term *12;
	double monthlyInterest = interest/12/100;
	double total = (principle * monthlyInterest)/(1-pow(1 + monthlyInterest, -years)); //total for mortgage
	return(total);
}

Line 2 - look what you do here.
Line 3 - years = 0.

Your formula uses years, so replace it with 0. Anything raised to the 0 power is 1. Denominator is 1 - 1 = 0. Divide by 0 error.

Delete line 2 and see if you get better results.

This article has been dead for over six months. Start a new discussion instead.