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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
#include <iostream> #include <iomanip> // For std::setw and std::setprecision using namespace std; // Function prototypes void displayMenu(); void checkBalance(double balance); void deposit(double &balance); void withdraw(double &balance); int main() { double balance = 0.0; // Initial balance int choice; do { displayMenu(); cin >> choice; switch (choice) { case 1: checkBalance(balance); break; case 2: deposit(balance); break; case 3: withdraw(balance); break; case 4: cout << "Exiting the ATM. Have a great day!" << endl; break; default: cout << "Invalid choice. Please select a valid option." << endl; break; } } while (choice != 4); return 0; } // Function to display the ATM menu void displayMenu() { cout << "\nATM Menu" << endl; cout << "1. Check Balance" << endl; cout << "2. Deposit Money" << endl; cout << "3. Withdraw Money" << endl; cout << "4. Exit" << endl; cout << "Enter your choice: "; } // Function to check and display the current balance void checkBalance(double balance) { cout << fixed << setprecision(2); // Set precision for displaying currency cout << "Current Balance: $" << balance << endl; } // Function to deposit money into the account void deposit(double &balance) { double amount; cout << "Enter amount to deposit: $"; cin >> amount; if (amount > 0) { balance += amount; cout << "Deposited $" << amount << ". New Balance: $" << balance << endl; } else { cout << "Invalid deposit amount. Please enter a positive value." << endl; } } // Function to withdraw money from the account void withdraw(double &balance) { double amount; cout << "Enter amount to withdraw: $"; cin >> amount; if (amount > 0 && amount <= balance) { balance -= amount; cout << "Withdrew $" << amount << ". New Balance: $" << balance << endl; } else if (amount > balance) { cout << "Insufficient funds. Withdrawal amount exceeds balance." << endl; } else { cout << "Invalid withdrawal amount. Please enter a positive value." << endl; } } |
Explanation
- Include Libraries:
#include <iostream>
: For input and output operations.#include <iomanip>
: For formatting the output (e.g., setting decimal precision).
Advertisement - Function Prototypes:
void displayMenu();
: Displays the ATM menu.void checkBalance(double balance);
: Displays the current balance.void deposit(double &balance);
: Allows depositing money and updates the balance.void withdraw(double &balance);
: Allows withdrawing money and updates the balance.
- Main Function:
- Initializes
balance
to0.0
. - Runs a
do-while
loop to repeatedly show the menu and perform operations based on user input. - Uses a
switch
statement to handle different user choices.
- Initializes
- Menu Display Function (
displayMenu
):- Outputs the available options to the console.
- Check Balance Function (
checkBalance
):- Displays the current balance formatted to two decimal places.
- Deposit Function (
deposit
):- Takes the deposit amount from the user.
- Checks if the amount is positive before adding it to the balance.
Advertisement - Withdraw Function (
withdraw
):- Takes the withdrawal amount from the user.
- Checks if the amount is positive and does not exceed the current balance before subtracting it from the balance.
- Provides appropriate messages if the withdrawal is invalid or exceeds the balance.