It pretty much works fine...except for the fact that it wont output the total amount of withdrawals and deposits. I have no idea how to make it do that. Can anyone help?

#include <iostream>
using namespace std;


float userInput()
{
	float balance;
	
	cout << "Please enter your current checkbook balance..." << endl;
	cin >> balance;
	
	return balance;
}


float useMoney(float deposit, float withdrawal)
{
	float total = 0;
	float cash;
	
	cout << "If you would like to make a withdrawal, please enter a negative number." << endl;
	
	do
	{
		cout << "You may now either make a withdrawal or a deposit." << endl;
		cout << "Typing a zero (0) will terminate your transaction." << endl;
		cin >> cash;
		total += cash;
		
		if(cash > 0)
			deposit++;
		else if(cash < 0)
			withdrawal++;
	}
	while(cash != 0);
	
	return total;
}

char endingData(float balance, float withdrawal, float deposit, float total)
{
	char choice;
	cout << "Opening balance: " << balance << endl;
	cout << "Number of Withdrawals: " << withdrawal << endl;
	cout << "Number of Deposits: " << deposit << endl;
	balance += total;
	cout << "Closing balance: " << balance << endl;
	
	cout << "Would you like to balance another checkbook?" << endl;
	cout << "Type 'y' for yes or type 'n' for no and to quit the program." << endl;
	cin >> choice;
	
	return choice;
}


int main()
{
	char choice;
	float balance, withdrawal, deposit, cash, total;
	
	balance = 0;
	cash = 0;
	withdrawal = 0;
	deposit = 0;
	total = 0;
	
	do
	{
		balance = userInput();
		total = useMoney(deposit, withdrawal);
		choice = endingData(balance, deposit, withdrawal, total);
	}
	while(choice == 'y');
}

It pretty much works fine...except for the fact that it wont output the total amount of withdrawals and deposits. I have no idea how to make it do that. Can anyone help?

Recommended Answers

All 2 Replies

Change the arguments/parameters of your useMoney() function to references. That way, the modified values within the function's scope can get back to the calling scope.

#include <iostream>

void someFunc(int unChangeableValue, int &changeableValue) {
  unChangeableValue++;
  changeableValue++;
}

int main() {
  int value1 = 0, value2 = 0;

  std::cout << "value1 is " << value1 << " and value2 is " << value2 << std::endl;

  someFunc(value1, value2);

  std::cout << "value1 is " << value1 << " and value2 is now " << value2 << std::endl;

  return 0;
}

Ye' have the useMoney() function prototyped to accept a float deposit and a float withdrawal. Both of these variables you have declared inside the main() function on line #60; however, ye' did not pass these vars into the function.

Solution:
change line #70 to accept your balance and withdrawal vars by reference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.