ATM Simulator: Multiple Operations and Account Management in C++
In this C++ exercise, you will create a simple simulation of an ATM (Automated Teller Machine). The program will allow the user to perform basic banking operations such as checking their balance, withdrawing money, depositing funds, and exiting the session.
The user starts with an initial balance and is presented with a menu of options. Proper checks are included to ensure that the user cannot withdraw more money than available in the account.
This task enhances your understanding of input handling, loops, and switch-case statements in C++. By the end of the exercise, you'll be comfortable using control structures to build interactive applications.
Core Concepts
Switch-Case Logic
Using a switch statement provides a clean way to handle multiple menu selections based on the user's numeric input.
Do-While Loops
A do-while loop ensures the ATM menu is displayed at least once and continues until the user explicitly chooses the exit option.
Pro Tip: Validation
Always validate withdrawal amounts against the current balance. This prevents the account from reaching a negative state and ensures data integrity.
Objectives
- 1. Start with a default account balance (e.g., $1000).
- 2. Show a menu with options for Balance, Withdrawal, Deposit, and Exit.
- 3. Use a
switchblock to execute the corresponding banking operation. - 4. Ensure withdrawals do not exceed the available balance and repeat until exit.
Example C++ Code
#include <iostream>
using namespace std;
int main() {
int choice;
double balance = 1000.0;
double amount;
do {
// Display ATM Menu
cout << "\nATM Menu:\n";
cout << "1. Check Balance\n";
cout << "2. Withdraw\n";
cout << "3. Deposit\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Your current balance is: $" << balance << endl;
break;
case 2:
cout << "Enter amount to withdraw: ";
cin >> amount;
if (amount <= balance) {
balance -= amount;
cout << "Withdrawal successful. New balance: $" << balance << endl;
} else {
cout << "Insufficient balance." << endl;
}
break;
case 3:
cout << "Enter amount to deposit: ";
cin >> amount;
balance += amount;
cout << "Deposit successful. New balance: $" << balance << endl;
break;
case 4:
cout << "Thank you for using the ATM. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 4);
return 0;
}
Console Output:
1. Check Balance
2. Withdraw
3. Deposit
4. Exit
Enter your choice: 2
Enter amount to withdraw: 300
Withdrawal successful. New balance: $700