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;
		}
	}
}

Recommended Answers

All 10 Replies

I called it in the second set of code.

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?

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.

Thank you!!!! It worked....i am so grateful!!!

Thank you!!!! It worked....i am so grateful!!!

You're welcome. Glad it worked.

I have the same assignment.. I've come this far. After I get the amounts from the user, how do I use what they inputed to save in the program so I can use those same answers in the monthlyPayment function for the calculation? Any help is greatly appreciated!

#include <iostream>

#include <string>

#include <cmath>

using namespace std;

class Payment {

    private:

  int amount;
int term;
double interest;

public:

int getAmount(int amount) {
    cout << "Enter your loan amount: " << amount << endl;
    if (amount <= 0)
        cout << "please enter a valid entry." << endl;
    return amount;
    }

int getTerm(int term) {
    cout << "Enter your term: " << term;
    if (term <= 0)
        cout << "please enter a valid entry." << endl;
    return term;
}

double getInterest(int interest) {
    cout << "Enter your interest rate: " << interest << endl;
    if (interest <= 0)
        cout << "please enter a valid entry." << endl;
    return interest;
}

void monthlyPayment();
void displayMessage();
};

int main ()

{

}

void monthlyPayment() {

}
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.