0% found this document useful (0 votes)
2 views5 pages

Code Docs

atm code on c++
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Code Docs

atm code on c++
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <iostream>

#include <iomanip>
#include <string>
#include <sstream>
using namespace std;

struct Account {
string username;
string password;
double balance; //kaya sya double is for decimal points
};

– a function to format the balance into thousands separators 000,000,000.00


string formatWithCommas(double value) {
stringstream ss; ​ – stringsteam treats number into text; ss is a variable name
ss << fixed << setprecision(2) << value;
string result = [Link](); ​ – converts the stringstream into string

int insertPos = [Link]('.') - 3; // Find the position 3 digits before the decimal point
while (insertPos > 0) {
[Link](insertPos, ","); ​ – insert commas
insertPos -= 3; // Move 3 digits back
}

return result;
}

int main() {
Account acc;
int choice;
string enteredPin;
bool loggedIn = false;
string createAcc; // for yes/no
int attempts = 0; // Keep track of the number of attempts
const int maxAttempts = 3; ​ – max number of attempts
double dep;

cout << "==============================\n";


cout << " ATM \n";
cout << "==============================\n\n";

// Ask if the user wants to create an account

cout << "Do you want to create an account? (yes/no): ";


cin >> createAcc;

// convert input to lowercase for easier comparison


for (auto &c : createAcc) c = tolower(c);

if (createAcc == "yes") {
[Link](); // clear leftover newline
cout << "\n--- ACCOUNT CREATION ---\n";

cout << "Enter your name: ";


getline(cin, [Link]);

// PIN validation
do {
cout << "Set a 4-digit PIN: ";
cin >> [Link];
if ([Link]() != 4 || !isdigit([Link][0]) || !isdigit([Link][1]) ||
!isdigit([Link][2]) || !isdigit([Link][3])) {
cout << "Invalid PIN! Please enter exactly 4 digits (e.g., 0000).\n";
}
} while ([Link]() != 4 || !isdigit([Link][0]) || !isdigit([Link][1]) ||
!isdigit([Link][2]) || !isdigit([Link][3]));
​ – it keeps looping if the input is still invalid

// Deposit validation
do {
cout << "Enter initial deposit: ";
cin >> [Link];
if ([Link] < 0)
cout << "Deposit cannot be negative. Try again.\n";
} while ([Link] < 0);

cout << "\nAccount created successfully!\n";


cout << "Welcome, " << [Link] << "!\n";
}
else {
cout << "You must create an account first to use this ATM.\n";
return 0;
}

// Login section
cout << "\n--- LOGIN ---\n";

// Allow up to 3 attempts to enter the correct PIN


while (attempts < maxAttempts) {
cout << "Enter PIN: ";
cin >> enteredPin;

if (enteredPin == [Link]) {
loggedIn = true;
cout << "Login successful!\n";
break; // Exit the loop if login is successful
} else {
attempts++;​ – +1 in attempts if you entered wrong input
if (attempts < maxAttempts) {
cout << "Incorrect PIN. You have " << maxAttempts - attempts << " attempts left.\n";
}
}
}

// If the user failed to log in after 3 attempts


if (!loggedIn) {
cout << "Incorrect PIN entered 3 times. Exiting...\n";
return 0; // Exit the program
}

// Main menu loop


while (loggedIn) {
cout << "\n===== ATM MENU =====\n";
cout << "1. Check Balance\n";
cout << "2. Deposit\n";
cout << "3. Withdraw\n";
cout << "4. Exit\n";
cout << "Choose option: ";
cin >> choice;

[Link](locale(""));

switch (choice) {
case 1: {
// Manually format the balance with commas
string formattedBalance = formatWithCommas([Link]); ​ –
formatWithCommas([Link]) is the function from the first part
cout << "Your current balance is: Php " << formattedBalance << endl;
break;
}

case 2: {
cout << "Your current balance is: Php " << formatWithCommas([Link]) << endl;

// Enter deposit amount


cout << "Enter amount to deposit: ";
cin >> dep;

if (dep > 0) {
[Link] += dep; // Adds deposit

// Format and display the deposit and new balance


cout << "Deposit successful. Amount deposited: Php "
<< formatWithCommas(dep) << ". New balance: Php "
<< formatWithCommas([Link]) << endl;
} else {
cout << "Invalid amount. Deposit must be positive.\n";
}
break;
}

case 3: {
double withdraw;
cout << "Your current balance is: Php " << formatWithCommas([Link]) << endl;

// Enter withdrawal amount


cout << "Enter amount to withdraw: ";
cin >> withdraw;

if (withdraw > [Link]) {


cout << "Insufficient balance.\n";
} else if (withdraw <= 0) {
cout << "Invalid amount.\n";
} else {
[Link] -= withdraw; // Subtracts withdrawn amount

// Format and display the withdrawal and remaining balance


cout << "Withdrawal successful. Amount withdrawn: Php "
<< formatWithCommas(withdraw) << ". Remaining balance: Php "
<< formatWithCommas([Link]) << endl;
}
break;
}

case 4:
cout << "Thank you for using the ATM System!\n";
loggedIn = false; // exit the while loop
break;

default:
cout << "Invalid choice. Try again.\n";
}
}

return 0;
}

You might also like