Hey everyone,
I typically don't like to go on forums and throw my code at people and expect answers... but, I'm a beginner (and a little desperate) with all this so a little help would be much appreciated. Here's the details:

• The purpose of the program is to have the user enter their loan amount, number of years of the mortgage, and the annual interest rate of the loan. The program then calculates the fixed monthly payment, total payment, interest paid, and principal paid.
1. The formula to calculate the interest paid in the current month:
interestPaid = (annualInterestRate / 1200) * outstandingLoanAmount.
2. The formula to calculate principalPaid during the current month:
principalPaid = monthlyPayment – interestPaid in the current month.
3. The formula to calculate subsequent outstandingLoanAmount:
outstandingLoanAmount = outstandingLoanAmount – principalPaid in the current month.

For the first month, the outstandingLoanAmount is the same as the loanAmount.

• I'm trying to write a program that displays the following:
Fixed monthly payment: \$xxx.xx
Total payment: \$xxxxx.xx

Month Interest Paid Principal Paid.
------- --------------- ----------------
1 \$aaa.aa \$bbb.bb
2 \$ccc.cc \$ddd.dd
3 \$eee.ee \$fff.ff
• • •
• • •
• • •
----------- -----------------
Total \$yyyy.yy \$zzzzz.zz

• My program consists of two .cpp files and one header file.

I'd hate to post the entirety of my code, so I'm only going to paste what I think is necessary to answer my question.

Main Function:

``````int main()
{
double loanAmount;
int numOfYears;
double annualInterestRate;
int displayResult;

Mortgage myMortgage();

cout << "Enter loan amount: \n";
myMortgage.setLoanAmount( loanAmount );

cout << "Enter number of years: \n";
cin >> numOfYears;

cout << "Enter annual interest rate: \n";
cin >> annualInterestRate;

cout << displayResult;

return 0;
}``````

Mortgage Constructor:

``````Mortgage::Mortgage (double amount, int years, double annualRate)
{
//loanAmount = amount;
setLoanAmount(amount);

//numOfYears = years;
setNumOfYears(years);

//annualInterestRate = annualRate;
setAnnualInterestRate(annualRate);

}``````

Set Function:

``````void Mortgage::setLoanAmount ( double amount )
{
loanAmount = amount;
}``````

error C2228: left of '.setLoanAmount' must have class/struct/union

I appreciate any help I can get. If the code I provided isn't enough code for you to help, I will be more than glad to post more. Thanks a lot.

3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by ArkM

I think the problem is with the constructor. You either need to add parameters matching the class constructor in the line `Mortgage myMortgage();` or make a default constructor with no parameters.

It's the same problem as this:

``````#include <iostream>

using namespace std;

class MyClass {
public:
MyClass( int a );
int num;
};

MyClass::MyClass( int a )
{
num = a;
}

int main()
{
MyClass myclass();

myclass.num = 5; // Error
cout << myclass.num; // Error

return 1;
}``````

You can fix it by creating another constructor `MyClass();` , or by changing `MyClass myclass();` to `MyClass myclass( 5 );` .

It's too early for start without debugging: you can't compile the source w/o errors.
I think it's not only this error message was emitted by the compiler. Obviously `Mortgage myMortgage();` was not recognized as a valid declaration too.

May be Evan's post hits the target but stricktly speaking in this case we need another piece of sources to help you (namely class Mortgage definition). Please read this daniweb forum instruction carefully (especially the 4th paragraph):

Next time use this form of code tag for your snippets:

[/code]
It numerates source lines then it's possible to refer to line numbers.

Thanks for the replies, Evan and ArkM.

Evan, I tried both of your solutions, but I had no luck.

ArkM, I did read that sticky before posting. I thought I had answered all the questions in paragraph 4.

Here's my code:

Mortgage.h

``````class Mortgage
{
public:
Mortgage ( double, int, double );

//get functions
double getLoanAmount();
int getNumOfYears();
double getAnnualInterestRate();

//set functions
void setLoanAmount(double);
void setNumOfYears(int);
void setAnnualInterestRate(double);

double fixedMonthlyPayment();
double totalPayment();
void displayResult();

private:
double loanAmount;
int numOfYears;
double annualInterestRate;

}; // end class Mortgage``````

Mortgage.cpp

``````#include <iostream>
using std::cout;
using std::endl;

#include <cmath>
using std::pow;

#include "Mortgage.h"

Mortgage::Mortgage (double amount, int years, double annualRate)
{
//loanAmount = amount;
setLoanAmount(amount);

//numOfYears = years;
setNumOfYears(years);

//annualInterestRate = annualRate;
setAnnualInterestRate(annualRate);

} //end Mortgage constructor

//function that sets the data members;
void Mortgage::setLoanAmount ( double amount )
{
loanAmount = amount;

} //end function setLoanAmount

double Mortgage::getLoanAmount()
{
return loanAmount;
} // end function getLoanAmount

double Mortgage::fixedMonthlyPayment()
{
double monthlyRate = annualInterestRate/1200;
int month = numOfYears * 12;
double fixedMonthlyPayment;

fixedMonthlyPayment = loanAmount * monthlyRate /
(1.0 - (1.0 / pow(1 + monthlyRate, month) ) );

return fixedMonthlyPayment;

}

double Mortgage::totalPayment()
{
int month = numOfYears * 12;
double total;

total = fixedMonthlyPayment() * month;

}

//display the result to the Mortgage user
void Mortgage::displayResult()
{
int counter = 1;
double interestPaid;
double outstandingLoanAmount;
double principalPaid;

cout << "Fixed Monthly Payment: \$" << fixedMonthlyPayment() << "\n";
cout << "Total Payment: \$" << totalPayment() << "\n\n";

cout << "Month     Interest Paid     Principal Paid\n";
cout << "-----     -------------     --------------\n";

outstandingLoanAmount = loanAmount;

while (counter <= numOfYears * 12)
{
interestPaid = outstandingLoanAmount * annualInterestRate/1200;
principalPaid = fixedMonthlyPayment() - interestPaid;
outstandingLoanAmount = outstandingLoanAmount - principalPaid;

cout << counter << "     " << interestPaid << "     " << principalPaid << "\n";
}

} //end function displayResult``````

mortgageTest.cpp

``````#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::fixed;

#include <cmath>
using std::pow;

#include <iomanip>
using std::setprecision;

#include <string>
using std::getline;

#include "Mortgage.h"

int main()
{
double loanAmount;
int numOfYears;
double annualInterestRate;
int displayResult;

Mortgage myMortgage();

cout << "Enter loan amount: \n";
myMortgage.setLoanAmount( loanAmount );

cout << "Enter number of years: \n";
cin >> numOfYears;

cout << "Enter annual interest rate: \n";
cin >> annualInterestRate;

cout << displayResult;

return 0;

}``````

Just as Evan expected: no class Mortgage constructor without parameters (so called default constructor). Therefore `Mortgage myMortgage()` declaration of myMortgage variable is wrong and `myMortgage.setLoanAmount(loanAmount)` expression is invalid: myMortgage is not recognized as a class or struct variable.