almost ready to give up!!!

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 */

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;

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();

Advertisements

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
>

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!

"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::

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!