ATM machine program in c++ using functions

Table of Contents

Source Code

#include <iostream>
#include <string>

using namespace std;

// Function to handle user login
bool login() {
    string username, password;

    cout << "Enter username: ";
    cin >> username;
    cout << "Enter password: ";
    cin >> password;

    // Verify username and password against a database or file
    // Return true if login is successful, false otherwise
    if(username=="admin" && password=="password")
      return true;
    else
      return false;
}

// Function to check account balance
void check_balance() {
    // Get account balance from a database or file
    int balance = 1000;
    cout << "Your current balance is: $" << balance << endl;
}

// Function to handle money withdrawal
void withdraw() {
    int amount;
    cout << "Enter amount to withdraw: $";
    cin >> amount;

    // Check if the entered amount is valid and there are sufficient funds
    if (amount > 0 && amount <= 1000) {
        // Update account balance in a database or file
        cout << "Please collect your cash." << endl;
        cout << "Your current balance is: $" << (1000-amount) << endl;
    } else {
        cout << "Invalid amount entered." << endl;
    }
}

// Function to handle money deposit
void deposit() {
    int amount;
    cout << "Enter amount to deposit: $";
    cin >> amount;

    // Check if the entered amount is valid
    if (amount > 0) {
        // Update account balance in a database or file
        cout << "Deposit accepted." << endl;
        cout << "Your current balance is: $" << (amount+1000) << endl;
    } else {
        cout << "Invalid amount entered." << endl;
    }
}

// Function to handle changing the ATM pin
void change_pin() {
    int current_pin, new_pin;

    cout << "Enter current pin: ";
    cin >> current_pin;
    cout << "Enter new pin: ";
    cin >> new_pin;

    // Verify current pin against a database or file
    // Update pin in a database or file if current pin is correct
    if (current_pin == 1234) {
        cout << "Pin changed successfully." << endl;
    } else {
        cout << "Incorrect pin entered." << endl;
    }
}

// Function to handle viewing transaction history
void transaction_history() {
    // Retrieve transaction history from a database or file
    cout << "Transaction history:" << endl;
    cout << "Date \t\t Amount \t\t Balance" << endl;
    cout << "01/01/2022 \t $100 \t\t $900" << endl;
    cout << "01/02/2022 \t $50 \t\t $850" << endl;
}

Code Explanation

An ATM machine management system in C++ could involve the following basic functions:

  1. login(): This function would handle user authentication and ensure that only authorized users are able to access the ATM machine.
  2. check_balance(): This function would allow users to check their account balance.
  3. withdraw(): This function would allow users to withdraw money from their account.
  4. deposit(): This function would allow users to deposit money into their account.
  5. change_pin(): This function would allow users to change their ATM pin.
  6. transaction_history(): This function would allow users to view their transaction history.
  7. logout(): This function would handle user logout and ensure that the user session is terminated properly.
  8. main(): This function would handle the overall flow of the program and call the appropriate functions based on user input.

It’s important to consider the security and the error handling of the system.