SavingsAccount Class I\'m having a problem with an assignment in C++ and need a
ID: 3762503 • Letter: S
Question
SavingsAccount Class
I'm having a problem with an assignment in C++ and need a little help... I got the first part of the assignment but am having problems with adding the interest rate to the array int the second part of the assignment... any help would be appreciated!!! The code displays the contents of the array but not the interest rate added to each month...here's the code I have so far
#include<iostream>
#include<iomanip>
#include <string>
using namespace std;
class SavingsAccount
{
private:
static double annual_Interest_Rate;
double savingsBalance;
public:
SavingsAccount(int sb=0)
{
savingsBalance = sb;
}
void calculateMonthlyInterest()
{
savingsBalance = savingsBalance + (savingsBalance*annual_Interest_Rate/12);
}
static void modifyInterestRate(double air)
{
annual_Interest_Rate = air;
}
double get_balance()
{
return savingsBalance;
}
};
double SavingsAccount::annual_Interest_Rate = 0.03;
int main()
{
const int NUM_MONTHS = 12;
string name[NUM_MONTHS] = {"January", "Febuary", "March","April", "May", "June", "July", "August", "September","October", "November" , "December"};
SavingsAccount savingAccounts[5]={2000,4000,8000,16000,32000};
SavingsAccount Monthly ;
// I think that here is the place that I'm having the problem
int i=0;
for(int i=0; i<5; i++)
{for (int month = 0; month< NUM_MONTHS; month++)
{cout << "The Balance for " << name[month] << " is " << savingAccounts[i].get_balance() << endl;
};
savingAccounts[i].calculateMonthlyInterest();
cout << " " << endl;
}
//SavingsAccount::modifyInterestRate(0.04);
//savingAccounts[i].calculateMonthlyInterest();
system("pause");
return 0;
}
Explanation / Answer
#include<iostream>
#include<iomanip>
#include <string>
using namespace std;
class SavingsAccount
{
private:
static double annual_Interest_Rate;
double savingsBalance;
public:
SavingsAccount(int sb=0)
{
savingsBalance = sb;
}
void calculateMonthlyInterest()
{
savingsBalance = savingsBalance + (savingsBalance*annual_Interest_Rate/12.0);
}
static void modifyInterestRate(double air)
{
annual_Interest_Rate = air;
}
double get_balance()
{
return savingsBalance;
}
};
double SavingsAccount::annual_Interest_Rate = 0.03;
int main()
{
const int NUM_MONTHS = 12;
string name[NUM_MONTHS] = {"January", "Febuary", "March","April", "May", "June", "July", "August", "September","October", "November" , "December"};
SavingsAccount savingAccounts[5]={2000,4000,8000,16000,32000};
SavingsAccount Monthly ;
// I think that here is the place that I'm having the problem
int i=0;
for(int i=0; i<5; i++)
{
for (int month = 0; month< NUM_MONTHS; month++) {
savingAccounts[i].calculateMonthlyInterest();
cout << "The Balance for " << name[month] << " is " << savingAccounts[i].get_balance() << endl;
};
cout << " " << endl;
}
//SavingsAccount::modifyInterestRate(0.04);
//savingAccounts[i].calculateMonthlyInterest();
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.