Username & Password Validation using C++
Posted by Samath
Last Updated: January 05, 2017
  538

This program asks the user for a username and password and checks if the entered values match the values in a constant variable. if the variables match a success message is printed. This program can be used in any program that requires username and password authentication.

#include <iostream> 
#include <string> 

using namespace std;

int main()
{
	const string USERNAME = "user";
	const string PASSWORD = "123456";

	string username, password;
	cout << "Enter Username: ";
	cin >> username;

	if(username.length() < 4)
	{
		cout << "Username length must be atleast 4 characters long.";
	}
	else  
	{
		cout << "Enter Password: ";
		cin >> password;
		if(password.length() < 6)
		{
			cout << "Password length must be atleast 6 characters long.";
		}
		else 
		{
			if(username == USERNAME && password == PASSWORD)
			{
				cout << "User credentials are correct!!!" << endl;
			}
			else
			{
				cout << "Invalid login details" << endl;
			}
		}
	}

	return 0;
}