almost ready to give up!!!

Discussion in 'C++' started by David, May 18, 2004.

  1. David

    David Guest

    I am getting the an output with exponential values and negative numbers.Can
    you see what is generating that problem
    Any help will be appreciated.



    /* Create a savings accountclass. Use a dat data member to contain the
    annualInterest rate
    for each of the savers. Each member of the calss contains a private
    membr savingsBalance
    indicating the amount the saver currently has on deposit.Provide a
    calculate MonthlyInterest
    member function that calculates the monthly interest by mulyiplying the
    balance by annualInterestRate
    divided by 12; This interest should be added to savingssBalance. Provide
    a member funtion
    modifyInterestRate tahe sets the annualInteretrte to a new value. Write
    adriver proram to test
    clas savings Account. Instantiate two different savingsAccount objects,
    saver1 and saver2, with balances of
    $2000 and $3000,respectively. Set annual interest rate to 3% than calculate
    the monthly interest rate and print
    the new balances for eac of the savers.Then set the annual interset rate to
    4% and calculate the next month's
    interest and print the new balances for each of rthe savers */




    #include <iostream>

    using namespace std;


    class SavingsAccount //class definition
    {
    public: // prototypes of member functions
    SavingsAccount(double);
    double cal_monthlyInterest();
    void modifyInterestRate();
    void showBalance();

    private: //Data Member

    double savingsBalance;
    double annualInterestRate;
    };


    SavingsAccount::SavingsAccount(double balance) //constructor
    {
    savingsBalance = balance;
    }


    double SavingsAccount::cal_monthlyInterest()
    {


    SavingsAccount::savingsBalance += (annualInterestRate/12) *
    savingsBalance;
    return savingsBalance;

    }

    void SavingsAccount::modifyInterestRate()
    {

    SavingsAccount::annualInterestRate = 0.04;
    }
    void SavingsAccount:: showBalance()
    {

    cout<< "Balance is : $"<<SavingsAccount::savingsBalance<<"\n";
    }

    int main()

    {
    SavingsAccount saver1(2000);
    SavingsAccount saver2(3000);

    cout<<"\n";
    cout<<" The program calculates 2 fixed interest rates for 2 fixed
    accounts.\n";
    cout<<"\n";

    cout<<"New balances for Saver1 account @ 3% and 4% respectively are :\n";
    cout<<"\n";
    saver1.cal_monthlyInterest();
    saver1.showBalance();
    cout<<"\n";

    saver1.modifyInterestRate();
    saver1.cal_monthlyInterest();
    saver1.showBalance();
    cout<<"\n";

    cout<<"New balances for Saver2 account @ 3% and 4% respectively are :\n";
    cout<<"\n";
    saver2.cal_monthlyInterest();
    saver2.showBalance();

    saver2.modifyInterestRate();
    cout<<"\n";
    saver2.cal_monthlyInterest();
    saver2.showBalance();
    cout<<"\n";

    cout<<"\n"<<"---------------------------------------------------------------
    ----------------"<<"\n";
    return 0;

    }



    OUTPUT




    The program calculates 2 fixed interest rates for 2 fixed accounts.

    New balances for Saver1 account @ 3% and 4% respectively are :

    Balance is : $-1.54266e+064

    Balance is : $-1.5478e+064

    New balances for Saver2 account @ 3% and 4% respectively are :

    Balance is : $-2.31399e+064

    Balance is : $-2.3217e+064


    ----------------------------------------------------------------------------
    ---
    Press any key to continue
     
    David, May 18, 2004
    #1
    1. Advertising

  2. David

    Leor Zolman Guest

    On Tue, 18 May 2004 02:46:46 GMT, "David" <> wrote:
    >
    > class SavingsAccount //class definition
    > {
    > public: // prototypes of member functions
    > SavingsAccount(double);
    > double cal_monthlyInterest();
    > void modifyInterestRate();
    > void showBalance();
    >
    >private: //Data Member
    >
    > double savingsBalance;
    > double annualInterestRate;
    > };
    >
    >
    > SavingsAccount::SavingsAccount(double balance) //constructor
    > {
    > savingsBalance = balance;
    > }
    >


    In your constructor, you don't initialize annualInterestRate. It will
    contain garbage. Not healthy for floating point numbers (not that it's
    necessarily healthy for ints, either, but in the case of floating point,
    you may segfault as soon as you even try to /read/ an uninitialized
    floating point number.)

    >
    > double SavingsAccount::cal_monthlyInterest()
    > {
    >
    >
    > SavingsAccount::savingsBalance += (annualInterestRate/12) *
    >savingsBalance;


    Yet above, you read annualInterestRate's value.

    > return savingsBalance;
    >
    > }
    >
    > void SavingsAccount::modifyInterestRate()
    > {
    >
    > SavingsAccount::annualInterestRate = 0.04;
    > }


    If you actually called the function above before you sampled that interest
    rate, you'd have been OK. But, um, since you're setting it to a constant
    value, you could have just done that in the constructor. Usually a function
    like this would exist to take a /new/ rate as a parameter, and stuff it
    into the data member. But whatever works for you (in this case, it doesn't
    yet.)

    > void SavingsAccount:: showBalance()
    > {
    >
    > cout<< "Balance is : $"<<SavingsAccount::savingsBalance<<"\n";
    > }
    >
    > int main()
    >
    > {
    > SavingsAccount saver1(2000);
    > SavingsAccount saver2(3000);
    >
    > cout<<"\n";
    > cout<<" The program calculates 2 fixed interest rates for 2 fixed
    >accounts.\n";
    > cout<<"\n";
    >
    > cout<<"New balances for Saver1 account @ 3% and 4% respectively are :\n";
    > cout<<"\n";
    > saver1.cal_monthlyInterest();


    So above is where you use the interest rate before giving it a value.

    HTH,
    -leor

    > saver1.showBalance();
    > cout<<"\n";
    >
    > saver1.modifyInterestRate();
    > saver1.cal_monthlyInterest();
    > saver1.showBalance();
    > cout<<"\n";
    >
    > cout<<"New balances for Saver2 account @ 3% and 4% respectively are :\n";
    > cout<<"\n";
    > saver2.cal_monthlyInterest();
    > saver2.showBalance();
    >
    > saver2.modifyInterestRate();
    > cout<<"\n";
    > saver2.cal_monthlyInterest();
    > saver2.showBalance();
    > cout<<"\n";
    >
    >cout<<"\n"<<"---------------------------------------------------------------
    >----------------"<<"\n";
    >return 0;
    >
    > }


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, May 18, 2004
    #2
    1. Advertising

  3. David

    Leor Zolman Guest

    On Mon, 17 May 2004 20:34:14 -0700, "Dave" <> wrote:
    om/tools/stlfilt.html
    >
    >Leor,
    >
    >Could you please elaborate with regarding to segfaulting upon trying to read
    >an uninitialized floating point value? Why would this happen? I suppose
    >this implies that there are bit patterns that do not represent a valid
    >floating point number and that trying to interpret those bit patterns as
    >such will cause some sort of exception. I'm very curious about the details
    >of this, so I'd love to hear more!


    Yes, you've pretty much explained it. I probably shouldn't have used the
    word "segfault", but I've been following a thread on clc about errant
    pointer values, and I had segfault on the brain. To be more precise, using
    an uninitialized floating point value has undefined behavior because, as
    you said, some bit patterns do not represent valid floating point numbers.
    Not all 2^64 combinations are actually used (in the case of a
    typically-sized double). This is not uncommon; I've seen it happen in my
    own programs (blush).
    -leor

    >
    >Dave
    >


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, May 18, 2004
    #3
  4. David

    Dave Guest

    "Leor Zolman" <> wrote in message
    news:...
    > On Tue, 18 May 2004 02:46:46 GMT, "David" <> wrote:
    > >
    > > class SavingsAccount //class definition
    > > {
    > > public: // prototypes of member functions
    > > SavingsAccount(double);
    > > double cal_monthlyInterest();
    > > void modifyInterestRate();
    > > void showBalance();
    > >
    > >private: //Data Member
    > >
    > > double savingsBalance;
    > > double annualInterestRate;
    > > };
    > >
    > >
    > > SavingsAccount::SavingsAccount(double balance) //constructor
    > > {
    > > savingsBalance = balance;
    > > }
    > >

    >
    > In your constructor, you don't initialize annualInterestRate. It will
    > contain garbage. Not healthy for floating point numbers (not that it's
    > necessarily healthy for ints, either, but in the case of floating point,
    > you may segfault as soon as you even try to /read/ an uninitialized
    > floating point number.)
    >
    > >
    > > double SavingsAccount::cal_monthlyInterest()
    > > {
    > >
    > >
    > > SavingsAccount::savingsBalance += (annualInterestRate/12) *
    > >savingsBalance;

    >
    > Yet above, you read annualInterestRate's value.
    >
    > > return savingsBalance;
    > >
    > > }
    > >
    > > void SavingsAccount::modifyInterestRate()
    > > {
    > >
    > > SavingsAccount::annualInterestRate = 0.04;
    > > }

    >
    > If you actually called the function above before you sampled that interest
    > rate, you'd have been OK. But, um, since you're setting it to a constant
    > value, you could have just done that in the constructor. Usually a

    function
    > like this would exist to take a /new/ rate as a parameter, and stuff it
    > into the data member. But whatever works for you (in this case, it doesn't
    > yet.)
    >
    > > void SavingsAccount:: showBalance()
    > > {
    > >
    > > cout<< "Balance is : $"<<SavingsAccount::savingsBalance<<"\n";
    > > }
    > >
    > > int main()
    > >
    > > {
    > > SavingsAccount saver1(2000);
    > > SavingsAccount saver2(3000);
    > >
    > > cout<<"\n";
    > > cout<<" The program calculates 2 fixed interest rates for 2 fixed
    > >accounts.\n";
    > > cout<<"\n";
    > >
    > > cout<<"New balances for Saver1 account @ 3% and 4% respectively are

    :\n";
    > > cout<<"\n";
    > > saver1.cal_monthlyInterest();

    >
    > So above is where you use the interest rate before giving it a value.
    >
    > HTH,
    > -leor
    >
    > > saver1.showBalance();
    > > cout<<"\n";
    > >
    > > saver1.modifyInterestRate();
    > > saver1.cal_monthlyInterest();
    > > saver1.showBalance();
    > > cout<<"\n";
    > >
    > > cout<<"New balances for Saver2 account @ 3% and 4% respectively are

    :\n";
    > > cout<<"\n";
    > > saver2.cal_monthlyInterest();
    > > saver2.showBalance();
    > >
    > > saver2.modifyInterestRate();
    > > cout<<"\n";
    > > saver2.cal_monthlyInterest();
    > > saver2.showBalance();
    > > cout<<"\n";
    > >

    >
    >cout<<"\n"<<"--------------------------------------------------------------

    -
    > >----------------"<<"\n";
    > >return 0;
    > >
    > > }

    >
    > --
    > Leor Zolman --- BD Software --- www.bdsoft.com
    > On-Site Training in C/C++, Java, Perl and Unix
    > C++ users: download BD Software's free STL Error Message Decryptor at:
    > www.bdsoft.com/tools/stlfilt.html


    Leor,

    Could you please elaborate with regarding to segfaulting upon trying to read
    an uninitialized floating point value? Why would this happen? I suppose
    this implies that there are bit patterns that do not represent a valid
    floating point number and that trying to interpret those bit patterns as
    such will cause some sort of exception. I'm very curious about the details
    of this, so I'd love to hear more!

    Dave
     
    Dave, May 18, 2004
    #4
  5. David

    red floyd Guest

    "David" <> wrote in message news:<qOeqc.488068$Pk3.224835@pd7tw1no>...
    > I am getting the an output with exponential values and negative numbers.Can
    > you see what is generating that problem
    > Any help will be appreciated.
    >
    > [code redacted -- thank you for posting your work]


    You don't set the initial interest rate anywhere, so you're
    multiplying by undefined. After you reset to 0.04, the balance is
    undefined (because you were adding undefined to the balance), hence
    the garbage.

    Also, you don't need to prefix the member variables with
    SavingsAccount::
     
    red floyd, May 18, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave E
    Replies:
    1
    Views:
    2,031
    Mike Labosh
    Dec 22, 2005
  2. Joenco
    Replies:
    2
    Views:
    757
    Joenco
    Jul 3, 2003
  3. grocery_stocker
    Replies:
    10
    Views:
    661
    Keith Thompson
    May 25, 2005
  4. Zed Shaw
    Replies:
    1
    Views:
    124
    Dr Nic
    Aug 8, 2006
  5. Avdi Grimm

    Ruby is Almost Ready!

    Avdi Grimm, Apr 11, 2008, in forum: Ruby
    Replies:
    3
    Views:
    108
    Marc Heiler
    Apr 11, 2008
Loading...

Share This Page