-5

This programs purpose is to be a menu driven check and balancing program. I am trying to get the variable "balance" to remember the value it holds after specific operations have been done to it. I'm fairly new to C++ and I can't find this information anywhere.

int main(void)
{
double balance, deposit, withdraw;
char select;

std::cout << "Welcome to the check balancing program." << std::endl;
std::cout << "Please enter the current balance : " << std::endl;
std::cin >> balance;
std::cout << "Current balance : " << balance << std::endl;


do
{
std::cout << "please make your selection. \n";
std::cout << "W/w : Withdraw, " << "D/d : Deposit, " << "E/e : Ex  <<std::endl;
    std::cin >> select;

    switch (select)
    {
        case 'W' : case 'w':
            std::cout << "Please enter a withdrawal amount : ";
            std::cin >> withdraw;
            std::cout << "withdraw : " << withdraw << std::endl;
            std::cout << "Remaining balance : " << (balance = balance  - withdraw) << std::endl;
            break;
        case 'D' : case 'd':
            std::cout << "Please enter a deposit amount : ";
            std::cin >> deposit;
            std::cout << "deposit : " << deposit << std::endl;
            std::cout << "Remaining balance : " << (balance = balance + deposit) << std::endl;
            break;
        case 'E' : case 'e':
            std::cout << "The new balance : " << balance <<   std::endl;
            std::cout << "Thank you for using check balance program.   Goodbye." << std::endl;
            break;
        default:
            std::cout << "You have entered an invalid code. Please   try again." << std::endl;
        }
} while (select != 'e');
}
  • 2
    All questions on stackoverflow.com must include complete information in the question itself ***as plain text***. Links to external web sites, that can stop working at any time rendering the question meaningless, are not acceptable. – Sam Varshavchik Mar 21 '17 at 21:30
  • remember the value after you do what?? variables usually dont forget their value – 463035818_is_not_an_ai Mar 21 '17 at 21:31
  • 1
    use `balance= operations with balance as operand` – monster Mar 21 '17 at 21:31
  • Do you know how to assign values to variables? http://en.cppreference.com/w/cpp/language/operator_assignment – UnholySheep Mar 21 '17 at 21:31
  • I know I'm probably not wording this correctly. What I mean is on this program it asks for the balance, so you input it. Then it asks if you would want to withdraw (subtract) or deposit (add). When i select the option to end the menu the program is supposed to give me the new balance, but it gives me the balance from the very beginning. – Tyler Rodriguez Mar 21 '17 at 21:33
  • 2
    [C++ books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) you should read. – Captain Obvlious Mar 21 '17 at 21:35
  • @TylerRodriguez -- *I'm fairly new to C++ and I can't find this information anywhere* -- It's called logic -- this isn't a magic bullet that you will find using google. Simply create a variable to hold the original balance: `double origBalance = balance;` and work with `balance`. – PaulMcKenzie Mar 21 '17 at 21:38
  • You can do: `balance -= withdraw` and `balance += deposit`. Its really that simple if I got your question correctly: _variables that save their values after operations_. – BusyProgrammer Mar 21 '17 at 21:41
  • Also, please post your code as formatted text in your post. It is a lot harder for us to replicate your code if we cannot edit it. – BusyProgrammer Mar 21 '17 at 21:42

1 Answers1

-1

In your 'Deposit' case statement, you need to save the new value by assigning the variable balance with the sum of the original balance plus the deposit, which should be balance + deposit. So:

balance = balance + deposit

Then you can print out the result:

std::cout << "Remaining Balance: " << balance << std::endl;
privatestaticint
  • 854
  • 1
  • 9
  • 23
  • @CaptainObvlious how so? He's clearly printing the result of the summation but never saving it for use later in his program. – privatestaticint Mar 21 '17 at 21:40
  • @privatestaticint that fixed what I was wanting. Am I wording this incorrectly? what should I have asked to make things more clearer for future reference? – Tyler Rodriguez Mar 21 '17 at 21:44
  • @TylerRodriguez I think you worded it fine - I understood your problem clearly. People on here are just very strict about the posting guidelines. In the case of your post, you linked 2 images of the code and output as opposed to using the actual text (people have issues with this because links can stop working anytime, whereas text would be preserved in the question forever). – privatestaticint Mar 21 '17 at 21:47
  • @TylerRodriguez if this fixed your issue, please mark it as the solution so people in the future know what fixed the issue. – privatestaticint Mar 21 '17 at 21:50