Introduction
In this tutorial, we will build a simple login and registration system using the C++ programming language. This system will allow users to register with their username and password and then log in using those credentials. The basic authentication system will be implemented using simple file handling and condition checking in C++.
Objective
The main objective of this program is to provide a basic understanding of user authentication. You will learn how to store user information in a file, verify login details, and implement the functionality of registration and login. This program will be built in C++ to help beginners understand how to manage user authentication using files.
Code Implementation
#include #include #include using namespace std; void registerUser() { string username, password; ofstream outFile("users.txt", ios::app); cout << "Enter username: "; cin >> username; cout << "Enter password: "; cin >> password; outFile << username << " " << password << endl; outFile.close(); cout << "Registration Successful!" << endl; } bool loginUser() { string username, password, fileUsername, filePassword; cout << "Enter username: "; cin >> username; cout << "Enter password: "; cin >> password; ifstream inFile("users.txt"); while (inFile >> fileUsername >> filePassword) { if (fileUsername == username && filePassword == password) { inFile.close(); return true; } } inFile.close(); return false; } int main() { int choice; while (true) { cout << "\n1. Register\n2. Login\n3. Exit\n"; cout << "Enter your choice: "; cin >> choice; if (choice == 1) { registerUser(); } else if (choice == 2) { if (loginUser()) { cout << "Login Successful!" << endl; } else { cout << "Invalid credentials!" << endl; } } else if (choice == 3) { cout << "Exiting program..." << endl; break; } else { cout << "Invalid choice! Please try again." << endl; } } return 0; }
Explanation of the Program
This program is designed to be simple and functional, divided into different sections for registration and login:
- registerUser(): This function is used to register a new user. It takes the username and password input from the user and stores it in a text file named
users.txt
. - loginUser(): This function is used to log in the user. It takes the username and password from the user and checks if these match any entry in the
users.txt
file. - main(): The main function provides the user interface. It displays options for the user to either register, log in, or exit. Based on the user’s choice, the corresponding function is called.
How to Run the Program
To run the program:
-
- Make sure you have a C++ compiler installed (e.g., GCC, Code::Blocks, etc.).
- Create a new file and copy the provided code into it. Save the file with a .cpp extension (e.g.,
auth_system.cpp
). - Compile the program using your C++ compiler. For example, if using GCC, run the following command in the terminal:
g++ auth_system.cpp -o auth_system
-
- Run the compiled program using the command:
./auth_system
- The program will prompt you to register or log in. Follow the on-screen instructions to use the system.