0% found this document useful (0 votes)
6 views2 pages

3.write A Program For The Printed Passwords

This document contains a C++ program that prompts the user for a username and password. The password input is masked with asterisks as the user types, and it allows for backspace functionality to delete characters. Finally, the program displays the entered password for testing purposes.

Uploaded by

Reuben Anthony
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

3.write A Program For The Printed Passwords

This document contains a C++ program that prompts the user for a username and password. The password input is masked with asterisks as the user types, and it allows for backspace functionality to delete characters. Finally, the program displays the entered password for testing purposes.

Uploaded by

Reuben Anthony
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Write a Program for the printed passwords

#include <iostream>
#include <stdio.h> // Needed for getchar()
using namespace std;

enum IN {
IN_BACK = 8,
IN_RET = 13
};

string takePasswdFromUser(char sp = '*') {


string passwd = "";
char ch_ipt;

while (true) {
ch_ipt = getchar(); // Fixed: getchar() requires <stdio.h>

if (ch_ipt == IN::IN_RET) {
cout << endl;
return passwd;
} else if (ch_ipt == IN::IN_BACK && !passwd.empty()) {
passwd.pop_back();
cout << "\b \b"; // Erase the last '*' from the console
continue;
} else if (ch_ipt == IN::IN_BACK && passwd.empty()) {
continue;
}

passwd.push_back(ch_ipt);
cout << sp; // Print '*' instead of the actual character
}
}

int main() {
string name;
string input;

cout << "Enter the Username: ";


cin >> name;

cout << "Enter the Password: ";


input = takePasswdFromUser(); // Call function to take password input

cout << "\nEntered Password: " << input << endl; // Display password for
testing

return 0;
}

You might also like