Password C++ Task

I have been tasked with setting up a password feature to access a database, while this is still very early stages I thought I would share the source code behind this simple login section. All it does at the moment is asked for a password with 3 attempts. The password is set to 1234 but you can change the password by editing this line:

if (pwset.PasswordEntry == "1234")

Because this is still very early stages it does not do much but it seems to be a good start.

#include <iostream>
#include <string>

class PasswordSet 
{
public:
	bool access = false;
	std::string PasswordEntry;
};

void PwLogin() {
	PasswordSet pwset;
	std::cout << "Please enter your Password" << std::endl;

	int PwLoop = 0;
	int PwCounter = 3;

	while (PwLoop<3)
	{
		std::cin >> pwset.PasswordEntry;

		if (pwset.PasswordEntry == "1234")
		{
			std::cout << "Login Success" << std::endl;
			pwset.access = true;
			exit(0);
		}

		else if (PwLoop >= 2)
		{
			exit(1);
		}

		else
		{
			PwLoop++;
			PwCounter--;
			printf("Please try again %d attempts remaining\n", PwCounter);
		}
	}
}

void PWFunction()
{
	std::string password;

	PasswordSet pwset;
	if (pwset.access == false)
	{
		PwLogin();
	}
	else 
	{
	printf("Already Logged in");
	}
}


int main(){
	PWFunction();
}

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *