Login and Registration System in C++

1. Introduction

Creating a login and registration system in C++ demonstrates handling user data and interactions within an application. This tutorial covers implementing such a system using in-memory storage, focusing on registration, login, and password reset functionalities.

Login and Registration System in C++

2. Program Steps

1. Initialize in-memory storage for user credentials.

2. Define functions for registration, login, and password reset.

3. Implement a simple menu-driven interface for user interaction.

4. Perform basic validation of user inputs.

3. Code Program

#include <iostream>
#include <map>
#include <string>

using namespace std;

// In-memory storage for user credentials
map<string, string> credentials;

// Function to register a new user
void registerUser() {
    string username, password;
    cout << "Enter username: ";
    cin >> username;
    if (credentials.find(username) != credentials.end()) {
        cout << "Username already exists.\n";
        return;
    }
    cout << "Enter password: ";
    cin >> password;
    credentials[username] = password;
    cout << "Registration successful.\n";
}

// Function to login a user
void loginUser() {
    string username, password;
    cout << "Enter username: ";
    cin >> username;
    cout << "Enter password: ";
    cin >> password;
    if (credentials.find(username) != credentials.end() && credentials[username] == password) {
        cout << "Login successful.\n";
    } else {
        cout << "Invalid username or password.\n";
    }
}

// Function to reset a user's password
void resetPassword() {
    string username, newPassword;
    cout << "Enter username: ";
    cin >> username;
    if (credentials.find(username) == credentials.end()) {
        cout << "Username does not exist.\n";
        return;
    }
    cout << "Enter new password: ";
    cin >> newPassword;
    credentials[username] = newPassword;
    cout << "Password reset successful.\n";
}

// Main function to drive the program
int main() {
    int choice;
    do {
        cout << "\n1. Register\n2. Login\n3. Reset Password\n4. Exit\nChoose an option: ";
        cin >> choice;
        switch(choice) {
            case 1:
                registerUser();
                break;
            case 2:
                loginUser();
                break;
            case 3:
                resetPassword();
                break;
            case 4:
                cout << "Exiting program.\n";
                break;
            default:
                cout << "Invalid choice. Please try again.\n";
        }
    } while(choice != 4);
    return 0;
}

Output:

1. Register
2. Login
3. Reset Password
4. Exit
Choose an option: 1
Enter username: Ramesh
Enter password: password
Registration successful.
1. Register
2. Login
3. Reset Password
4. Exit
Choose an option: 2
Enter username: Ramesh
Enter password: password
Login successful.
1. Register
2. Login
3. Reset Password
4. Exit
Choose an option: 2
Enter username: Ramesh
Enter password: wrong
Invalid username or password.
1. Register
2. Login
3. Reset Password
4. Exit
Choose an option: 3
Enter username: Ramesh
Enter new password: password123
Password reset successful.
1. Register
2. Login
3. Reset Password
4. Exit
Choose an option: 2
Enter username: Ramesh
Enter password: password123
Login successful.
1. Register
2. Login
3. Reset Password
4. Exit
Choose an option: 4
Exiting program.

Explanation:

1. In-Memory Storage: A map named credentials stores username-password pairs, simulating a database in a simple manner.

2. Registration: registerUser allows new users to create an account by entering a unique username and password. The function checks if the username already exists to prevent duplicates.

3. Login: loginUser checks the credentials map for the entered username and password, granting access if they match an existing entry.

4. Password Reset: resetPassword enables users to change their password for an existing account, demonstrating a basic account management feature.

5. User Interface: The program presents a menu-driven interface using a loop. This interface enables users to select actions like registering, logging in, or resetting their password. The loop continues until the user chooses to exit.

6. Validation: Basic input validation ensures that users cannot register with an existing username and that login attempts must match stored credentials.

This tutorial offers a foundation for building more complex applications requiring user management in C++.

Comments