Hi all, this is my second classes involving c and c++ and right now i'm pretty stuck. The assignment is as follows:
Create a SavingAccount class.
Use a static data member annualInterestRate to store the annual interest rate for each of the savers.
Each member of the class contains a private data member savingsBalance indicating the amount the saver currently has on deposit.
Provide member function calculateMontlyInterest that calculates the monthly interest by multiplying the balance by annualInterestRate divided by 12; this interest should be added to savingsBalance.
Provide a static member function member function modifyInterestRate that sets the static annualInterestRate to a new value.
Write a driver program to test class SavingsAccount.
Instantiate two different objects of class SavingsAccount, saver1 & saver2, with balances of $2000.00 and $3000.00, respectively.
set the annualInterestRate to 3%.
Then calculate the monthly interest and print the new balances for each of the savers.
Then set annualInterestRate to 4%, calculate the next month's interest and print the new balances for each of the savers.
I have done as much as you see the following.
I think I know something about statics and data member; but, not perfectly. I can't eliminate the errors anyhow. Please help me and guide me how I should fix this program.
Code:
#include<iostream.h>
#include<iomanip.h>
class savingsAccount
{
private:
double savingsBalance; //data member
static double annualInterestRate; //data member
public:
savingsAccount() {savingsBalance = 0.0; };
savingsAccount(double balance) {savingsBalance = balance; };
static void modifyInterestRate(double rates);
void get_balance(double balance){this -> savingsBalance = balance;};
double calculateMonthlyInterest(); // add interest every month
void printBalance(); // Display the saving account
};
double savingsAccount::annualInterestRate = 0.030;
double savingsAccount::calculateMonthlyInterest(){
double interest = savingsBalance * annualInterestRate/12;
savingsBalance += interest;
return savingsBalance; // add to savingsBalance
}
void savingsAccount::modifyInterestRate(double i){
if(i < 0.0 || i >= 1.0) throw out_of_range("invalid rate");
annualInterestRate = i;
}
void savingsAccount::printBalance(){
cout << setiosflags(ios::fixed | ios::showpoint)
<< "$" << setprecision(2) << savingsBalance
<< resetiosflags (ios::fixed | ios::showpoint) << endl;
}
int main()
{
/* The Scenario
Each saver has $3000 and $4000 deposit in each account.
Print the amount of money of their saving account for one year,
so this shows how much amount of money is rising by 3% interest.
Then, rise the interest upto 4%, and print how much money both of saver
has in month of 13th.
*/
savingsAccount saver1(2000);
savingsAccount saver2(3000);
for (int n = 0; n < 12; n++){
saver1.calculateMonthlyRate();
cout << "1:" << endl;
saver1.printBalance();
saver2.calculateMonthlyRate();
cout << "2:" << endl;
saver2.printBalance();
}
saver1.modifyInterestRate(0.040);
saver1.calculateMonthlyRate();
cout << "1:" << endl;
saver1.printBalance();
saver2.calculateMonthlyRate();
cout << "2:" << endl;
saver2.printBalance();
return 0;
}
/*
---------------The following is the error that I get------------------
if(i < 0.0 || i >= 1.0) throw std::out_of_range("invalid rate");
^
prg10.8ontheway.cpp(29) : Error: Compile all files with -Ae to support exception handling
annualInterestRate = i;
^
prg10.8ontheway.cpp(30) : Error: ';' expected following declaration of struct member
<< "$" << setprecision(2) << savingsBalance
^
prg10.8ontheway.cpp(35) : Error: reference must refer to same type or be const
Had: smanip_long
and: smanip_long&
<< resetiosflags (ios::fixed | ios::showpoint) << endl;
^
prg10.8ontheway.cpp(36) : Error: reference must refer to same type or be const
Had: smanip_long
and: smanip_long&
saver1.calculateMonthlyRate();
^
prg10.8ontheway.cpp(52) : Error: 'calculateMonthlyRate' is not a member of 'savingsAccount'
Fatal error: too many errors
--- errorlevel 1
*/