RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 1630 | Replies: 6
Reply
Join Date: Jun 2005
Location: California
Posts: 92
Reputation: djbsabkcb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
djbsabkcb's Avatar
djbsabkcb djbsabkcb is offline Offline
Junior Poster in Training

Help C++ program using classes with account balances

  #1  
Jul 16th, 2005
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;
        void add_interest(double rate);

        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;
}

void Account::add_interest(double rate)
{
        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)
        {
                my_account.add_interest(monthly_rate);
                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;
}
<< moderator edit: added code tags: [code][/code] >>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2005
Posts: 464
Reputation: winbatch is on a distinguished road 
Rep Power: 4
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: C++ program using classes with account balances

  #2  
Jul 17th, 2005
Show us the values you are entering and your output..
Reply With Quote  
Join Date: Jun 2005
Location: California
Posts: 92
Reputation: djbsabkcb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
djbsabkcb's Avatar
djbsabkcb djbsabkcb is offline Offline
Junior Poster in Training

Re: C++ program using classes with account balances

  #3  
Jul 17th, 2005
Originally Posted by winbatch
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]$
Reply With Quote  
Join Date: Jun 2005
Location: California
Posts: 92
Reputation: djbsabkcb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
djbsabkcb's Avatar
djbsabkcb djbsabkcb is offline Offline
Junior Poster in Training

Re: C++ program using classes with account balances

  #4  
Jul 17th, 2005
Originally Posted by djbsabkcb
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
Reply With Quote  
Join Date: Jun 2005
Location: California
Posts: 92
Reputation: djbsabkcb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
djbsabkcb's Avatar
djbsabkcb djbsabkcb is offline Offline
Junior Poster in Training

Solution Re: C++ program using classes with account balances

  #5  
Jul 17th, 2005
figured it out. the calculation for the add_interest member function was wrong. It should have been


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

thanks.
Reply With Quote  
Join Date: Jun 2005
Location: Cambridge, MA
Posts: 1,330
Reputation: Rashakil Fol has a spectacular aura about Rashakil Fol has a spectacular aura about 
Rep Power: 7
Solved Threads: 44
Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Salamander Man

Re: C++ program using classes with account balances

  #6  
Jul 17th, 2005
You're dividing your interest rate by 100.0 twice.
Reply With Quote  
Join Date: Jun 2005
Location: California
Posts: 92
Reputation: djbsabkcb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
djbsabkcb's Avatar
djbsabkcb djbsabkcb is offline Offline
Junior Poster in Training

Solution Re: C++ program using classes with account balances

  #7  
Jul 17th, 2005
Originally Posted by winbatch
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 9:42 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC