almost ready to give up!!!

D

David

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
 
L

Leor Zolman

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
 
L

Leor Zolman

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
 
D

Dave

Leor Zolman said:
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.)


Yet above, you read annualInterestRate's value.


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.)


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

HTH,
-leor

-

--
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
 
R

red floyd

David said:
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::
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top