8 Replies - 9366 Views - Last Post: 04 June 2008 - 01:03 AM Rate Topic: -----

#1 DLRogue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-May 08

Write a program that tracks deposits, withdrawals and the balance of a

Posted 31 May 2008 - 09:05 PM

Hello, I am writing this program for class:

Quote

Write a program that tracks deposits, withdrawals and the balance of a savings account. Us a sentinel-controlled loop for user input. The user should be able to deposit or withdraw money from the savings account. The output will display the amount of deposit or withdrawal and the current balance. Do not allow the balance to become negative. Display an error if the user tries to withdraw more than his/her balance. Define a function called Deposit(). This function will handle deposits by adding the amount deposited to the balance. Define a function called Withdraw(). This function will subtract the amount input by the user from the balance. If a negative balance will result, an error message should be displayed and the balance should not be altered. Define a function called CheckBalance(). This function will return the current account balance. There is no I/O needed, the balance should be initialized to 0 each time the program is run.


We just learned about functions in class and I am drawing a blank here. I don't even know where to begin. I know it says post our code first, but as of now, I have no code. I don't want my homework done for me, I just need to know where to start this. Can anyone please help?

Is This A Good Question/Topic? 0
  • +

Replies To: Write a program that tracks deposits, withdrawals and the balance of a

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5309
  • View blog
  • Posts: 27,210
  • Joined: 10-May 07

Re: Write a program that tracks deposits, withdrawals and the balance of a

Posted 31 May 2008 - 09:10 PM

View PostDLRogue, on 1 Jun, 2008 - 12:05 AM, said:

I don't even know where to begin.

Flow chart.

Seriously, before you begin a large project, you should plan it out. Maybe not a flowchart exactly, but figure out everything that is required, & start a basic skeleton. Figure out which functions you need to run outside of main, & what values will be passed to them & back. Then when you've got that down, start filling in the details.
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Write a program that tracks deposits, withdrawals and the balance of a

Posted 31 May 2008 - 09:21 PM

Let's start with the loop: sentinel value is an exit condition. Usually they are used in while loops. Here's an idea in somewhat pseudo code:

int main()
{
     while(yourFlag) 
     {
          int balance;
          //Ask for user input
          cin >> choice;
          cin >> input;
          
          //If deposit
          balance = Deposit (input, balance);
          
          //If withdrawal
          balance = Withdraw(input, balance);
          
          //So on and so forth
     }
}

int Deposit (int amount, int curBalance)
{
    curBalance += amount;
    return curBalance;
}

int Withdraw (int amount, int curBalance)
{
    curBalance -= amount;
    return curBalance;
}



I second planning out your program before coding like no2pencil said. Planning is the most important part of any coding project in my humble opinion.

This post has been edited by KYA: 31 May 2008 - 09:22 PM

Was This Post Helpful? 0
  • +
  • -

#4 DLRogue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-May 08

Re: Write a program that tracks deposits, withdrawals and the balance of a

Posted 01 June 2008 - 08:20 PM

So there's nothing especially difficult to this? The whole function thing was confusing the world out of me! I thought there was more to this program than simple math.
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Write a program that tracks deposits, withdrawals and the balance of a

Posted 01 June 2008 - 08:23 PM

Yup, nothing more then adding or subtracting based on your project description.

Just pass the values to various functions as parameters.
Was This Post Helpful? 0
  • +
  • -

#6 DLRogue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-May 08

Re: Write a program that tracks deposits, withdrawals and the balance of a

Posted 02 June 2008 - 06:13 PM

Okies, here's my code:
#include "stdafx.h"
#include <iostream>

using namespace std;
int main()  
{  

	//Variables
	int Balance=0;
	int newBalance=0;
	int Amount=0;
	int answer=0;

		 cout << "Do you want to make a deposit, withdrawal, or check balance?""/n"; 
		 cout << "Enter 1 for Deposit, 2 for Withdrawal, or 3 for Check Balance.""/n";
		 cin >> answer;
	
	if(newBalance >= 0)


	 {  
		for(answer = 1)

		{

		cout << "How much do you want to deposit? " << endl;
		cin >> Amount;
		newBalance = Balance + Amount;
		return newBalance;
		cout << "Your Deposit is: " << Amount << "/n" << endl;
		cout << "Your Balance is: " << newBalance << "/n" << endl;

		}

		{

		for(answer = 2)
		cout << "How much do you want to withdraw? " << endl;
		cin >> Amount;
		newBalance = Balance - Amount;
		return newBalance;
		cout << "Your Withdrawal is: " << Amount << "/n" << endl;
		cout << "Your Balance is: " << newBalance << "/n" << endl;

		}

		{
		
		for(answer = 3)
		return newBalance;
		cout << "Your balance is: " << newBalance << "/n" << endl;

		}

	}

	 else

	{
		
		cout << "Your account is overdrawn, please make a deposit" "/n" " before attempting to withdraw more funds." << endl;

	}

	
		 
	cout << "Thank you and have a nice day!";

return(0);

}



But I keep going these error message:

Quote

error C2143: syntax error : missing ';' before ')'


Not quite understanding this because I'm looking and where they say ";" needs to go, it isn't necessary. Can someone please help?
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Write a program that tracks deposits, withdrawals and the balance of a

Posted 02 June 2008 - 11:19 PM

You're using the incorrect loop for the user input. for loops take three arguments or null values in place of them.

Also, you cannot have multiple return statements inside main(). You'd have to pass control to functions and have them return the values.

The escape character is "\n" not "/n"

/*
 * Bank Program
 * KYA
 * Possible Solution
 * Enjoy!
 */

#include <iostream>

using namespace std;

//function prototypes
int Deposit(int amount, int curBalance);
int Withdrawal(int amount, int curBalance);
void checkBalance(int curBalance);

int main()  
{  

    //Variables
    int curBalance=0; //assumes a starting balance of nothing
    int amount=0;
    int answer=0;
	bool notDone = true;

	while (notDone) {

     cout << "Do you want to make a deposit, withdrawal, or check balance?""\n";
     cout << "Enter 1 for Deposit, 2 for Withdrawal, 3 for Check Balance, or 4 to quit.""\n";
     cin >> answer;
    
    switch(answer)
	{
		case 1:
			 cout << "How much do you want to deposit? " << endl;
			 cin >> amount;
			 curBalance = Deposit(amount, curBalance); 
			 cout << "Your Deposit is: " << amount << "\n" << endl;
		     cout << "Your Balance is: " << curBalance << "\n" << endl;
			 break;
		case 2:
			cout << "How much do you want to withdraw? " << endl;
			cin >> amount;
			if (amount > curBalance){
				cout << "You cannot overdraw your account. Try again.\n";
				break;
			}//end account check
			else {
				curBalance = Withdrawal(amount, curBalance);
				cout << "Your Withdrawal is: " << amount << "\n" << endl;
				cout << "Your Balance is: " << curBalance << "\n" << endl;
				break;
			}
		case 3:
			checkBalance(curBalance);
			break;
		case 4:
			notDone = false;
			break;
		default:
			cout << "Not Valid input, try again.";
			break;
	}//end switch
	}//end main while loop

    cout << "\nThank you and have a nice day!\n";
	system("pause");
    return 0;
}//end main

int Deposit(int amount, int curBalance)
{
	curBalance += amount;
	return curBalance;
}//end deposit

int Withdrawal(int amount, int curBalance)
{
	curBalance -= amount;
	return curBalance;
}//end withdrawal

//POINTLESS METHOD!!!
void checkBalance (int curBalance)
{
	cout << "\nYour current balance is: " << curBalance << "\n";
}//this function is unnecessary, you can check the variable inside main(), but your instructions say to use this



Enjoy. Feel free to press the thank you button if you liked it.
Was This Post Helpful? 1
  • +
  • -

#8 DLRogue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-May 08

Re: Write a program that tracks deposits, withdrawals and the balance of a

Posted 03 June 2008 - 11:05 PM

View PostKYA, on 2 Jun, 2008 - 11:19 PM, said:

Feel free to press the thank you button if you liked it.


Haven't stopped pressing it yet!! :bananaman: :bananaman: :bananaman: :genius:

The SWITCH CASE loop was what I was looking for but couldn't remember. Also, I'm not sure what you meant by the multiple return statements. I'm looking through my code and I only see the one at the very bottom. If I'm missing the others, please show me what I'm missing, please! :stupid: :stupid:
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Write a program that tracks deposits, withdrawals and the balance of a

Posted 04 June 2008 - 01:03 AM

Under each for statement you had a

return newBalance;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1