ATM Transaction Logger in C++

In this exercise, you will develop a simple ATM simulator in C++ that logs every transaction to a file. Logging is a vital component in real-world applications, especially in banking systems, where it's necessary to keep track of all operations for auditing and troubleshooting. This program allows users to simulate deposits and withdrawals and writes each transaction, along with a timestamp and transaction type, to a log file. The exercise will help you practice file handling, user input, and basic logic for account operations.

Group

File Management in C++

Objective

1. Create a program that simulates a simple ATM with options to deposit or withdraw money.
2. For each operation, write a log entry into a file called "atm_log.txt".
3. Each log entry should include the operation type, the amount, and a timestamp.
4. Use conditionals to ensure the balance does not go negative during withdrawal.
5. Display the current balance after each operation.

Develop a program that logs ATM operations into a log file.

Example C++ Exercise

 Copy C++ Code
#include <iostream>       // For standard input and output
#include <fstream>        // For file operations
#include <ctime>          // For timestamp generation
using namespace std;

int main() {
    ofstream logFile("atm_log.txt", ios::app); // Open file in append mode
    double balance = 1000.0; // Initial balance
    int choice;
    double amount;

    // Check if the file opened successfully
    if (!logFile.is_open()) {
        cout << "Error: Could not open log file." << endl;
        return 1;
    }

    // Display ATM menu
    cout << "ATM Simulator" << endl;
    cout << "1. Deposit" << endl;
    cout << "2. Withdraw" << endl;
    cout << "Enter your choice: ";
    cin >> choice;

    // Get current time
    time_t now = time(0);
    char* dt = ctime(&now); // Convert time to string

    if (choice == 1) {
        // Deposit operation
        cout << "Enter amount to deposit: ";
        cin >> amount;
        balance += amount; // Update balance
        logFile << "Deposit: +" << amount << " | Time: " << dt; // Log transaction
        cout << "Deposit successful. Current balance: $" << balance << endl;
    } else if (choice == 2) {
        // Withdrawal operation
        cout << "Enter amount to withdraw: ";
        cin >> amount;
        if (amount > balance) {
            // Check for insufficient funds
            cout << "Insufficient funds." << endl;
            logFile << "Withdrawal failed: $" << amount << " | Reason: Insufficient funds | Time: " << dt;
        } else {
            balance -= amount; // Deduct amount from balance
            logFile << "Withdrawal: -" << amount << " | Time: " << dt; // Log transaction
            cout << "Withdrawal successful. Current balance: $" << balance << endl;
        }
    } else {
        // Invalid option
        cout << "Invalid option." << endl;
    }

    logFile.close(); // Close the file
    return 0; // Exit the program
}

 Output

//Output Example:
ATM Simulator
1. Deposit
2. Withdraw
Enter your choice: 1
Enter amount to deposit: 300
Deposit successful. Current balance: $1300

//atm_log.txt content after operation:
Deposit: +300 | Time: Wed Apr 16 14:55:30 2025

Share this C++ Exercise


More C++ Programming Exercises of File Management 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++.

  • Read and Display a Text File Using C++

    In this C++ exercise, you will create a program that reads the contents of a text file and prints them to the console. This task demonstrates how to work with file input streams in...

  • Register User Information and Save It to a File in C++

    In this C++ exercise, you will create a program that collects basic user information—specifically, their name and age—and writes it into a text file. This task introduces you to fi...

  • Count Words in a Text File Using C++

    In this exercise, you will write a C++ program that reads a text file and counts the total number of words contained in it. This task will help you become familiar with file input ...

  • Copy File Contents Using C++

    This exercise involves writing a C++ program to copy the entire content of one file into another. By completing this task, you will practice using file streams in C++, specifically...

  • Find and Replace a Word in a File Using C++

    In this exercise, you will create a C++ program that searches for a specific word in a text file and replaces it with another word. This task will help you strengthen your understa...

  • Save and Load a List of Numbers from a File in C++

    In this exercise, you will create a C++ program that allows the user to input a list of numbers, save them to a text file, and then read and display those numbers from the file. Th...