I am an absolute newbie to C++. I have an ATM program problem where I need to keep a running total of transactions and the balance of the checking account. I cannot think of how to set an accumulator for the transactions or how to keep a running total of the balance after each withdrawal and deposit. I added a static number to try to keep a running total of the transactions, this could be completely wrong but I wasn't sure what else to try. Thanks for all your help.

Below is the prompt and the code I've written so far:

Reorganize the program to implement functions to perform the main transactions of deposit or withdrawal as well as check balance, and exit. As this program executes each transaction you should allow the user to see the results of each transaction by displaying the following message:
“After your < Deposit/Withdrawal > your current balance is <insert current balance>”

At the end of the program modify the ending message to include the following:
“You have completed <insert number of transactions>
Your beginning balance was <Insert Initial Balance>
Your ending balance is <insert current balance>”
Note that calls to the check balance and the exit call do not count as transactions in the overall transaction count displayed at the end of the program.

#include <iostream>
#include <iomanip>
using namespace std;

void displayMenu ( );
void showStatic (); //Function for transaction accumulator
double depositBalance (double);
double withdrawalBalance (double);
int selection;  //Choice option
double deposit, withdrawal, fdeposit, fwithdraw, amount;
double balance = 500.00;
int total = 0;

int main ()

{  
	cout << setprecision(2) << fixed << endl;
	// Do while loop for menu selection
	do
	{
	displayMenu ();
	cout << "Which option would you like to select? " <<endl;
	cin >> selection;

	switch (selection)
	{
	case 1:
		fdeposit = depositBalance (deposit);
	cout << "After your deposit your current balance is $" << fdeposit << endl;
		continue;

	case 2:
		fwithdraw = withdrawalBalance (withdrawal);
	cout << "After your withdrawal your current balance is $" << fwithdraw << endl;
		continue;

	case 3:
		cout << "Your balance is $" << balance <<endl;
		continue;

	case 4:	
		cout << "Good-bye!" << endl;
		exit (0);

	default:
		cout << "This is not an option.  Please try again!" << endl;
		break;
	}       
        } while (selection !=4);

	// For loop for transaction accumulator
	for (int count = 1; count == (selection == 1 || selection == 2); count++)
	{
		showStatic ();
	}
	
        //Ending messages
        cout << "You have completed " << showStatic () << " transactions. ";
	cout << "Your beginning balanace is $" << balance << endl;
        cout << "Your ending balance is $ " << endl;

	system ("pause");
}
	void displayMenu ()
	{
	cout << "Account Menu" << endl; 
	cout << "-------------------" << endl;
	cout << "1. Deposit" << endl;
	cout << "2. Withdrawal" << endl;
	cout << "3. Check Balance"<< endl;
	cout << "4. Exit" << endl;
	}

	double depositBalance (double num)
	{ 
		cout << "How much do you want to deposit? ";
		cin >> deposit;
		return balance + deposit;
	}

	double withdrawalBalance (double num)
	{
		cout << "How much do you want to withdraw? ";
		cin >> withdrawal;
		return balance - withdrawal;
	}
	
	void showStatic ()
	{ 
		static int statNum;
		cout << "You have completed " << statNum << " transactions." << endl;
		statNum++;
	}
case 1:
		fdeposit = depositBalance (deposit);
	cout << "After your deposit your current balance is $" << fdeposit << endl;
		continue;

	case 2:
		fwithdraw = withdrawalBalance (withdrawal);
	cout << "After your withdrawal your current balance is $" << fwithdraw << endl;
		continue;

	case 3:
		cout << "Your balance is $" << balance <<endl;
		continue;

What is the purpose of the variables fdeposit, fwithdraw, and balance?

I cannot think of how to set an accumulator for the transactions or how to keep a running total of the balance after each withdrawal and deposit.

What is the variable you defined to keep the running total?

What is the purpose of the variables fdeposit, fwithdraw, and balance?


What is the variable you defined to keep the running total?

fdeposit stores the balance after a deposit, fwithdraw stores the balance after a withdrawal, and balance is the initial balance. The initial balance is $500.00.

I had defined the variable total to keep the running total but I did not know how to include it with the static number.

Maybe you should consider naming your variables "initial_balance" (if you need to keep track of that for any reason) and "current_balance". Your current_balance is the value that will keep changing every time you do a deposit or withdrawal.

You don't need a "static number" to keep track of the number of transactions, since you're already using global variables for everything. Call it something useful, like transaction_count, make it a global with all the others, initialize it to 0, and then do something to it (left to your discretion) each time you do -any- transaction. (As written, your "static number" just keeps track of the number of times you've printed out your number of transactions -- not what you intended!)

fdeposit stores the balance after a deposit, fwithdraw stores the balance after a withdrawal, and balance is the initial balance. The initial balance is $500.00.

I had defined the variable total to keep the running total but I did not know how to include it with the static number.

Why? What is the purpose of 4 different balances? Do you really care about your balances after each deposit and withdrawal as separate values? Or is a deposit or withdraw simple a value added/subtracted from the overall balance?

Ok so I figured out how to keep a total for the initial and current balances but I am still having trouble with the running total of transactions. I can't figure out a way to keep a running total without the selection option adding up. This is my updated code:

#include <iostream>
#include <iomanip>
using namespace std;

void displayMenu ( );
double deposit(double);
double withdraw(double);
double initialBalance(double);
double getBalance(double);
int transactions(int);
int selection;
double balance = 500.00;
int total = 0;
double amount;

int main ()

{  
	cout << setprecision(2) << fixed << endl;

	//Do while statement for menu selection
	do
	{
	displayMenu ();
	cout << "Which option would you like to select? " <<endl;
	cin >> selection;

		
	switch (selection)
	{
	case 1:
		cout << "After your deposit your current balance is $" <<  deposit(amount) << endl;
		continue;

	case 2:
		cout << "After your withdrawal your current balance is $" << withdraw (amount)<< endl;
		continue;

	case 3:
		cout << "Your balance is $" << balance <<endl;
		continue;

	case 4:	
		cout << "Good-bye!" << endl;
		cout << "You have completed " << transactions(selection) << " transactions. " << endl;
		cout << "Your beginning balanace is $" << initialBalance(balance) << endl;	
		cout << "Your ending balance is $" << getBalance (amount) << endl;
		break;

	default:
		cout << "This is not an option.  Please try again!" << endl;
		continue;

	} 
	
	} while (selection !=4);
	
	system ("pause");
}
	void displayMenu ()
	{
	cout << "Account Menu" << endl; 
	cout << "-------------------" << endl;
	cout << "1. Deposit" << endl;
	cout << "2. Withdrawal" << endl;
	cout << "3. Check Balance"<< endl;
	cout << "4. Exit" << endl;

	}

 double initialBalance(double balance)
{
   return balance = 500.00;
}

double deposit(double amount)
{
   cout << "How much would you like to deposit? $";
   cin >> amount;
   return balance += amount;
}

double withdraw(double amount)
{
   cout << "How much would you like to withdraw? $";
   cin >> amount;
   return balance -= amount;
}

double getBalance(double amount)
{
   return balance;
}

int transactions(int selection)
{
	for (int count = 1; count == (selection == 1 || selection == 2); count++)
	
		return total+=selection;
	
}

Looking at line 32 and lines 76-81:

First, at line 32, you're printing a prompt for what your balance will be after the deposit, then when you call the function, at line 78, you prompt for how much to deposit. So your output text will probably be out of proper order.

You also pass in to deposit() the uninitialized variable amount, then read it and use it only within the function -- so instead, make it a local variable of the function and don't pass it at all.

If you think about what has to happen in what order, the rest will fall into place: prompt the user for an amount to deposit, get the amount from the user, update the balance, print the result of the transaction.

Same for withdrawal.

For your transactions() function, I have no idea what you're trying to accomplish there, but again, you're passing in and using an uninitialized variable.

This article has been dead for over six months. Start a new discussion instead.