I recently made a program which uses getch()
to mask the password entered. But it gives access as soon as the correct code is entered without waiting for enter key to be pressed. what changes should be done? Also, is getch()
allowed in Ubuntu? If not, what alternative is to be used?
My code looks like this. I already gave default password in a different function.
char pass[4];
cout << "\nEnter Administrator Password: ";
for (i = 0; i < 4; i++)
{
pass[i] = getch();
cout << "*";
}
for (i = 0; i < 4; i++)
{
if(admin_pass[i] == pass[i])
return 1;
else
return 0;
}
You can use the getpass()
as follows,
#include<stdio.h>
#include<unistd.h>
#include<string.h>
char *pass=getpass("\nEnter Administrator Password: ");
if(strcmp(admin_pass,pass)==0)
return 1;
else
return 0;
The function getpass()
is defined in the header file unistd.h
.