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

#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

#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

#ifndef SAVIGS_H
#define SAVINGS_H

#include "Account.h"

class Savings : public Account
{
public:
  Savings();
  Savings(double b);
  void daily_interest();
};

#endif 

Account.cpp

#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

#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

#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

#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

Recommended Answers

All 11 Replies

where did you get those expected numbers?

You don't need all three classes, just Account class would work. But if the intent is to learn polymorphism then just ignore this suggestion.

#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <iostream>
#include <string>

using namespace std;

const int DAYS_PER_MONTH = 30;

class Account
{
public:
   Account(double interest_rate);
   Account(double b,double interest_rate);
   void deposit(double amount);
   void withdraw(double amount);
   double get_balance() const;
   void daily_interest();
private:
   double balance;
   double daily_interest_rate;
};

#endif 

the expected numbers came from my professor

I think your professor's numbers are wrong. Small rounding errors could account for some of the difference but not nearly that much.

Do the math manually with pencil & paper + calculator to see if you get the same results. My guess is your program produces the correct numbers.

I get the same from your code. You could probably confirm this better by using AD's suggestion of one account class declared twice one as Checking and one as Savings.

Try using DAYS_PER_MONTH = 28 and replace the magic number 30 with that defined value everywhere in your code.

In your main program.
Checking c = Checking(1000.0);
Savings s = Savings(1000.0);
should really be.
Checking checking(1000.0);
Savings savings(1000.0);
then change all those 'c' and 's' to checking or savings.

Just because the question asks for the results up to 30 days, does not mean a month is 30 days...

Thatr is standard accounting practice when estimating income/expenses, at least hear in the US. It just simplifies the math. Of course the exact number of days is used when when banks "give" us interest on our accounts.

I agree with your suggestions, but they don't change the results.

I have to agree. The professor's results just don't seem to be right. I tweaked a few things to see if some obvious error could reproduce his expected output. That didn't work at all.

There is the possibility that the OP has overlooked some important instruction(s). He should post the exact text of the assignment.

I hope you didn't miss a part of the question that mentions a $30 / month checking account fee?
Just a crazy thought because the difference is roughly a dollar per day.
When I changed the code to include a fee of $1
void Checking::daily_interest() { deposit((get_balance() * .03 / 30) - 1); }
The professor's answers popped out.

commented: Looks like I need to work on my reading skills, thank you everyone for your posts +0
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.