C++ Code Example: simple login

This code implements a simple authentication mechanism. The program takes the username and password inputs from the user, and then checks if the inputted username and password match the predefined values of “user” and “123456” respectively. If the inputted values match, the program outputs “Successfully logged in”. If not, it outputs “Wrong username or password”.

#include <iostream>
using namespace std;

bool checkUserInput(string username, string password) {
    string pw = "123456", name = "user";

    if (username.compare(name) == 0 && password.compare(pw) == 0) {
        return true;
    } else {
        return false;
    }
}

int main() {
    string username, password;

    cout << "Enter username: " << endl;
    cin >> username;
    cout << "Enter password: " << endl;
    cin >> password;

    if (checkUserInput(username, password)) {
        cout << "Successfully logged in";
    } else {
        cout << "Wrong username or password";
    }

    return 0;
}
Output first run
Enter username: 
user
Enter password: 
123456
Successfully logged in
Output second run
Enter username: 
jason
Enter password: 
3453
Wrong username or password