ATM Simulator with Multiple Operations 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. Based on their selection, the corresponding operation is executed. Proper checks are included to ensure that the user cannot withdraw more money than available in the account.

This project will help you practice using loops, conditionals, user input, and switch-case statements. It is an excellent example of applying control structures to build an interactive application.

Group

Advanced Conditional Structures in C++

Objective

1. Start with a default account balance (e.g., $1000).
2. Show a menu with the following options:
- 1. Check Balance
- 2. Withdraw
- 3. Deposit
- 4. Exit
3. Based on the user's choice, perform the corresponding action.
4. For withdrawals, ensure that the amount does not exceed the available balance.
5. Repeat the menu until the user chooses to exit.

Write a program that simulates an ATM with multiple options such as withdrawal, balance inquiry, deposit, and exit.

Example C++ Exercise

 Copy C++ Code
#include <iostream>  // Include input/output stream library

using namespace std;

int main() {
    int choice;  // Variable to store user's menu selection
    double balance = 1000.0;  // Initial balance set to $1000
    double amount;  // Variable for deposit or withdrawal amount

    // Use a loop to keep showing the menu until user exits
    do {
        // Display the 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:
                // Display current balance
                cout << "Your current balance is: $" << balance << endl;
                break;
            case 2:
                // Prompt user for withdrawal amount
                cout << "Enter amount to withdraw: ";
                cin >> amount;
                // Check if sufficient balance is available
                if (amount <= balance) {
                    balance -= amount;
                    cout << "Withdrawal successful. New balance: $" << balance << endl;
                } else {
                    cout << "Insufficient balance." << endl;
                }
                break;
            case 3:
                // Prompt user for deposit amount
                cout << "Enter amount to deposit: ";
                cin >> amount;
                // Add deposit to balance
                balance += amount;
                cout << "Deposit successful. New balance: $" << balance << endl;
                break;
            case 4:
                // Exit message
                cout << "Thank you for using the ATM. Goodbye!" << endl;
                break;
            default:
                // Handle invalid option
                cout << "Invalid choice. Please try again." << endl;
        }
    } while (choice != 4);  // Loop ends when user selects exit

    return 0;  // End of program
}

 Output

ATM Menu:
1. Check Balance
2. Withdraw
3. Deposit
4. Exit
Enter your choice: 1
Your current balance is: $1000

ATM Menu:
1. Check Balance
2. Withdraw
3. Deposit
4. Exit
Enter your choice: 2
Enter amount to withdraw: 300
Withdrawal successful. New balance: $700

ATM Menu:
1. Check Balance
2. Withdraw
3. Deposit
4. Exit
Enter your choice: 4
Thank you for using the ATM. Goodbye!

Share this C++ Exercise


More C++ Programming Exercises of Advanced Conditional Structures in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.