GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-Mar-2005, 16:27
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

savings account problem


I am trying to write a savings account program, and i did what i thought was the right thing.
suprise suprise, when the program runs i get weird results, as if the class objects are not being initialised by the constructor, since it is giving me weirds "garbage" numbers?
here are the class declaration and implementation files

CPP / C++ / C Code:
#ifndef SAVACC_H
#define SAVACC_H
#include <stdlib.h>
#include <ctime>
#include <iostream>

class SavingsAccount
{
public:
	SavingsAccount( double savingsBalance, double annualInterestRate );
	//constructor for the class
	//initializes the account balance to dollars
	//and the interest rate to rate percent.
	SavingsAccount();
	//initializes the account balance to zero
	//and the interest rate to zero.
	double get_balance();
	//returns the current account balance
	 double get_rate();
	//returns the current rate
	double calculateMonthlyInterest();
	//calculates monthly interest by multiplying
	// savingsBalance by the annualInterestRate divided by 12.
	 double modifyInterestRate();
	void printtestresults(SavingsAccount);
 
private:
	double savingsBalance;
	double annualInterestRate;//expressed as a percentage
};

#endif SAVACC_H

CPP / C++ / C Code:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;
#include <stdlib.h>
#include <iomanip>

using std::setw;

#include <cstdlib>

#include <new>

#include "savingsaccount.h"

SavingsAccount::SavingsAccount( double savingsBalance, double annualInterestRate )
{
	savingsBalance = savingsBalance;
	annualInterestRate = annualInterestRate;
}

SavingsAccount::SavingsAccount()
{
	savingsBalance = 0.0;
	annualInterestRate = 0.0;
}

double SavingsAccount::get_balance()
{
	return savingsBalance;
}

double SavingsAccount::get_rate()
{
	return annualInterestRate;
}
double SavingsAccount::calculateMonthlyInterest()
{
   return ((savingsBalance * annualInterestRate)/12);
	
}
double SavingsAccount::modifyInterestRate()
{
	double rate = 0;
	rate = annualInterestRate;
	rate = rate +1;
	return rate;
}
void SavingsAccount::printtestresults(SavingsAccount joe)
{
    cout <<"balance = "<<savingsBalance<<endl;
	cout <<"annual Interest Rate : "<<annualInterestRate<<endl;
    cout <<"the monthly interest rate for this customer is :"<<endl;
	cout <<joe.calculateMonthlyInterest()<<endl;
}

CPP / C++ / C Code:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;
#include <stdlib.h>
#include <ctime>

#include "savingsaccount.h"


int main()
{
	SavingsAccount saver1(2000.00,3.0);

	cout <<"saver1 is initialized as follows:\n";
	saver1.printtestresults(saver1);
	cout <<"saver1's new balance is : "<<(2000 + saver1.calculateMonthlyInterest());
	cout <<"saver1's interest for next month is : "<<saver1.modifyInterestRate<<endl;
	saver1.printtestresults(saver1);
	
	return 0;
}
Last edited by LuciWiz : 31-Mar-2005 at 23:20. Reason: Correcting formating
  #2  
Old 31-Mar-2005, 16:51
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,623
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by ap6118
I am trying to write a savings account program

**EDIT*** Note changes here


What compiler are you using? Borland compilers give warnings:
Quote:
Warning W8004 'annualInterestRate' is assigned a value that is never used in function SavingsAccount::SavingsAccount(double,double)
Warning W8004 'savingsBalance' is assigned a value that is never used in function SavingsAccount::SavingsAccount(double,double)

(I thought that other compilers would also give warnings, but apparently not)

Since your observation of program output makes you think the constructor is not initializing things properly, Look at your constructor:
CPP / C++ / C Code:
SavingsAccount::SavingsAccount( double savingsBalance, double annualInterestRate )
{
  savingsBalance = savingsBalance;
  annualInterestRate = annualInterestRate;
}

This does nothing! (The argument savingsBalance is used as source and target of the assignment. Same for annualInterestRate.)

You could do this:

CPP / C++ / C Code:
SavingsAccount::SavingsAccount( double savingsBalance, double annualInterestRate )
{
  SavingsAccount::savingsBalance = savingsBalance;
  SavingsAccount::annualInterestRate = annualInterestRate;
}

or something like this:

CPP / C++ / C Code:
SavingsAccount::SavingsAccount( double sBalance, double aInterestRate )
{
  savingsBalance = sBalance;
  annualInterestRate = aInterestRate;
}

Either will work. See the difference between these and what your constructor did?

Regards,

Dave



Regards,

Dave
Last edited by davekw7x : 31-Mar-2005 at 17:36. Reason: First was incomplete
  #3  
Old 31-Mar-2005, 17:18
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

sorry i should have thought of that


i am using visual studio 6
there are no compiler errors or warnings.
just the output of the program are garbage numbers.

exemple:

saver1 is initialised as followes:
balance = -9.25596+061 dollars
blah blah blh
  #4  
Old 31-Mar-2005, 17:40
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,623
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by ap6118
i am using visual studio 6
there are no compiler errors or warnings.
just the output of the program are garbage numbers.

exemple:

saver1 is initialised as followes:
balance = -9.25596+061 dollars
blah blah blh

I edited my previous post to show some possibilities. I'm sorry that I didn't say these things at first, but I somehow thought there might have been some useful compiler messages. Sorry.

Try reading my edited post and see if it helps (and/or see if it makes sense). You definitely need to change your constructor

Regards,

Dave
  #5  
Old 31-Mar-2005, 20:05
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

again,i'm in your debt.


thank you it worked fine.i saw the problem with the constructor.it was basicaly doing nothing.but it's allright now.
and another thing.i tried something else,i tried to declare annualInterestRate as a static and have get_rate function to be a static member function, but when i run the program i get this message from the compiler

savingsaccount.obj : error LNK2001: unresolved external symbol "private: static double SavingsAccount::annualInterestRate" (?annualInterestRate@SavingsAccount@@0NA)
Debug/savingsaccount.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

the lines i am referring to in the class declaration are:
CPP / C++ / C Code:
static double get_rate();
	//returns the current rate

static double annualInterestRate;//expressed as a percentage



ps: does anybody know of a better compiler than visual studio,it's eally getting on my nerves!!
  #6  
Old 01-Apr-2005, 09:37
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,623
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by ap6118
thank you it worked fine.i saw the problem with the constructor.it was basicaly doing nothing.but it's allright now.
and another thing.i tried something else,i tried to declare annualInterestRate as a static and have get_rate function to be a static member function, but when i run the program i get this message from the compiler

savingsaccount.obj : error LNK2001: unresolved external symbol "private: static double SavingsAccount::annualInterestRate" (?annualInterestRate@SavingsAccount@@0NA)
Debug/savingsaccount.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

I don't see the point of trying to make a member function static.

As for the variable:

in SavingsAccount.h you have

CPP / C++ / C Code:
private:
  double savingsBalance;
  static double annualInterestRate;//expressed as a percentage

Now, each instance of this class has its own savingsBalance, but there is only one annualInterestRate for all instances. This declaration does not actually allocate any memory for the variable. So in your SavingsAccount.cpp you have a line like this:

CPP / C++ / C Code:
double SavingsAccount::annualInterestRate;

Now, you realize that if you change annualInterestRate for a particular object (in an initializer or any other function), it will change for all instances, right?

Regards,

Dave
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem modifying an array of char in a function ronin C Programming Language 10 28-Mar-2005 18:15
windows user account dexterwolf CPP / C++ Forum 1 04-Feb-2005 11:17
Tee chart problem Arun CPP / C++ Forum 0 01-Sep-2004 23:23
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53
RE: A simple update and } problem Dagma20 MySQL / PHP Forum 3 27-Mar-2004 16:37

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:21.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.