C++ Program For Atm Cash Withdrawal Transaction
ATMs have become an indispensable part of modern banking, providing convenient access to funds. In this article, you will learn how to create a basic C++ program that simulates an ATM cash withdrawal transaction, covering essential checks like sufficient balance.
Problem Statement
Users need a reliable way to withdraw cash from their bank accounts via an ATM. The core problem is to design a program that accurately processes a withdrawal request, ensures the user has sufficient funds, dispenses the requested amount, and updates the account balance, all while maintaining security and preventing invalid transactions.
Example
A successful withdrawal might look like this:
Welcome to the ATM!
Your current balance is: $1000.00
Enter amount to withdraw: $200
Transaction successful! Please take your cash.
Your new balance is: $800.00
An unsuccessful attempt due to insufficient funds:
Welcome to the ATM!
Your current balance is: $1000.00
Enter amount to withdraw: $1200
Insufficient funds. Your current balance is $1000.00.
Background & Knowledge Prerequisites
To understand this program, you should have a basic grasp of C++ fundamentals:
- Variables: Declaring and using variables to store data (e.g., account balance, withdrawal amount).
- Input/Output: Using
cinto get user input andcoutto display information. - Conditional Statements: Employing
ifandelseto make decisions based on conditions (e.g., checking if funds are sufficient). - Basic Arithmetic Operators: Performing calculations like subtraction.
Use Cases or Case Studies
Here are common scenarios an ATM withdrawal system must handle:
- Standard Withdrawal: A user requests an amount less than or equal to their available balance.
- Insufficient Funds: A user attempts to withdraw more money than they have in their account.
- Invalid Withdrawal Amount: A user tries to withdraw a negative amount, zero, or an amount that isn't a multiple of the dispense denominations (e.g., trying to withdraw $7.50 when only $10 and $20 bills are available).
- Maximum Daily Limit: The system needs to check if the requested withdrawal exceeds a predefined daily withdrawal limit (not implemented in this basic example, but a real-world consideration).
- ATM Out of Cash: The physical ATM unit might not have enough cash to fulfill the request.
Solution Approaches
For a basic ATM withdrawal simulation, the primary approach involves a series of checks on the requested amount against the available balance.
Basic ATM Cash Withdrawal Simulation
This approach simulates a user interaction where they input a desired withdrawal amount, and the program verifies if the transaction can proceed.
- One-line summary: A simple C++ program that checks a predefined account balance against a user-input withdrawal amount.
// ATM Cash Withdrawal Simulation
#include <iostream> // Required for input/output operations
#include <iomanip> // Required for setting precision for currency display
using namespace std;
int main() {
// Step 1: Initialize account balance
double accountBalance = 1000.00; // Starting balance of the account
double withdrawalAmount; // Variable to store the user's requested amount
// Step 2: Display welcome message and current balance
cout << "Welcome to the ATM!" << endl;
cout << fixed << setprecision(2); // Format output to two decimal places for currency
cout << "Your current balance is: $" << accountBalance << endl;
// Step 3: Prompt user for withdrawal amount
cout << "Enter amount to withdraw: $";
cin >> withdrawalAmount;
// Step 4: Validate the withdrawal amount and process transaction
if (withdrawalAmount <= 0) {
cout << "Invalid amount. Please enter a positive number." << endl;
} else if (withdrawalAmount > accountBalance) {
cout << "Insufficient funds. Your current balance is $" << accountBalance << "." << endl;
} else {
// Step 4a: Process successful withdrawal
accountBalance -= withdrawalAmount; // Deduct the amount from the balance
cout << "Transaction successful! Please take your cash." << endl;
cout << "Your new balance is: $" << accountBalance << endl;
}
return 0; // Indicate successful program execution
}
- Sample Output:
Scenario 1: Successful Withdrawal
Welcome to the ATM!
Your current balance is: $1000.00
Enter amount to withdraw: $300
Transaction successful! Please take your cash.
Your new balance is: $700.00
Scenario 2: Insufficient Funds
Welcome to the ATM!
Your current balance is: $1000.00
Enter amount to withdraw: $1500
Insufficient funds. Your current balance is $1000.00.
Scenario 3: Invalid Amount (Zero or Negative)
Welcome to the ATM!
Your current balance is: $1000.00
Enter amount to withdraw: $-50
Invalid amount. Please enter a positive number.
- Stepwise explanation for clarity:
- Include Headers: The program starts by including
for console input/output andto format currency output. - Initialize Balance: A
doublevariableaccountBalanceis set to an initial value (e.g.,$1000.00). AnotherdoublevariablewithdrawalAmountis declared to store the user's input. - Display Info: The program greets the user and displays their current account balance, formatted to two decimal places using
fixedandsetprecision(2). - Get Input: The user is prompted to enter the amount they wish to withdraw, which is then read into the
withdrawalAmountvariable usingcin. - Validate Amount: An
if-else if-elsestructure checks the validity of the request:- It first verifies if the
withdrawalAmountis positive. If not, an error message is displayed.
- It first verifies if the
withdrawalAmount is greater than accountBalance. If it is, an "Insufficient funds" message is shown.- Process Transaction: For a successful transaction,
withdrawalAmountis subtracted fromaccountBalance, and a success message along with the new balance is displayed.
Conclusion
This article provided a foundational understanding of how to simulate an ATM cash withdrawal transaction using C++. By implementing simple conditional logic, we can effectively manage account balances and ensure that only valid withdrawals are processed, laying the groundwork for more complex banking simulations.
Summary
- Problem: Simulate ATM cash withdrawal while validating funds.
- Core Logic: Use
if-elsestatements to check if the requested amount is valid and if sufficient funds are available. - Key Variables:
accountBalance(initial funds) andwithdrawalAmount(user input). - User Interaction: Prompts for input and provides clear feedback on transaction status.
- Output Formatting: Uses
to display currency values with two decimal places.