Reputation: 326
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;
}
Upvotes: 3
Views: 1786
Reputation: 7610
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
.
Upvotes: 3
Reputation: 6736
my suggestion is to use getch() in caution. <conio.h>
is an very old library, and is frequently changed from OS to OS. For many cases, read
is an alternative for getch
.
See
http://www.cplusplus.com/forum/articles/7312/#msg33734
Upvotes: 1