-
password
Hi, Could anyone advise me. I am doing a project in C that requires a password. The problem I have is when the user types in the password, it is displayed on screen. How can the password entered be disguised as **.Thanks RC
void Password(void)
{
printf("\t\tPlease enter password ");
scanf("%s",Passwordin);
//check if user entered correct password marcie using strcmp
if(strcmp(Passwordin,"marcie")==0)
{
printf("\n\t\tWelcome\n");
clrscr();
}
else
{
// if password incorrect Password function recalled until correct Password entered
printf("\t\tincorrect password\n");
Password();
}
}
-
Just a very slight modification to the above code, to make changing max. password length easier:
Code:
#include<stdio.h>
#include<conio.h>
#define MAXPASSWD 10
int main()
{
char buffer[MAXPASSWD + 1];
int i, ch;
printf("Enter a password (MAXPASSWD alphanumeric sequenc max):\n" );
printf("Password: ");
/* Read password but no larger than 10 characters */
for( i = 0; i < MAXPASSWD ; i++ )
{
//get character
ch = _getch();
//if user enters newline than quit
if(ch == '\r' || ch == '\n') break;
//if user enters backspace
if(ch == '\b')
{
//if there are no preceeding letters
if(i == 0)
{
--i;
continue;
}else //if there are preceeding letters
{
printf("\b%c\b",' ');
i-=2;
}
}else //no backspace was entered
{
putc('*',stdout);
buffer[i] = (char)ch;
}
}
/* Terminate string with null character: */
buffer[i] = '\0';
return 0;
}