Metadata
Notes
Detailed Notes on Banking Program in C++
In this practice exercise, we will create a simple banking program in C++ that allows users to:
- Check their balance
- Deposit money
- Withdraw money
- Exit the program
The program will involve basic functions for depositing, withdrawing, and showing the balance, along with proper input validation and error handling.
Key Concepts and Structure
The program consists of the following components:
- Functions:
showBalance(): Displays the user’s current bank account balance.deposit(): Allows the user to deposit money into the account.withdraw(): Allows the user to withdraw money from the account.
- Control Flow:
- A
do-whileloop is used to repeatedly present a menu to the user until they decide to exit. - A
switchstatement handles user inputs (choices) to invoke the corresponding actions.
- A
- Error Handling:
- Checks are included to prevent invalid inputs such as negative deposits, overdrawing from the account, and non-numeric inputs.
1. Program Structure: Functions and Main Program
We start by declaring three functions:
showBalance(double balance): Displays the current balance.deposit(): Prompts the user to enter a deposit amount and adds it to the balance.withdraw(double balance): Prompts the user to enter a withdrawal amount and deducts it from the balance.
Main Function:
#include <iostream>
#include <iomanip>
using namespace std;
void showBalance(double balance) {
cout << "Your balance is: $" << fixed << setprecision(2) << balance << endl;
}
double deposit() {
double amount;
cout << "Enter amount to be deposited: ";
cin >> amount;
if (amount > 0) {
return amount;
} else {
cout << "That's not a valid amount." << endl;
return 0;
}
}
double withdraw(double balance) {
double amount;
cout << "Enter amount to be withdrawn: ";
cin >> amount;
if (amount > 0 && amount <= balance) {
return amount;
} else if (amount > balance) {
cout << "Insufficient funds!" << endl;
return 0;
} else {
cout << "That's not a valid amount." << endl;
return 0;
}
}
int main() {
double balance = 0;
int choice = 0;
do {
cout << "**********************************" << endl;
cout << "1. Show balance" << endl;
cout << "2. Deposit money" << endl;
cout << "3. Withdraw money" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
showBalance(balance);
break;
case 2:
balance += deposit();
break;
case 3:
balance -= withdraw(balance);
break;
case 4:
cout << "Thanks for visiting!" << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
} while (choice != 4);
return 0;
}
Main Function Breakdown:
- Variables:
balance: Holds the current balance, initially set to zero.choice: Stores the user’s menu choice.
- Menu:
- The program displays a menu of options (Show Balance, Deposit, Withdraw, Exit).
- The
do-whileloop continues until the user selects option4(Exit).
- Switch Statement:
- Based on the user’s choice, a case is executed to:
- Display the balance (
showBalance()). - Perform a deposit and update the balance (
deposit()). - Perform a withdrawal and update the balance (
withdraw()). - Exit the program.
- Display the balance (
- Based on the user’s choice, a case is executed to:
2. Functions Implementation
showBalance() Function:
void showBalance(double balance) {
cout << "Your balance is: $" << fixed << setprecision(2) << balance << endl;
}
- Purpose: This function displays the current balance of the user, formatted to two decimal places (to represent dollars and cents).
- Formatting:
- The
fixedmanipulator ensures the output is in fixed-point notation (not scientific notation). - The
setprecision(2)manipulator ensures that the balance is displayed with exactly two decimal places.
- The
deposit() Function:
double deposit() {
double amount;
cout << "Enter amount to be deposited: ";
cin >> amount;
if (amount > 0) {
return amount;
} else {
cout << "That's not a valid amount." << endl;
return 0;
}
}
- Purpose: Allows the user to deposit money into their account.
- Input Validation:
- The user is prompted to enter a deposit amount.
- If the amount is positive, it is returned to be added to the balance.
- If the amount is zero or negative, an error message is displayed, and zero is returned.
withdraw() Function:
double withdraw(double balance) {
double amount;
cout << "Enter amount to be withdrawn: ";
cin >> amount;
if (amount > 0 && amount <= balance) {
return amount;
} else if (amount > balance) {
cout << "Insufficient funds!" << endl;
return 0;
} else {
cout << "That's not a valid amount." << endl;
return 0;
}
}
- Purpose: Allows the user to withdraw money from their account.
- Input Validation:
- If the user tries to withdraw more than the available balance, an error message is displayed (“Insufficient funds”).
- If the user enters an invalid (negative or zero) amount, an error message is displayed.
- The amount to withdraw is returned if valid, otherwise,
0is returned.
3. Handling User Input and Error Prevention
In the program, we handle user input errors by ensuring that:
- Invalid deposits (negative amounts) are rejected.
- Insufficient funds are flagged when trying to withdraw more than the available balance.
- Invalid menu choices (anything other than 1, 2, 3, or 4) are flagged as “Invalid choice”.
4. Clearing Input Buffer
In C++, if the user enters invalid data (such as characters instead of numbers), it can cause issues with subsequent inputs. To prevent this, the following lines are added:
cin.clear(); // Clears the error flags
cin.ignore(numeric_limits<streamsize>::max(), '\\n'); // Ignores the invalid input
These lines ensure that invalid inputs are cleared from the input buffer and do not affect subsequent inputs.
5. Output Formatting
We use the std::fixed and std::setprecision(2) to ensure the balance is always shown in a monetary format (e.g., $123.45 instead of 123.45 or 1.2345e+02).
6. Example Run of the Program
When the program runs, the user is shown the menu, and they can interact with the program by:
- Checking their balance.
- Depositing money.
- Withdrawing money.
- Exiting the program.
Example interaction:
**********************************
1. Show balance
2. Deposit money
3. Withdraw money
4. Exit
Enter your choice: 1
Your balance is: $0.00
Enter your choice: 2
Enter amount to be deposited: 1000.50
Your balance is: $1000.50
Enter your choice: 3
Enter amount to be withdrawn: 500.25
Your balance is: $500.25
Enter your choice: 4
Thanks for visiting!
Conclusion
This program provides a simple simulation of a banking system, allowing users to deposit, withdraw, and check their account balance. By using functions, control flow structures like switch and do-while, and proper input validation, the program offers a practical way to manage user input and prevent errors. This can serve as a foundation for more advanced banking programs with additional features like transferring funds, handling multiple accounts, etc.