You are on page 1of 2

1 one:

#include <iostream>
#include <string>

using namespace std;

int main() {
// Hardcoded username and password (replace these with your actual
authentication mechanism)
const string correctUsername = "user";
const string correctPassword = "pass";

string enteredUsername;
string enteredPassword;

// Get user input for username


cout << "Enter username: ";
cin >> enteredUsername;

// Get user input for password


cout << "Enter password: ";
cin >> enteredPassword;

// Check if the entered credentials are correct


if (enteredUsername == correctUsername && enteredPassword == correctPassword) {
cout << "Login successful. Welcome, " << enteredUsername << "!" << endl;
} else {
cout << "Login failed. Incorrect username or password." << endl;
}

return 0;
}

for hidden password:

#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int main() {
const string correctUsername = "user";
const string correctPassword = "pass";

string enteredUsername;
string enteredPassword;

cout << "Enter username: ";


cin >> enteredUsername;

cout << "Enter password: ";


HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));
cin >> enteredPassword;

// Restore console mode


SetConsoleMode(hStdin, mode);

if (enteredUsername == correctUsername && enteredPassword == correctPassword) {


cout << "Login successful. Welcome, " << enteredUsername << "!" << endl;
} else {
cout << "Login failed. Incorrect username or password." << endl;
}

return 0;
}

for unix

<ScrollWheelDown>#include <iostream>
#include <string>
#include <termios.h>
#include <unistd.h>

using namespace std;

int main() {
const string correctUsername = "user";
const string correctPassword = "pass";

string enteredUsername;
string enteredPassword;

cout << "Enter username: ";


cin >> enteredUsername;

cout << "Enter password: ";

termios oldt;
tcgetattr(STDIN_FILENO, &oldt);
termios newt = oldt;
newt.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &newt);

cin >> enteredPassword;

// Restore terminal settings


tcsetattr(STDIN_FILENO, TCSANOW, &oldt);

if (enteredUsername == correctUsername && enteredPassword == correctPassword) {


cout << "Login successful. Welcome, " << enteredUsername << "!" << endl;
} else {
cout << "Login failed. Incorrect username or password." << endl;
}

return 0;
}

You might also like