having a problem with a homework problem

I'm currently stuck on a homework problem. I have most of it down, but when my input hits the variable setting functions, it turns the input into garbage (even though they arent necessary, but I need them as part of the problem).

the problem is:

Design a class that will determine the monthly payment on a home mortgage. The monthly payment with interest compounded monthly can be calculated as follows:
Payment = (Loan * (Rate / 12) * Term) / Term - 1
Where
Term = (1 + Rate / 12) to the (12 * Years) power
Payment = the monthly payment
Loan = the dollar amount of the loan
Rate = the annual interest rate
Years = the number of years of the loan
The class should have member functions for setting loan, rate, and years. It should have getter functions for the monthly and total amount paid to the bank at the end of the loan period.
Input validation: Do not accept negative numbers.


here is what i have so far:

javascript:tx('code')
#include <iostream>
#include <cmath>
using namespace std;


class mortgage
{
public:

double loan;
double rate;
double years;
double L, R, Y;

mortgage();

void setLoan(double){ loan = L; }
void setRate(double){ rate = R; }
void setYears(double){ years = Y; }

double getPayment(double);
double getTotal(double);
};


mortgage::mortgage()
{

}



double mortgage::getPayment(double term)
{
return ((loan*(rate / 12)*term) / (term - 1));
}

double mortgage::getTotal(double term)
{
return getPayment(term)*years;
}


int main()
{

mortgage M;

double term = 0.0;

cout << "What is the loan amount given?: ";
cin >> M.L;

cout << endl;

cout << "What is the interest rate on the loan?: ";
cin >> M.R;

cout << endl;

cout << "How many years is the loan period?: ";
cin >> M.Y;

cout << endl;

term = pow((1 + (M.rate / 12)), 12 * M.years);

cout << "Monthly payment amount: $" << M.getPayment(term) << endl;
cout << "Total payment after interest: " << "$" << M.getTotal(term) << endl;


system("pause");
return 0;
}



If anyone can give me a hand here that would be great
The problem is that you do not set the member variable loan, rate, years anywhere so they remain garbage.

The member variables L, R, Y are nonsense. Remove them and solely use loan, rate, years instead.


Furthermore: Within the constructor of a class (mortgage::mortgage()) you should initialize the member variables.
Topic archived. No new replies allowed.