Write a program that calculates the closing balance for an indeterminate number of checkbooks. The program must logically flow as follows:

* Step 1. Ask for the data for the first checkbook- opening balance, withdrawals and deposits
* Step 2. Display the opening balance, the number of withdrawals, the number of deposits and the closing balance
* Step 3. Ask whether the user wishes to calculate the balance on another checkbook:
o If so, go back to Step 1
o Otherwise, end the program

All data will be obtained from standard input. The format for the monetary data will be a series of numbers, each one occurring on its own line of input. The first number is the opening balance. Thereafter, the user will enter withdrawals as negative numbers and deposits as positive numbers. The user will signify the end of the monetary data by entering a zero. The user will enter a lower case 'y' to select the option of balancing another checkbook; otherwise, the user will enter a lower case 'n'.

This is my code so far:

#include <iostream>
using namespace std;
	
	// Ask the user to input the balace.
	int askUser(int balance)
	{
	    cout << "Please enter the current balance in checkbook: " << endl;
	    cin >> balance;
		return balance;
	}
	
	// Calculate the number of deposits and withdrawals.
	int addMoney(int withdrawals, int deposits, int total)
	{
	   cout << "Enter a negative integer to indicate a withdrawal.\n";
	   int money;
	   do
	   {
	      cout << "Please deposit or withdraw money into the account: 0 ends the transaction " << endl;
	      cin >> money;
	      total += money;
	      if (money > 0 ) ++deposits;
	      else if (money < 0) ++withdrawals;
	   }
	   while (money != 0);
	   return 0;
	}   
	 
	// Calcuate the total balance after the user withdraws and deposits money and provide a choice to balance another checkbook.
	void OutputData(int balance, int withdrawals, int deposits, int total,  char  decision)
	{
    
	    cout << "Opening balance: " << balance << endl;
	    cout << "Number of Deposits: " << deposits << endl;
	    cout << "Number of Withdrawls: " << withdrawals << endl;
		balance += total;
	    cout << "Closing Balance: " << balance << endl;
	    cout << "To balance another checkbook press y, otherwise press n to quit the program: " << endl;
	    cin >> decision;
	}
	 
	//Call the above functions and determining wherether to end the loop or continue with the calculations.
	int main()
	{
	    char decision = 0;
	    int balance, withdrawals, deposits, total, money;
		money = 0;
		balance = 0;
		deposits = 0;
		withdrawals = 0;
		total = 0;
		balance = 0;
	    int finish = 1;
	    do						// Using the do-while loop to call the fucntions.
	    {
	        askUser(balance);
	        addMoney(withdrawals, deposits, total);
	        OutputData(balance, withdrawals, deposits, total, decision);
    }
	    while (decision == 'y');
	 
	}

I am having trouble with this because I am not suppose to use reference variables. Can anyone please tell me what I am doing wrong? Everytime I try to return a value, the calculations always come out to be zero. I don't know why this is happening. Please help!!!

Recommended Answers

All 5 Replies

I am having trouble with this because I am not suppose to use reference variables.

And indeed you are not. However, you are CALLING your function as if you ARE passing by reference.

Lines 5 and 56:

int askUser(int balance)
askUser(balance);

Note that in line 5, your function RETURNS an int. Note that in line 56, you do not do anything with that return value.

Suppose you type in 24 on line 7. 24 is returned from the function and in line 56 you completely ignore that 24 and it's gone before because you never harnessed that value. And since balance is NOT passed by reference, passing balance to the function doesn't do anything. And the function really doesn't use the passed parameter, so why bother with it? Seems like this would be better.

int askUser()
{
    int balance;
    cout << "Please enter the current balance in checkbook: " << endl;
    cin >> balance;
    return balance;
}

Then the call on line 56 would be this...

balance = askUser();

Ok so I did that but it is still not giving me all the calculations. My teacher advised me to call OutputData() directly from addMoney() rather than from main. And I cannot do that since that would give me a forward declaration error.

>> Ok so I did that but it is still not giving me all the calculations. My teacher advised me to call OutputData() directly from addMoney() rather than from main. And I cannot do that since that would give me a forward declaration error.


Can't comment on revised code I haven't seen and without seeing the exact error message.

OutputData is going to have some of the same problems as discussed before. Ditto addMoney. You're passing parameters by value that function purely as local variables. I'm talking about "total" in addMoney and "decision" in OutputData. Those shouldn't be parameters if you aren't passing by reference and if you aren't using the passed values in the function. You should declare them locally in the function and RETURN them from the function, as discussed above. Changing them inside the function has no effect at all unless you return the value or pass by reference.

For example...

char OutputData(int balance, int withdrawals, int deposits, int total)
{
    char decision;
    cout << "Opening balance: " << balance << endl;
    cout << "Number of Deposits: " << deposits << endl;
    cout << "Number of Withdrawls: " << withdrawals << endl;
    balance += total;
    cout << "Closing Balance: " << balance << endl;
    cout << "To balance another checkbook press y, otherwise press n to quit the program: " << endl;
    cin >> decision;
    return decision;
}

Function call...

decision = OutputData(balance, withdrawals, deposits, total);
#include <iostream>
using namespace std;
	
// Ask the user to input the balace.
int askUser()
{
	 int balance;
	 cout << "Please enter the current balance in checkbook: " << endl;
	 cin >> balance;
	 return balance;
}
	
// Calculate the number of deposits and withdrawals.
int addMoney(int deposits, int withdrawals)
{
	int total = 0;
	cout << "Enter a negative integer to indicate a withdrawal.\n"<< endl;
	int money;
	do
	 {
	      cout << "Please deposit or withdraw money into the account: 0 ends the transaction " << endl;
	      cin >> money;
	      total += money;
	      if (money > 0 ) ++deposits;
	      else if (money < 0) ++withdrawals;
	 } while (money != 0);
	   return total;
}   

	 
// Calcuate the total balance after the user withdraws and deposits money and provide a choice to balance another checkbook.
char OutputData(int balance, int withdrawals, int deposits, int total)
{
	char decision;
	cout << "Opening balance: " << balance << endl;
	cout << "Number of Deposits: " << deposits << endl;
	cout << "Number of Withdrawls: " << withdrawals << endl;
    balance += total;
	cout << "Closing Balance: " << balance << endl;
	cout << "To balance another checkbook press y, otherwise press n to quit the program: " << endl;
	cin >> decision;
	return decision;
}
	 
//Call the above functions and determining wherether to end the loop or continue with the calculations.
int main()
{
	    char decision = 0;
	    int balance, withdrawals, deposits, total, money;
		money = 0;
		balance = 0;
		deposits = 0;
		withdrawals = 0;
		total = 0;
	    int finish = 1;
	    do						// Using the do-while loop to call the functions.
	    {
	        balance = askUser();
		    total = addMoney(deposits, withdrawals);
	        decision = OutputData(balance, withdrawals, deposits, total);
        }
	    while (decision == 'y');

	 system("pause");
}

So this is what my code looks like now. It now calculates the closing balance correctly but still does not give me the number of deposits and withdrawals. How can I call OutputData() directly from addMoney()?

1) Move the OutputData() function above addMoney()
2) Add prototypes (look it up) of your functions at the top of the program

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.