So I am stuck on my assignment where I am suppose to make mortgage calculator and I am suppose to do the following things:

1. Mortgage Calculator : prompts the user for (a) principal, (b) Annual interest rate , (c) number of months.It calculates the monthly payment and prints the summary of the mortgage.

2. Loan estimator : base on (a)monthly payment one is willing to pay (b)annual interest rate, and (c) terms in number of months , it calculates the maximum affordable loan amount.

3.Credit card minimum payment system : base on the credit card debt, annual interest rate , and the minimum payment for each month, it calculates the number of payments (months) it takes to pay-off the credit carddebt.

I was given a header file:

class Mortgage{

     private :

         double  principal ;    // amount
         double  apr ;          // annual rate
         int term ;             // terms in number of month
         double payment ;

    public :

		double get_principal() { return principal;};
		double get_apr () { return apr ; } ;
		double get_payment() {return payment ; };
		int get_term() { return term; } ;

		double calc_payment(double P, double rate, int term);  // calculate the monthly payment
		double estimate(double pay, double rate, int term) ;  // estimate the max loan

		void display() ;    // print out the summary of the mortgage
		double total();   // compute total interest paid


	// constructors
    Mortgage(double principal, double rate , int term) ;
    Mortgage(Mortgage & mtg) ;     // copy constructor
	Mortgage(double dummy,  double rate, int term, double monthly_payment) ; //  for the use of option 2
	Mortgage() ;  // default constructor



} ;

double Mortgage::calc_payment(double P, double rate, int term){

	return P * ((rate * (pow(( 1+ rate ), term)) / (pow((1 + rate), term) - 1)));
};

Mortgage::Mortgage(double principal, double rate, int term){

	double mortgage;
	double pay = 0.0;

	mortgage = pay * ((pow((1+rate),(term))-1)/((rate*(pow((1+rate),(term))))));

}

and now I am suppose to implement 3 functions that fulfills the above listed requirements. This is the code I have so far:

#include "stdafx.h"
#include<iostream>
#include<cmath>
#include<iomanip>

#include "mortgage.h"

using namespace std;

void process();
void maxLoan();
void debt();
void display();

double calc_payment(double P, double rate, int term);
void estimate(double payment, double principal, double rate, int term);

void main_menu() {

	printf("\n\tOptions:\n") ;
	printf("\n\t1: Determine the monthly payment based on \n\t\tLoan amount, interest rate and terms.\n") ;
	printf("\n\t2: Determine the maximum Loan amount based on \n\t\tmonthly payment, interest rate and terms.\n") ;
	printf("\n\t3: Determine the total interest paid based on \n\t\tDebt amount,monthly payment and rate .\n") ;
	printf("\n\t0: Quit \n") ;

}


int _tmain(int argc, _TCHAR* argv[])
{
	int choice;
	char resp[10];

	printf("\n\t\tWelcome to CSC262 Financial Computing System!!\n") ;

		while(true) {

			main_menu();
		
		while(true){
			printf("\n\t\tYour choice is: ") ;
			cin >> choice ;
			if ( choice < 0 || choice > 3 ) {
				printf("\n%d is an invalid choice!") ;
				printf("\nPlease enter your choice 0, 1, 2 or 3 :") ;
			}
			else break;
		}
		switch (choice) {
			case 1: process();
			break;
			case 2: maxLoan();
			break;
			case 3: debt();
			break;
			case 0: printf ("\n\nThank you for using CSC262 Financial Computing System!\n\n");
			return 0;
			break;
		}
		
		printf("\nWant to continue: (Y/N)? ");
		cin >> resp;
		if ( resp[0] == 'Y' || resp[0] == 'y') continue;
		else break;
		}

		//display();

		printf("\n\nThank you for using the CSC 262 Financial Computing System!!!\n\n");

		return 0;

}


void process(){ // process option 1 : determine Monthly mortgage payment
	int term ;
	double principal , rate ;
	double total_interest = 0.0 ;
	//char resp[10];
	double payment = 0.0;
 
	cout << "\n\tPlease enter the Loan amount in $ :" ;
	cin >> principal ;
	cout << "\n\tPlease enter the interest rate (e.g 4.5 for 4.5%) :" ;
	cin >> rate ;
	cout << "\n\tPlease enter the terms in # of months :" ;
	cin >> term ;

Mortgage mortgage(principal, rate, term);
display();

}

/*double calc_payment(double P, double rate, int term)
{
	double payment;
	payment = P * ((rate*pow((1+rate),(term)))/(pow((1+rate),(term))-1));
	return payment;
}*/
void estimate(double payment, double principal, double rate, int term)
{
	cout << setw(14) << "Interest" << setw(10) << "Principal" << endl;
	cout << setw(14) << "--------" << setw(10) << "---------" << endl;
 
	double total_principal = 0; // total of payments applied to principal
	double total_interest = 0; // total of payments applied to interest	

	for (int i = 0; i < term; i++) { // print each payment 

	double to_interest = principal * rate; // periodic interest
	double to_principal = payment - to_interest; // principal part of payment

	principal -= (payment - to_interest);
	total_interest += to_interest;
	total_principal += to_principal;

	cout << setw(3) << i + 1 << ":" <<
	setw(10) << to_interest << setw(10) << to_principal << endl;

	}

	cout << endl << setw(14) << total_interest << setw(10) << total_principal << endl;
}



void display() {

	double principal = 0.0;
	double rate = 0.0;
	double payment = 0.0;
	int term = 0;
	char resp[10];

	cout << "\nMortgage Summary : \n\n";
	cout << "Principal : " << principal;
	cout << "\nAnnual Rate : " << rate << "%\n";
	cout << "Term : " << term << "\n";
	cout << "Monthly Payment : " << payment << "\n\n";
	 
	printf("Do you want to print the payment table? (Y/N) ");
	cin >> resp;
	if ( resp[0] == 'Y' || resp[0] == 'y')
	estimate(payment, principal, rate, term);
	else return;

}

void maxLoan(){

	double rate , pay , MaxLoan;
	int term;

	cout << "\n\tPlease enter the monthly payment you feel comfortable with : ";
	cin >> pay ;
	cout << "\n Please enter the interest rate : " ;
	cin >> rate;
	cout << "\n Please enter the term in # of months : ";
	cin >> term;

	MaxLoan = pay * (pow((1+rate),(term))-1)/(rate*(pow((1+rate),(term))));

	cout << "\n The maximum loan will be : " << MaxLoan;
}

void debt(){ // process option 3

	double principal = 0.0;
	double rate = 0.0;
	double total_interest = 0.0 ;
	double payment = 0.0 ;
	double interest = 0.0 ;

	int n = 0 ;

	cout << "\nPlease enter the debt amount in Dollars : " ;
	cin >> principal;
	cout << "\nPlease enter the interest rate : " ;
	cin >> rate;

	double remain = principal ;
	if ( remain > 0 ){
		interest = remain * (rate/1200) ;
		total_interest += interest ;
		n++ ;
		remain -= payment - interest ;
	}
	else {
		exit(0);
	}
	//return remain;

	cout << "\nThe total interest you will have to payoff is: " << remain << endl;

	

}

The problem is that none of my functions are working correctly. I am mainly having issues with the calculations. Could anyone please help me solve this issue?

Recommended Answers

All 4 Replies

instead of doing

pay * (pow((1+rate),(term))-1)/(rate*(pow((1+rate),(term))));

why don't you break it up in segments and then combine them together. Here is a math program that I used which might be able to help you code.

float the_work(float pie_over_180, float angle, float force, float meters, float kinetic_friction)
{
	float Angle_with_pie_over_180 = (angle * pie_over_180);
	float cosine = cos(Angle_with_pie_over_180);
	float force_times_cosine = (force * cosine);
	float force_with_cosine_minus_friction = (force_times_cosine - kinetic_friction);
	float answer = (force_with_cosine_minus_friction * meters);
	return answer;
}

float Angled_force_Work()
{
	system("cls");

	float pie_over_180 = (3.14159265 / 180);
	float angle, force, meters, kinetic_friction;

	cout << "For this your formula is Fcx = Fc(cos(angle)) " << endl << endl;
	cout << "So that way we can use the answer to find the net minus kinetic friction" << endl << endl;
	cout << "So we can find Force_Net times displacementX" << endl << endl;
	cout << "Let's start it off..." << endl << endl;
	system("pause");
	system("cls");

	cout << "What is your force? ";
	cin >> force;
	cout << endl;
	cout << "What is your angle? ";
	cin >> angle;
	cout << endl;
	cout << "How many meters do you wish to travel? ";
	cin >> meters;
	cout << endl;
	cout << "What is your kinetic friction? ";
	cin >> kinetic_friction;
	cout << endl;

	float Answer = the_work(pie_over_180, angle, force, meters, kinetic_friction);

	cout << "Your answer is: " << Answer << endl << endl;
	system("pause");
	system("cls");
	return 0;
}

Probably you will hate for me saying this, but I don't think you understand how classes really work. If I were I would step back from this assignment, and make a simple class instead, and play with it until I understand the basics. Just write a simple one, with get/set methods, make an instance of it, pass it to the function as parameter, as reference, work on it and see what happens.

Also don't mix printf with streams, this is C++ not C anymore.

while(true) {
 
main_menu();

You should really avoid things like that, use do - while or switch instead.

@Zvjezdan23 That was the given part of his assignment, don't try to fix that. And how is that code of yours relevant to his?

commented: Well written :) +3

It's a different type of way in figuring out power and whatever other result number he needs. Be can make a function by passing the variables so he can calculate the math work outside of the code and he can keep it more organized. He could even put that work in a different header so it does not confuse him while he is developing his code.

It's another way of giving the result of something so he can declare the result of the function the_work and pass that declaration to a class so he can have the result in hand.

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.