Updating Variables?

Hi all,

I'm trying to write a program (Win32) that gives or takes "money" based upon the throw of the die. My only problem is that I can't get the variable I set to keep track of the money to increase or decrease accordingly. Any help would be greatly appreciated!


Code looks something like this:

#include <iostream>
using namespace std;

int dieRoll;
const int gain = 2;
const int loss = 1;
int guess;
char again;

int main ()
{
double bank = 10.00;
double newbank1 = bank - loss;
double newbank2 = bank + gain;

do
{
cout << "Your bank is $" << bank << "." << endl;
cout << "Please enter a guess for the next die roll." << endl;
cout << "It costs $1.00 to play." << endl;
cout << "If you are correct, I will give you $2.00." << endl;
cout << endl;

cin >> guess;
dieRoll = (int)(rand () % 6) + 1;

if (guess > dieRoll || guess < dieRoll)
{
cout << "\nSorry, the die rolled a " << dieRoll << "." << endl;
cout << "Your bank is now $" << newbank1 << "." << endl;
cout << "Do you want to continue?" << endl;

cin >> again;
cout << endl;
}

else
{
cout << "\nWinner! The die rolled a " << dieRoll << "." << endl;
cout << "Your bank is now $" << newbank2 << "." << endl;
cout << "Do you want to continue?" << endl;

cin >> again;
cout << endl;
}

} while ( again == 'y');

cout << "\nThanks for playing! Your bank is now $" << bank << ". Come back real soon!" << endl;

return 0;

}
get rid of newbank1 and newbank2, and just modify 'bank' directly.

ie:
1
2
3
4
5
// when the user wins, add 'gain' to their bank value
bank += gain;

// when they lose, remove 'loss' from their bank value
bank -= loss;

Thanks so much for the help! I just have one more question... is it possible to have 'bank' update throughout the loop and, dare I ask, upon it's exit?
That's what the code I posted does. Once you modify bank, it forgets its old value and only remembers its new value. So adding/subtracting with the +=, -= operators directly modify 'bank'.

It will keep track of the new, modified value forever (or until the variable goes out of scope and dies)
I completely understand now; thank you very much for your help, Disch!
Topic archived. No new replies allowed.