Login Authentication Program in C++

  • C++
  • 1 min read

Here is an example of a login authentication program in C++ using a dictionary object to store usernames and passwords.

Example: C++ Authentication Program

The following C++ program using the map function to create a dictionary object and using the map's find method to check whether the key exists or not.

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
    // create your dictonary
    map<string, string>dict = { {"david", "123"} };
    string username, password;
    cout<<"Enter username: "<<endl; cin>>username;
    cout<<"Enter password: "<<endl; cin>>password;
    // Check if provided username and password matches with the one is dictonary
    if(dict.find(username) != dict.end() && dict[username] == password) {
        cout<<"Login Successfully!";
    } else {
        cout<<"Invalid Credentials";
    }
    return 0;
}

Output:

Enter username: 
david
Enter password: 
123
Login Successfully!