Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > almost ready to give up!!!

Reply
Thread Tools

almost ready to give up!!!

 
 
David
Guest
Posts: n/a
 
      05-18-2004
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


 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      05-18-2004
On Tue, 18 May 2004 02:46:46 GMT, "David" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      05-18-2004
On Mon, 17 May 2004 20:34:14 -0700, "Dave" <(E-Mail Removed)> 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
 
Reply With Quote
 
Dave
Guest
Posts: n/a
 
      05-18-2004

"Leor Zolman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Tue, 18 May 2004 02:46:46 GMT, "David" <(E-Mail Removed)> 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


 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      05-18-2004
"David" <(E-Mail Removed)> 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::
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Vista SP1 almost ready Ian Front Page News 0 01-14-2008 03:06 PM
[ANN] 0.3.13.4 Pre-Release -- Almost Ready Zed Shaw Ruby 1 08-08-2006 10:57 AM
Access is denied: 'Interop.ADODB' error - almost ready to jump off the balcony! Dave E ASP .Net 1 12-22-2005 02:42 PM
New Computer Almost Ready For Big Switch On :) John Computer Information 1 12-21-2003 07:54 PM
Site almost ready - please comment if you have some time Joenco HTML 2 07-03-2003 02:20 AM



Advertisments