I have recently been developing a program incorporating all the theory we learnt in the first year of university & it is going really well. However, I have a problem with my username & password code (something we never did in my first year).
I have a couple of minor problems but the only one I think I need help with is getting the password characters to all display as "*", in order to maintain privacy. I would like them to behave like actual characters so, for example, if I pressed backspace, I would want it to delete the last character & not display another asterisks.
I have not been able to find a solution to my problem anywhere. So please may somebody help?
I'd rather not change the whole code of my username & password function as I am happy with it at the moment, so would somebody please help me with how to implement this within my current code?
If I have to change the username & password function then I am willing to do so. Also, would anybody like me to display more of my code on this matter?
The following is the function which allows the user to enter the password (but not the whole of my username & password code as that is scattered around the program). I would assume it would have something to do with this line:
cin >> password;
The whole function works perfectly as it should, I'd just like the password to be displayed with asterisk's :)
Here is my code anyway:
void authorisation(int& keyPressed, int& currentMonth, int& currentYear)
{
int getKeyPress();
void displayHeaderFooter( int& currentMonth, int& currentYear);
void loginSuccess(int& keyPressed, int& currentMonth, int& currentYear);
CLEAR_SCREEN();
displayHeaderFooter(currentMonth, currentYear);
ifstream Passfile("password.txt", ios::in);
Passfile >> inputPassword;
ifstream Userfile("username.txt", ios::in);
Userfile >> inputUsername;
Gotoxy(24, 12);
SelectTextColour( clCyan);
cout << "Please enter your username.";
Gotoxy(42, 10);
cin >> username;
Gotoxy(24, 13);
SelectTextColour( clDarkCyan);
cout << "Please enter your password.";
Gotoxy(42, 11);
cin >> password;
Userfile.close();
Passfile.close();
userHeader = 1;
while (username == inputUsername && password == inputPassword)
{
if (userHeader == 1)
{
Gotoxy(0, 0);
SelectTextColour( clGrey);
SelectBackColour( clDarkCyan);
cout << " " << username << " ";
}
Gotoxy(23, 18);
SelectTextColour( clGreen);
SelectBackColour( clBlack);
cout << "You have logged in successfully.";
Gotoxy(15, 19);
cout << "Please press the 'M' key to display the Main Menu.";
loginSuccess(keyPressed, currentMonth, currentYear);
}
if (username != inputUsername || password != inputPassword)
{
Gotoxy(16, 18);
SelectTextColour( clRed);
cout << "\a";
cout << "The username or password you entered is invalid.";
Gotoxy(21, 19);
cout << "Please press the 'L' key to try again.";
keyPressed = getKeyPress();
if (keyPressed == 'L')
{
authorisation(keyPressed, currentMonth, currentYear);
}
else
{
Gotoxy(16, 18);
SelectTextColour( clRed);
cout << "\a";
cout << " " << "The command you entered is invalid." << " ";
Gotoxy(21, 19);
cout << " " << "Please press the 'L' key to login." << " ";
}
}
}
Thank you in advance. I would be very grateful for any help :)
Question
Calum Veteran
I have recently been developing a program incorporating all the theory we learnt in the first year of university & it is going really well. However, I have a problem with my username & password code (something we never did in my first year).
I have a couple of minor problems but the only one I think I need help with is getting the password characters to all display as "*", in order to maintain privacy. I would like them to behave like actual characters so, for example, if I pressed backspace, I would want it to delete the last character & not display another asterisks.
I have not been able to find a solution to my problem anywhere. So please may somebody help?
I'd rather not change the whole code of my username & password function as I am happy with it at the moment, so would somebody please help me with how to implement this within my current code?
If I have to change the username & password function then I am willing to do so. Also, would anybody like me to display more of my code on this matter?
The following is the function which allows the user to enter the password (but not the whole of my username & password code as that is scattered around the program). I would assume it would have something to do with this line:
The whole function works perfectly as it should, I'd just like the password to be displayed with asterisk's :)
Here is my code anyway:
Thank you in advance. I would be very grateful for any help :)
Edited by cJr.Link to comment
https://www.neowin.net/forum/topic/668608-c-displaying-asterisks-for-password-characters/Share on other sites
8 answers to this question
Recommended Posts