3

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;
}
Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Akshayanti
  • 354
  • 3
  • 15

3 Answers3

3

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.

Deepu
  • 7,592
  • 4
  • 25
  • 47
2

The getpass function might help.

Yang
  • 7,712
  • 9
  • 48
  • 65
1

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

Shijing Lv
  • 6,286
  • 1
  • 20
  • 12