Jun 7, 2013 at 6:43pm UTC
I am making this program for a class and it works fine except for the daily interest rate for the checking account is off. The daily interest for the savings computes perfectly. The monthly interest rate for savings is 6% and for checking 3%.
Account.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <iostream>
#include <string>
using namespace std;
const int DAYS_PER_MONTH = 30;
class Account
{
public :
Account();
Account(double b);
void deposit(double amount);
void withdraw(double amount);
double get_balance() const ;
private :
double balance;
};
#endif
Checking.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#ifndef CHECKING_H
#define CHECKING_H
#include "Account.h"
class Checking: public Account
{
public :
Checking();
Checking(double b);
void daily_interest();
private :
};
#endif
Savings.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#ifndef SAVIGS_H
#define SAVINGS_H
#include "Account.h"
class Savings : public Account
{
public :
Savings();
Savings(double b);
void daily_interest();
};
#endif
Account.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include "Account.h"
Account::Account(){
balance=0;
}
Account::Account(double b) {
balance=b;
}
void Account::deposit(double b) {
balance += b;
}
void Account::withdraw(double b) {
balance -= b;
}
double Account::get_balance() const {
return balance;
}
Checking.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "Checking.h"
Checking::Checking() {
}
Checking::Checking(double b) {
this ->deposit(b);
}
void Checking::daily_interest() {
double d = get_balance();
d = d * (.03/30);
this ->deposit(d);
}
Savings.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "Savings.h"
Savings::Savings(){
}
Savings::Savings(double b) {
this ->deposit(b);
}
void Savings::daily_interest() {
double d = get_balance();
d = d * (.06/30);
this ->deposit(d);
}
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <iostream>
#include "Checking.h"
#include "Savings.h"
using namespace std;
int main()
{
Checking c = Checking(1000.0);
Savings s = Savings(1000.0);
for (int i = 1; i <= DAYS_PER_MONTH; i++)
{
c.deposit(i * 5);
c.withdraw(i * 2);
s.deposit(i * 5);
s.withdraw(i * 2);
c.daily_interest();
s.daily_interest();
if (i % 10 == 0)
{
cout << "day " << i << "\n" ;
cout << "Checking balance: " << c.get_balance() << "\n" ;
cout << "Savings balance: " << s.get_balance() << "\n" ;
}
}
return 0;
}
THE EXPECTED OUTPUT:
day 10
Checking balance: 1165.66
Savings balance: 1186.51
day 20
Checking balance: 1634.64
Savings balance: 1680.1
day 30
Checking balance: 2409.99
Savings balance: 2486.97
MY OUTPUT:
day 10
Checking balance: 1175.71
Savings balance: 1186.51
day 20
Checking balance: 1654.83
Savings balance: 1680.1
day 30
Checking balance: 2440.43
Savings balance: 2486.97
Last edited on Jun 7, 2013 at 6:46pm UTC
Jun 7, 2013 at 8:47pm UTC
Last edited on Jun 7, 2013 at 9:00pm UTC