473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

I can't believe I'm having this problem...PLEASE HELP!

SB
Ok, very simple problem. I'm trying to update a value by calling a function
using pass by reference, but it does not update the value. In short, the
value I'm trying to update is balance, which is a private member of the
class Account. I have a public function called getBalance(). I have another
public function called deposit, which I pass the balance (by calling
getBalance() using pass by reference) and a second value for the amount of
deposit. Inside the deposit() function if I print the value of the balance
plus the amount to be deposited, it prints the right value, i.e. current
balance plus deposit amount. However, it's not updating the balance member
of the Account class because when I call the getBalance(), it still shows
the original value before the deposit. I know I'm missing something very
simple here, but I just can't see it. Also, I'm getting the following
warning regarding the function calls using pass by reference, but I don't
understand it..

report4_q3.cpp: In function `int main()':
report4_q3.cpp:20: warning: initialization of non-const reference `double &'
from rvalue `double'
report4_q3a.cpp:21: warning: in passing argument 1 of
`Account::deposit(double &, double)'
report4_q3.cpp:27: warning: initialization of non-const reference `double &'
from rvalue `double'
report4_q3a.cpp:29: warning: in passing argument 1 of
`Account::withdrawal(double &, double)'
Here's the .h file....(report4_q3.h)
using namespace std;

class Account
{
private:
double balance;

public:
Account(); // default constructor
double getBalance();
void setBalance(double);
void deposit(double&, double);
void withdrawal(double&, double);
~Account(); // destructor
};

Here's the implementation file...(report4_q3a.cpp)
#include <iostream>
#include <string>

#include "report4_q3.h"

using namespace std;

// default constructor
Account::Account()
{
// give a starting balance of $500
balance = 500.00;
}

double Account::getBalance()
{
return balance;
}

void Account::deposit(double& bal, double deposit)
{
cout<<endl<<"making a deposit!"<<endl;
bal += deposit;
cout<<"after transaction balance is: $"<<bal<<endl; //this of course shows
the correct balance
}

void Account::withdrawal(double& bal, double withdrawal)
{
cout<<"making a withdrawal!"<<endl;
bal -= withdrawal;
cout<<"Your new balance is $"<<getBalance()<<endl;
}

// destructor
Account::~Account()
{
// do nothing
}

Here's the file with main...(report4_q3.cpp)
#include "report4_q3a.cpp"

using namespace std;

int main()
{
Account a;
int inputVal = 0;
double amount = 0.0;

cout<<"Your balance is: $"<<a.getBalance()<<endl;
cout<<endl<<"Press 1 to make a deposit, Press 2 to make a withdrawal, 3 for
account balance and 4 to quit."<<endl;
cin>>inputVal;

switch(inputVal)
{
case 1: {
cout<<"Enter the amount to deposit: ";
cin>>amount;
a.deposit(a.getBalance(), amount);
cout<<"Your new balance is $"<<a.getBalance()<<endl; // right here it
shows the original balance which is incorrect
}
break;
case 2: {
cout<<"Enter the amount to withdrawal: ";
cin>>amount;
a.withdrawal(a.getBalance(), amount);
}
break;
case 3: cout<<"Your balance is: $"<<a.getBalance()<<endl;
break;
case 4: return 0;
break;
default: cout<<"You have entered an invalid value - try again."<<endl;
}

return 0;
}

Any help is GREATLY appreciated!
Thanks!
SB
Jul 22 '05 #1
9 1711
SB wrote:
Ok, very simple problem. I'm trying to update a value by calling a function
using pass by reference, but it does not update the value. In short, the
value I'm trying to update is balance, which is a private member of the
class Account. I have a public function called getBalance(). I have another
public function called deposit, which I pass the balance (by calling
getBalance() using pass by reference) and a second value for the amount of
deposit. Inside the deposit() function if I print the value of the balance
plus the amount to be deposited, it prints the right value, i.e. current
balance plus deposit amount. However, it's not updating the balance member
of the Account class because when I call the getBalance(), it still shows
the original value before the deposit. I know I'm missing something very
simple here, but I just can't see it. Also, I'm getting the following
warning regarding the function calls using pass by reference, but I don't
understand it..


You are returning a double and then passing as a non-const
reference, you can't do that in C++. Besides the expression
bal -= withdrawal for example won't actually do anything
because it affects the temporary.

Jul 22 '05 #2
On Sun, 22 Feb 2004 10:42:06 -0500 in comp.lang.c++, "SB"
<vo******@hotmail.com> was alleged to have written:
I have a public function called getBalance(). I have another
public function called deposit, which I pass the balance (by calling
getBalance() using pass by reference)


Don't attempt to pass the balance as an argument to deposit().
Have deposit() update the member variable directly.

What you are doing is like: the ATM machine prints out a piece of paper
with your balance on it (getBalance) and to make a deposit you cross out
the amount on the paper and write in a new amount. The next time the
ATM prints a receipt, mysteriously the old amount is still there. This
is what all the messages about temporary (copy of the balance) bound to
non-const reference are telling you.

Jul 22 '05 #3
SB
Thanks, I'm still a little unsure why my original way didn't work...I'll
have to read your explaination again to fully understand. However, I did
what one of you suggested and just wrote a setBalance function which
directly updated the balance member directly, which worked perfectly.

Thanks!!!

"SB" <vo******@hotmail.com> wrote in message
news:g54_b.14306$Dc2.5980@lakeread01...
Ok, very simple problem. I'm trying to update a value by calling a function using pass by reference, but it does not update the value. In short, the
value I'm trying to update is balance, which is a private member of the
class Account. I have a public function called getBalance(). I have another public function called deposit, which I pass the balance (by calling
getBalance() using pass by reference) and a second value for the amount of
deposit. Inside the deposit() function if I print the value of the balance
plus the amount to be deposited, it prints the right value, i.e. current
balance plus deposit amount. However, it's not updating the balance member
of the Account class because when I call the getBalance(), it still shows
the original value before the deposit. I know I'm missing something very
simple here, but I just can't see it. Also, I'm getting the following
warning regarding the function calls using pass by reference, but I don't
understand it..

report4_q3.cpp: In function `int main()':
report4_q3.cpp:20: warning: initialization of non-const reference `double &' from rvalue `double'
report4_q3a.cpp:21: warning: in passing argument 1 of
`Account::deposit(double &, double)'
report4_q3.cpp:27: warning: initialization of non-const reference `double &' from rvalue `double'
report4_q3a.cpp:29: warning: in passing argument 1 of
`Account::withdrawal(double &, double)'
Here's the .h file....(report4_q3.h)
using namespace std;

class Account
{
private:
double balance;

public:
Account(); // default constructor
double getBalance();
void setBalance(double);
void deposit(double&, double);
void withdrawal(double&, double);
~Account(); // destructor
};

Here's the implementation file...(report4_q3a.cpp)
#include <iostream>
#include <string>

#include "report4_q3.h"

using namespace std;

// default constructor
Account::Account()
{
// give a starting balance of $500
balance = 500.00;
}

double Account::getBalance()
{
return balance;
}

void Account::deposit(double& bal, double deposit)
{
cout<<endl<<"making a deposit!"<<endl;
bal += deposit;
cout<<"after transaction balance is: $"<<bal<<endl; //this of course shows the correct balance
}

void Account::withdrawal(double& bal, double withdrawal)
{
cout<<"making a withdrawal!"<<endl;
bal -= withdrawal;
cout<<"Your new balance is $"<<getBalance()<<endl;
}

// destructor
Account::~Account()
{
// do nothing
}

Here's the file with main...(report4_q3.cpp)
#include "report4_q3a.cpp"

using namespace std;

int main()
{
Account a;
int inputVal = 0;
double amount = 0.0;

cout<<"Your balance is: $"<<a.getBalance()<<endl;
cout<<endl<<"Press 1 to make a deposit, Press 2 to make a withdrawal, 3 for account balance and 4 to quit."<<endl;
cin>>inputVal;

switch(inputVal)
{
case 1: {
cout<<"Enter the amount to deposit: ";
cin>>amount;
a.deposit(a.getBalance(), amount);
cout<<"Your new balance is $"<<a.getBalance()<<endl; // right here it
shows the original balance which is incorrect
}
break;
case 2: {
cout<<"Enter the amount to withdrawal: ";
cin>>amount;
a.withdrawal(a.getBalance(), amount);
}
break;
case 3: cout<<"Your balance is: $"<<a.getBalance()<<endl;
break;
case 4: return 0;
break;
default: cout<<"You have entered an invalid value - try again."<<endl;
}

return 0;
}

Any help is GREATLY appreciated!
Thanks!
SB

Jul 22 '05 #4
* "SB" <vo******@hotmail.com> schriebt:
[snipped]


You are ignoring the compiler warnings. Heed the warnings.

Jul 22 '05 #5
On Sun, 22 Feb 2004 16:33:54 GMT in comp.lang.c++, al***@start.no (Alf
P. Steinbach) was alleged to have written:
* "SB" <vo******@hotmail.com> schriebt:
[snipped]


You are ignoring the compiler warnings. Heed the warnings.


report4_q3.cpp:27: warning: I'd turn back if I were you.
Jul 22 '05 #6

"SB" <vo******@hotmail.com> wrote in message
news:cA4_b.14314$Dc2.5864@lakeread01...
Thanks, I'm still a little unsure why my original way didn't work

double getBalance(); This does not return a reference to your internal member. It returns
a copy of the value of that member.
a.deposit(a.getBalance(), amount);

When you make this call, you are passing a reference to the temporary
object returned by getBalance, not to your internal member.

In any case, there is no reason to pass a reference to something the
object already has access to.

Tom
Jul 22 '05 #7
Hello!

References in C++ can certainly be a bit confusing!

Look at this:
void deposit(double&, double);

and this:
double getBalance();

The problem is that the value getBalance() returns is only a copy of
a.balance. This means that the value deposit() changes is only a
temporary variable that disappears whan deposit() exits. This is what
the compiler tries to warn you about, that you possibly don't mean
what you say!

Do this instead:
double& getBalance();

Now, getBalance() returns a reference (the address of the the
variable, instead of the value) to a.balance. Now deposit() will
change a.balance directly, not just a copy.
Jul 22 '05 #8
"SB" <vo******@hotmail.com> wrote in message news:<g54_b.14306$Dc2.5980@lakeread01>...
Ok, very simple problem. I'm trying to update a value by calling a function
using pass by reference, but it does not update the value. In short, the
value I'm trying to update is balance, which is a private member of the
class Account. I have a public function called getBalance(). I have another
public function called deposit, which I pass the balance (by calling
getBalance() using pass by reference) and a second value for the amount of
deposit. Inside the deposit() function if I print the value of the balance
plus the amount to be deposited, it prints the right value, i.e. current
balance plus deposit amount. However, it's not updating the balance member
of the Account class because when I call the getBalance(), it still shows
the original value before the deposit. I know I'm missing something very
simple here, but I just can't see it. Also, I'm getting the following
warning regarding the function calls using pass by reference, but I don't
understand it..

report4_q3.cpp: In function `int main()':
report4_q3.cpp:20: warning: initialization of non-const reference `double &'
from rvalue `double'
report4_q3a.cpp:21: warning: in passing argument 1 of
`Account::deposit(double &, double)'
report4_q3.cpp:27: warning: initialization of non-const reference `double &'
from rvalue `double'
report4_q3a.cpp:29: warning: in passing argument 1 of
`Account::withdrawal(double &, double)'
Here's the .h file....(report4_q3.h)
using namespace std;

class Account
{
private:
double balance;

public:
Account(); // default constructor
double getBalance();
void setBalance(double);
void deposit(double&, double);
void withdrawal(double&, double);
~Account(); // destructor
};

Here's the implementation file...(report4_q3a.cpp)
#include <iostream>
#include <string>

#include "report4_q3.h"

using namespace std;

// default constructor
Account::Account()
{
// give a starting balance of $500
balance = 500.00;
}

double Account::getBalance()
{
return balance;
}

void Account::deposit(double& bal, double deposit)
{
cout<<endl<<"making a deposit!"<<endl;
bal += deposit;
cout<<"after transaction balance is: $"<<bal<<endl; //this of course shows
the correct balance
}

void Account::withdrawal(double& bal, double withdrawal)
{
cout<<"making a withdrawal!"<<endl;
bal -= withdrawal;
cout<<"Your new balance is $"<<getBalance()<<endl;
}

// destructor
Account::~Account()
{
// do nothing
}

Here's the file with main...(report4_q3.cpp)
#include "report4_q3a.cpp"

using namespace std;

int main()
{
Account a;
int inputVal = 0;
double amount = 0.0;

cout<<"Your balance is: $"<<a.getBalance()<<endl;
cout<<endl<<"Press 1 to make a deposit, Press 2 to make a withdrawal, 3 for
account balance and 4 to quit."<<endl;
cin>>inputVal;

switch(inputVal)
{
case 1: {
cout<<"Enter the amount to deposit: ";
cin>>amount;
a.deposit(a.getBalance(), amount); ^^^^^^^^^^^^^^
this returns a double, not a double &. That means you can't edit it
(i.e. you can't say a.getBalance() = 10.5;). But the 1st parameter in
deposit is expected to be a double & so that it can be changed. That's
what's causing you your problem. Change the return type of
Account::getBalance() from double to double &.
cout<<"Your new balance is $"<<a.getBalance()<<endl; // right here it
shows the original balance which is incorrect
}
break;
case 2: {
cout<<"Enter the amount to withdrawal: ";
cin>>amount;
a.withdrawal(a.getBalance(), amount);
}
break;
case 3: cout<<"Your balance is: $"<<a.getBalance()<<endl;
break;
case 4: return 0;
break;
default: cout<<"You have entered an invalid value - try again."<<endl;
}

return 0;
}

Any help is GREATLY appreciated!
Thanks!
SB


First of all, in deposit and withdraw, why do you need the 1st
parameter? Just use those 2 functions for the current object. No need
for the first param. That IS what's causing you your problem. See
above.
Jul 22 '05 #9
"SB" <vo******@hotmail.com> wrote in message news:<g54_b.14306$Dc2.5980@lakeread01>...
[snippers]
class Account
{
private:
double balance;

public:
Account(); // default constructor
double getBalance();
void setBalance(double);
void deposit(double&, double);
void withdrawal(double&, double);
~Account(); // destructor
}; [snip] void Account::deposit(double& bal, double deposit)
{
cout<<endl<<"making a deposit!"<<endl;
bal += deposit;
cout<<"after transaction balance is: $"<<bal<<endl; //this of course shows
the correct balance
}


Ok, it's not entirely clear what you want to do here.
But it looks like this should be like so:

The prototype inside the .h file should be:

void deposit(double);
And the code should be:

void Account::deposit(double deposit)
{
balance += deposit;
}

That is, you should be working directly on the data member, and
not exposing it to the outside world.

This is an excuse for me to get on one of my hobby horses about
object orientation. Try not to think of an object as just a
container of data and functions. Try to think of it as an
exporter of services. Sometimes the services it exports are
little more than just holding data. But here, it looks like
you are working on some kind of bank account thing. Well, to
deposit money into an account you don't need to know how much
money is already in there. (Well, usually anyway.) You just
shove it in there. And also, to finish a deposit, you don't
need to know other details, like how much is in there after
the deposit, where the bank stores the money, etc.

So, think of Account objects as things that have a place you
can shove money in (the deposit function). Then you can see
that there are plenty of other changes you will want to make.
For example, you should be checking for negative amounts,
and doing something about it when they happen. You might
try returning the actual amount deposited.

Also, an Account is a thing you can pull money out of. Again,
you should think about how that actually works. You request
an amount. You get some amount back. As long as you get
what you asked for, you don't need to know the balance
before or after. And since you've got other functions for
that, you don't need that in your withdrawal function.
All you want is to have the function return the actual
amount you withdrew.
Socks
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: lostinspace | last post by:
After four+ years of using FrontPage in a limited capacity and having created over 600 pages on my sites, I've finally (at least for the most part) abandoned FP, to begin the process of converting...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
19
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form...
22
by: Tera | last post by:
can you please give me the link ?
25
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My...
14
by: joey.powell | last post by:
I am using VS2005 for a windows forms application. I need to be able to use a worker thread function to offload some processing from the UI thread. The worker thread will need access to a...
9
by: Tony Girgenti | last post by:
Hello. I'm developing and testing a web application using VS.NET 2003, VB, .NET Framework 1.1.4322, ASP.NET 1.1.4322 and IIS5.1 on a WIN XP Pro, SP2 computer. I'm using a web form. For a...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
8
by: Mr. SweatyFinger | last post by:
where have they all gone. alt.suicide? alt.findContol.then.slit.your.own.throat?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.