ATM using file handling in C++
In this article, the task is to implement an ATM with functions like add, delete, search, and update users using file handling in C++.
File handling:
- File handling is used to store the output of a program in a file.
- In C++, files use three classes fstream, ifstream, ofstream available in fstream header file for file handling.
Some Important Instructions:
- The password for Admin login is 1234.
- Initially, the file is empty make sure to first log in as admin and add a user, and then login as a user.
Approach:
- Select user type and input password.
- In the given menu, select the basic operation and enter the details in the operations like add, delete, search and update users.
- After performing all the functions and operations, exit from the ATM system.
- All the file operations are done in a file named aaa.txt, where the data is written using ofstream mode and can be read using ifstream mode.
- After this, the file must be closed using <filename>.close().
Below is the implementation of the above approach:
C++
// C++ code to implement an ATM and // its basic functions #include <fstream> #include <iostream> #include <limits> #include <string.h> #include <unistd.h> using namespace std; // Class ATM to get user details class atm { private : char username[30]; int password; int balance; public : char * usernames( void ) { // Return username return the username; } int passwords( void ) { // Return the password return password; } // Function to get the data void getData( void ) { cin.ignore( numeric_limits<streamsize>::max(), '\n' ); cout << "\nEnter username:" ; cin.getline(username, 30); cout << "\nEnter 4-digit " << "password:" ; cin >> password; cin.ignore( numeric_limits<streamsize>::max(), '\n' ); cout << "\nEnter initial" << " balance:" ; cin >> balance; cin.ignore( numeric_limits<streamsize>::max(), '\n' ); } // Function displaying the data void showData( void ) { cout << "Username:" << username << ", Password:" << password << ", Balance:" << balance << endl; } // Member Functions int adduser(); int viewallusers(); int deleteuser( char *); void updateuserasdeposit( char *); void updateuseraswithdraw( char *); int searchspecificuser( char *, int ); int searchallusertodisplay( char *); }; // Function to implement functionality // of ATM User void atmUser() { atm a; char uname[30]; int pass, ch, ch1, ch2, found = 0; mainmenu: // System("cls"); cout << "\nWelcome to w3wiki ATM" ; cout << "\nLogin as :\n1. Admin\n2." << " User\n3. " "Exit\nChoose one : " ; cin >> ch; switch (ch) { case 1: rerun: // System("cls"); cout << "\nEnter details to" << " login as Admin\n" ; cout << "\nEnter password:" ; cin >> pass; if (pass == 1234) { admin: // System("cls"); cout << "\nWelcome to" << " Admin Menu" ; cout << "\n1. Add User\n2." << " Delete User\n3. " "View All User\n4. " << "Exit" ; cout << "\nSelect one : " ; cin >> ch1; switch (ch1) { case 1: a.adduser(); goto admin; case 2: cout << "\nEnter the " << "Username to be " "deleted : " ; cin.ignore( numeric_limits<streamsize>::max(), '\n' ); cin.getline(uname, 30); a.deleteuser(uname); goto admin; case 3: a.viewallusers(); // sleep(4); goto admin; case 4: break ; } } else { cout << "\nDetails are " << "incorrect ! Please" " try again" ; cin.get(); goto rerun; } goto mainmenu; case 2: // System("cls"); cout << "\n Enter details to" << " login as User\n" ; cin.ignore( numeric_limits<streamsize>::max(), '\n' ); cout << "Enter username:" ; cin.getline(uname, 30); cout << "\nEnter password:" ; cin >> pass; found = a.searchspecificuser( uname, pass); if (found) { user: // System("cls"); cout << "\nWelcome " << uname; cout << "\nWelcome to" << " User Menu" ; cout << "\n1. Deposit\n2." << " Withdraw\n3. View " "Account\n4. " << "Exit\nEnter your choice:" ; cin >> ch2; switch (ch2) { case 1: a.updateuserasdeposit(uname); goto user; case 2: a.updateuseraswithdraw(uname); goto user; case 3: a.searchallusertodisplay(uname); // sleep(4); goto user; case 4: cout << "Thank you" ; break ; } } else { cout << "\nNo account found" << " with username " ":(\n\nHit ENTER to continue " << uname; cin.get(); } goto mainmenu; case 3: cout << "\nThankyou for " << "banking with " << "w3wiki" ; cin.get(); break ; } } // Function to add user int atm::adduser() { atm a; ofstream file; // Open file in write mode file.open( "aaa.txt" , ios::out | ios::app); if (!file) { cout << "Error in creating " << "file.." << endl; return 0; } // Read from user a.getData(); // Write into file file.write(( char *)&a, sizeof (a)); // Close the file file.close(); return 0; } // View Users int atm::viewallusers() { atm a; ifstream file1; // Open file in read mode file1.open( "aaa.txt" , ios::in); if (!file1) { cout << "Error in opening file.." ; return 0; } // Read data from file file1.read(( char *)&a, sizeof (a)); while (!file1.eof()) { // Display data on monitor a.showData(); file1.read(( char *)&a, sizeof (a)); } // Close the file file1.close(); return 0; } // Function to delete the user int atm::deleteuser( char * uname) { atm a; fstream original, temp; original.open( "aaa.txt" , ios::in); if (!original) { cout << "\nfile not found" ; return 0; } else { temp.open( "temp.txt" , ios::out | ios::app); original.read(( char *)&a, sizeof (a)); // Till end of file is reached while (!original.eof()) { if (! strcmp (a.usernames(), uname)) { cout << "data found " << "and deleted\n" << a.username << "\n" ; } else { temp.write(( char *)&a, sizeof (a)); } original.read(( char *)&a, sizeof (a)); } original.close(); temp.close(); remove ( "aaa.txt" ); rename ( "temp.txt" , "aaa.txt" ); a.viewallusers(); } return 0; } // Function to update user by // depositing money void atm::updateuserasdeposit( char * uname) { atm a; fstream file, temp; file.open( "aaa.txt" , ios::in | ios::out | ios::ate); temp.open( "temp.txt" , ios::out | ios::app); file.seekg(0); file.read(( char *)&a, sizeof (a)); // Till end of the file while (!file.eof()) { if (! strcmp (a.usernames(), uname)) { int b; cout << "\nEnter amount " << "to deposit:" ; cin >> b; a.balance = a.balance + b; cout << "\nBalance is:" << a.balance; temp.write(( char *)&a, sizeof (a)); } else { temp.write(( char *)&a, sizeof (a)); } file.read(( char *)&a, sizeof (a)); } file.close(); temp.close(); remove ( "aaa.txt" ); rename ( "temp.txt" , "aaa.txt" ); } // Function to update user by // depositing withdrawing money void atm::updateuseraswithdraw( char * uname) { atm a; fstream file, temp; file.open( "aaa.txt" , ios::in | ios::out | ios::ate); temp.open( "temp.txt" , ios::out | ios::app); file.seekg(0); file.read(( char *)&a, sizeof (a)); // Till end of file is reached while (!file.eof()) { if (! strcmp (a.usernames(), uname)) { int b; cout << "\nEnter amount " << "to withdraw:" ; cin >> b; if (a.balance < b) { cout << "\nInsufficient " << "balance to withdraw" ; } else { a.balance = a.balance - b; temp.write(( char *)&a, sizeof (a)); cout << "\nBalance is:" << a.balance; } } else { temp.write(( char *)&a, sizeof (a)); } file.read(( char *)&a, sizeof (a)); } // Close the file file.close(); temp.close(); remove ( "aaa.txt" ); rename ( "temp.txt" , "aaa.txt" ); } // Search user int atm::searchspecificuser( char * uname, int pass) { atm a; fstream f; // Open the file f.open( "aaa.txt" , ios::in); if (!f) { cout << "Error in opening file.." ; return 0; } // Read data from file f.read(( char *)&a, sizeof (a)); while (!f.eof()) { if (! strcmp (a.usernames(), uname)) { if (a.passwords() == pass) { return 1; } } f.read(( char *)&a, sizeof (a)); } // Close the file f.close(); return 0; } // Search specific user int atm::searchallusertodisplay( char * uname) { atm a; fstream file1; // Open the file file1.open( "aaa.txt" , ios::in); if (!file1) { cout << "Error in opening file.." ; return 0; } // Read data from file file1.read(( char *)&a, sizeof (a)); while (!file1.eof()) { if (! strcmp (a.usernames(), uname)) { a.showData(); return 0; } file1.read(( char *)&a, sizeof (a)); } // Close the file file1.close(); return 0; } // Driver Code int main() { // Function Call atmUser(); return 0; } |
Output:
Contact Us