• September 22, 2021

PHSARIT

Bring Technology Ideas with you

 How to create simple login using C++?

As the previous post What is a function in C++ programming language?, we have create the simple login using function Void. This tutorial is working on allowing the users to input the values in for login in to our system.

Create Void Login as below:

void Login()
{
	//assign value into username and password;
	cout << "Welcome to my system" << endl;
	cout << "------------------"<< "Please login" << "----------" << endl;
	string username;
	string password;
	//user input value
	cout << "Please enter your username:";
	cin >> username;
	cout << "Please enter password:";
	cin >> password;
	//check username and password
	if (username == "admin") // true or false
	{
		//username is correct, check password
		if (password == "admin") // true or false
		{
			//check password is correct, message login
			cout << "Login successfully";
		}
		else
		{
			//password wrong
			cout << "Password does not match!";
		}
	} 
	else
	{	//username wrong
		cout << "Username does not match";
	}
}

** In order to allow Console In work in the code the above, it needs to include “#include <string>” in the top of your code:

Implement in the function in main as:

int main()
{
	Login();
	cout << endl;
	system("pause==0");
}

As the full code of the login:

#include<iostream>
#include<string>
using namespace std;
void Login()
{
	//assign value into username and password;
	cout << "Welcome to my system" << endl;
	cout << "------------------"<< "Please login" << "----------" << endl;
	string username;
	string password;
	//user input value
	cout << "Please enter your username:";
	cin >> username;
	cout << "Please enter password:";
	cin >> password;
	//check username and password
	if (username == "admin") // true or false
	{
		//username is correct, check password
		if (password == "admin") // true or false
		{
			//check password is correct, message login
			cout << "Login successfully";
		}
		else
		{
			//password wrong
			cout << "Password does not match!";
		}
	} 
	else
	{	//username wrong
		cout << "Username does not match";
	}
}
int main()
{
	
	Login();
	cout << endl;
	system("pause==0");
}

See the result of the code the above: the correct username and password

    result of correct both username and password

    See the wrong username or password

      right username but wrong password
        Wrong username but right password

        *** you can use char* as the datatype as well.

        Watch our video:

        Related post