Below is my code to calculate interest and balances and how many months to get there. However, my months calculations are way off by the thousands. Any idea?

``````#include<iostream>
#include<string>
#include<cmath>

using namespace std;

class Account
{
public:
Account();
Account (double bal);
void deposit(double amount);
void withdraw(double amount);
double get_balance() const;

private:
double balance;
};

Account::Account()
{
}
Account::Account(double bal)
{
balance = bal;
}

void Account::deposit(double amount)
{
balance +=amount;
}

void Account::withdraw (double amount)
{
if ( balance >= amount)
balance -= amount;
else
balance -= 5.00;  // Charge a \$5.00 penalty fee if attempting to withdraw more than balance.
}

double Account::get_balance()const
{
return balance;
}

{
balance *= ( 1 + (rate/100.0));
}

int main()
{

cout << " Enter initial investment: ";
double initial_investment;
cin >> initial_investment;

cout << " Enter annual interest rate in percentage: ";
double rate_in_percentage;
cin >> rate_in_percentage;

double monthly_rate = rate_in_percentage/100.0/12.0;
double final_amount = 2 * initial_investment;

Account my_account(initial_investment);

int months = 1;

//
// one iteration of the loop corresponds to one month
//

while ( my_account.get_balance()<final_amount)
{
months++;
}

cout << " It took " << months << " months for \$ " << initial_investment <<
" to double at " << rate_in_percentage << "% annual interest. \n";

cout << " Account balance: " << my_account.get_balance() << "\n";

return 0;
}
``````

Show us the values you are entering and your output..

Show us the values you are entering and your output..

Below is the values and the output:

[hs001@cs hs001]\$ g++ p6-5.cpp
[hs001@cs hs001]\$ a.out
Enter initial investment: 1000
Enter annual interest rate in percentage: 5
It took 16637 months for \$ 1000 to double at 5% annual interest.
Account balance: 2000.01
[hs001@cs hs001]\$

Below is the values and the output:

[hs001@cs hs001]\$ g++ p6-5.cpp
[hs001@cs hs001]\$ a.out
Enter initial investment: 1000
Enter annual interest rate in percentage: 5
It took 16637 months for \$ 1000 to double at 5% annual interest.
Account balance: 2000.01
[hs001@cs hs001]\$

Now here is what the correct output should be. the professor supplied the main program but I had to create the class to work with his main program. I am not getting the correct output yet.

Enter initial investment: 1000
Enter annual interest rate in percentage: 5
It took 168 months for \$1000 to double at 5% annual interest.
Account balance: 2002.48

figured it out. the calculation for the add_interest member function was wrong. It should have been

balance*= (1 + (12*(rate/12.0)));

thanks.

You're dividing your interest rate by 100.0 twice.

Show us the values you are entering and your output..

figured it out. the calculation for the add_interest member function was wrong. It should have been

balance*= (1 + (12*(rate/12.0)));

thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.