The assignment is to design a savings account program.

The program has to include: a constructor that accepts an argument of type double and uses it to set savingsBalance. If the value is passed is less than 0, set savingsBalance to 0.

Add a public member function named calaculateMonthlyInterest that accepts no argument and returns nothing.

Add a public member function named modifyInterestRate that accepts a single argument of type double and returns nothing. Declared the function as static.

The errors that I have are:

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

error C2511: 'void Savings_Account::modifyInterestRate(double)' : overloaded member function not found in 'Savings_Account'

error C2065: 'aI' : undeclared identifier

error C2662: 'Savings_Account::calculateMonthylyInterest' : cannot convert 'this' pointer from 'const Savings_Account' to 'Savings_Account &'

#ifndef SAVINGS_ACCOUNT_H
#define SAVINGS_ACCOUNT_H

class Savings_Account
{
public:
	//constructor
	Savings_Account(double s = 0)
	{
		setsavingsBalance(s);
	}

	void setsavingsBalance(double);
	
	double getsavingsBalance();
	double calculateMonthylyInterest();
	static double modifyInterestRate();
	double print() const;

private:
	double savingsBalance;
	static double annualInterestRate;
	//double customerInput;

};
#endif

#include "stdafx.h"
#include "Savings_Account.h"
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

//Savings_Account::Savings_Account(double savingsBalance, double annualInterestRate)
//{
//	setsavingsBalance(savingsBalance)
//}

void Savings_Account::setsavingsBalance(double s)
{
	if(savingsBalance > 0)
	{
		savingsBalance = s;
	}
	else
		savingsBalance = 0;
}

double Savings_Account::getsavingsBalance()
{
	return savingsBalance;
}

void Savings_Account::modifyInterestRate(double aI)
{
	if(aI > 0 && aI <1)
	{
		annuallyInterestRate = aI;
	}
	else 
		annuallyInterestRate =0.03;
}

double Savings_Account::annualInterestRate = 0;

double Savings_Account::calculateMonthylyInterest()
{
	double customerSavingsBal;
	
	cout << "Please enter your savings balance: ";
	cin >> savingsBalance;
	
	customerSavingsBal= savingsBalance * aI;
}

double Savings_Account::print() const
{
	cout << "Your savings account balance is: " << calculateMonthylyInterest() << endl;
}

// lab1.1.cpp : Defines the entry point for the console application.
//

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

using std::cin;
using std::cout;
using std::endl;

int _tmain(int argc, _TCHAR* argv[])
{
	//Savings_Account object
	Savings_Account savingsAccount;

        //prompt customer for savings balance and print the balance out
	savingsAccount.calculateMonthylyInterest().print();

	return 0;
}

Recommended Answers

All 4 Replies

Look at where the first error occurs - is the variable aI in scope there?

What's wrong with this pair (prototype and implementation)?

static double modifyInterestRate();

void Savings_Account::modifyInterestRate(double aI)

Why are you using .print( ) the way you do, tacking it onto another method?

Error 4 is probably related to 3.

It's helpful to post line numbers with your errors.

In any case, most of these are syntactic errors for which you may want to read up on a little more.

Error C2228: left of '.print' must have class/struct/union relates to this line: savingsAccount.calculateMonthylyInterest().print(); . "calculateMonthylyInterest()" returns a double and not a class that you can access members of. I suspect what you wanted to do is this:

savingsAccount.calculateMonthylyInterest();
savingsAccount.print();

Although since print (needlessly) calls calculateMonthylyInterest(), you could just have print() which would do the same thing.

Error C2511: 'void Savings_Account::modifyInterestRate(double)' : overloaded member function not found in 'Savings_Account' relates to the two different declarations of modifyInterestRate (obviously). Look at where you first declared it inside the class statement, Are the return types the same? How about the arguments inbetween the brackets? They need to match for it to work.

Careful of the scope of your variables as well, the 'aI' error comes up because you haven't created an aI variable in that function.

If you want the program to be functional without returning anything you will need to learn how to use class member variables. Using calculateMonthlyInterest() on cout is useless as it doesn't return anything. You might want to have a look at a function tutorial such as the one here or this one. Those should help even though they don't talk about classes.

Hopefully I've given you enough to get started - keep in mind this is still your assignment, you're better off learning yourself rather than just having the answer handed out to you.

The errors that I have are:

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

This is from this code :

//prompt customer for savings balance and print the balance out
	savingsAccount.calculateMonthylyInterest().print();

To solve it you can do 2 things :
1) Make calculate return the object
2) or use the call separately like so :

savingsAccount.calculateMonthylyInterest();
savingsAccount.print();

error C2511: 'void Savings_Account::modifyInterestRate(double)' : overloaded member function not found in 'Savings_Account'

That error is related to this.

static double modifyInterestRate(); //prototype
//and its definition 
void Savings_Account::modifyInterestRate(double aI)
{
	if(aI > 0 && aI <1)
	{
		annuallyInterestRate = aI;
	}
	else 
		annuallyInterestRate =0.03;
}

Do you see the difference? Change the prototype to
match its definition. So it should be

void modifyInterestRate(double);
//instead of 
//static double modifyInterestRate();

error C2065: 'aI' : undeclared identifier

This is from this snippet :

double Savings_Account::calculateMonthylyInterest()
{
	double customerSavingsBal;
 
	cout << "Please enter your savings balance: ";
	cin >> savingsBalance;
 
	customerSavingsBal= savingsBalance * aI; //<-- HERE
}

Where is aI declared in there? Did you mean to pass it as a parameter?
If so then you need to change the functions prototype as well.

error C2662: 'Savings_Account::calculateMonthylyInterest' : cannot convert 'this' pointer from 'const Savings_Account' to 'Savings_Account &'

That is probably from the other errors.

1 more this , this code is meaningless,

double Savings_Account::calculateMonthylyInterest()
{
	double customerSavingsBal;
 
	cout << "Please enter your savings balance: ";
	cin >> savingsBalance;
 
	customerSavingsBal= savingsBalance * aI;
}

customerSavingBal goes out of scope and nothing is changed
to you class variables.

Did you mean :

customerSavingsBal= savingsBalance * aI;
        savingsBalance    = customerSavingsBal; //???

The assignment wanted me to write the print() onto another method. It even showed me what it should look like.

I have no clue why the assignment wanted me to code it like that. I just made things more confusing. :(

I would totally prefer to write it out like this:

savingsAccount.calculateMonthylyInterest();
	
savingsAccount.print();

Thanks for the help,I appreciate it!

Look at where the first error occurs - is the variable aI in scope there?

What's wrong with this pair (prototype and implementation)?

static double modifyInterestRate();

void Savings_Account::modifyInterestRate(double aI)

Why are you using .print( ) the way you do, tacking it onto another method?

Error 4 is probably related to 3.

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.