I am having troubles in the part of entering the password. I can't figure out how I'm going to exclude the backspace key, delete key, enter key, and other special keys from reading it together with the actual password characters.
I have searched around for some answer but I simply don't understand their code.
This uses the non-standard but popular conio.h header (and also manages to get strcmp from somewhere without including the relevant header file, which is definitely not very portable).
Anyway, your question needs refinement. Which is the first line you do not understand?
I only want letters, numbers and special characters to be shown with asterisk for my password. The problem is when you press the backspace, enter, and delete, it still shows asterisk.
And yea, i was rushing when i put the header files. Anyway, that not the problem.
I only want letters, numbers and special characters to be shown with asterisk for my password. The problem is when you press the backspace, enter, and delete, it still shows asterisk.
My suggestion would be not to output the asterisk when the user presses backspace, enter or delete.
cout<<"ENTER USERNAME: ";
cin.getline(userChar,100);
system("cls");
cout<<"ENTER PASSWORD: ";
for (int i=0;;i++) //here is where the password goes
{
passChar[i]=getch();
if (passChar[i]==8)//8 for backspace
i-=2;
if (passChar[i]=='\n')
break;
system("cls");
cout<<"ENTER PASSWORD: ";
for (int j=0; j<i; j++)
cout<<"*";
cout<<endl;
}
now you "handle" every other special characters that you don't want the computer to read.
first things first...
Your character arrays are not initialized.
when you declare
char userChar[100];
It doesn't create a char array with nothing in them... you have to initialize them like so...
for (int i=0; i<100; userChar[i++]=0);
why would you create a new variable called p and do p-=2?
the original code that i wrote...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
cout<<"ENTER USERNAME: ";
cin.getline(userChar,100);
system("cls");
cout<<"ENTER PASSWORD: ";
for (int i=0;;i++) //here is where the password goes
{
passChar[i]=getch();
if (passChar[i]==8)//8 for backspace
i-=2;
if (passChar[i]=='\n')
break;
system("cls");
cout<<"ENTER PASSWORD: ";
for (int j=0; j<i; j++)
cout<<"*";
cout<<endl;
}
was correct.
if you do what you are currently doing, you are simply "hiding" the backspace char and not removing it from the array...
my method effectively method "erases" the character that was typed. You just have to put