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

When I attempt to start without debugging, I receive the following error:
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.

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):
http://www.daniweb.com/forums/thread78223.html

Next time use this form of code tag for your snippets:
[code=cplusplus] your snippet

[/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;
	return total;

}

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

}

About paragraph 4 and your starting post: no info about the error in line 24 of mortgageTest.cpp (no appropriate default constructor available), no info about this message target (class Mortgage definition) ;).

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.

A compiler generates default constructor only if no user defined constructors. You define Mortgage constructor with three parameters - so you must define default constriuctor for this class if you have a program construct where default constructor needed (line 24 of the test module). That's all.

About paragraph 4 again (sorry ;)):

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

Where is "no luck" info? Where is compiler diagnostic messages? Obviously if you follow Evan's suggestion #1 (define default constructor) you can't get error message C2228...

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.