Write a C++ program for simple ATM Simulation to Check Balance, Deposit, and Withdraw money.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include <iostream> using namespace std; int main() { int option; double balance = 1000.0; // Initial balance double depositAmount; double withdrawAmount; while (true) { // Display the menu cout << "ATM Menu:\n"; cout << "1. Check Balance\n"; cout << "2. Deposit\n"; cout << "3. Withdraw\n"; cout << "4. Exit\n"; cout << "Enter your option (1-4): "; cin >> option; // Handle the user's choice switch (option) { case 1: // Check balance cout << "Your current balance is: $" << balance << endl; break; case 2: // Deposit cout << "Enter deposit amount: $"; cin >> depositAmount; balance += depositAmount; cout << "Deposited $" << depositAmount << ". New balance is: $" << balance << endl; break; case 3: // Withdraw cout << "Enter withdraw amount: $"; cin >> withdrawAmount; if (withdrawAmount > balance) { cout << "Insufficient funds. Withdrawal denied." << endl; } else { balance -= withdrawAmount; cout << "Withdrew $" << withdrawAmount << ". New balance is: $" << balance << endl; } break; case 4: // Exit cout << "Exiting the ATM. Thank you!" << endl; return 0; // Exit the program default: // Invalid option cout << "Invalid option. Please enter a number between 1 and 4." << endl; break; } } return 0; } |
Possible Outputs
Output#1
ATM Menu:
1. Check Balance
2. Deposit
3. Withdraw
4. Exit
Enter your option (1-4): 1
Your current balance is: $1000.00
Output#2
ATM Menu:
1. Check Balance
2. Deposit
3. Withdraw
4. Exit
Enter your option (1-4): 2
Enter deposit amount: $500
Deposited $500.00. New balance is: $1500.00
Output#3
ATM Menu:
1. Check Balance
2. Deposit
3. Withdraw
4. Exit
Enter your option (1-4): 3
Enter withdraw amount: $200
Withdrew $200.00. New balance is: $1300.00\
Output#4
ATM Menu:
1. Check Balance
2. Deposit
3. Withdraw
4. Exit
Enter your option (1-4): 4
Exiting the ATM. Thank you!