-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathATM.cpp
More file actions
109 lines (94 loc) · 2.89 KB
/
Copy pathATM.cpp
File metadata and controls
109 lines (94 loc) · 2.89 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
using namespace std;
// Function prototypes
void displayMenu();
void checkBalance(double balance);
double deposit(double balance);
double withdraw(double balance);
int main() {
// Initial variables
const int PIN = 1234; // Preset PIN for simplicity
int enteredPin;
double balance = 1000.00; // Initial balance
int attempts = 0;
int maxAttempts = 3;
int choice;
// PIN verification
while (attempts < maxAttempts) {
cout << "Enter your 4-digit PIN: ";
cin >> enteredPin;
if (enteredPin == PIN) {
cout << "\nPIN verified successfully!\n";
do {
// Display ATM menu
displayMenu();
cout << "\nChoose an option: ";
cin >> choice;
switch (choice) {
case 1:
checkBalance(balance);
break;
case 2:
balance = deposit(balance);
break;
case 3:
balance = withdraw(balance);
break;
case 4:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice! Please try again.\n";
}
} while (choice != 4); // Loop until the user chooses to exit
break;
} else {
attempts++;
cout << "Incorrect PIN. Attempts remaining: " << maxAttempts - attempts << endl;
}
if (attempts == maxAttempts) {
cout << "Maximum attempts reached. Exiting program.\n";
}
}
return 0;
}
// Function to display ATM menu
void displayMenu() {
cout << "\n=== ATM MENU ===";
cout << "\n1. Check Balance";
cout << "\n2. Deposit";
cout << "\n3. Withdraw";
cout << "\n4. Exit";
}
// Function to check balance
void checkBalance(double balance) {
cout << "Your current balance is: $" << balance << endl;
}
// Function to deposit money
double deposit(double balance) {
double depositAmount;
cout << "Enter deposit amount: $";
cin >> depositAmount;
if (depositAmount > 0) {
balance += depositAmount;
cout << "Deposit successful! New balance: $" << balance << endl;
} else {
cout << "Invalid amount!\n";
}
return balance;
}
// Function to withdraw money
double withdraw(double balance) {
double withdrawAmount;
cout << "Enter withdraw amount: $";
cin >> withdrawAmount;
if (withdrawAmount > 0 && withdrawAmount <= balance) {
balance -= withdrawAmount;
cout << "Withdrawal successful! New balance: $" << balance << endl;
} else if (withdrawAmount > balance) {
cout << "Insufficient funds!\n";
} else {
cout << "Invalid amount!\n";
}
return balance;
}