Honesty Rocks! truth rules.

Password Function

HOME      >>       Programming

khalilov

If you are making a logging system in a C++ program you don't want the password you are writing to appear in characters right? making it appear as stars is alot better.

#include<stdio.h> #include<conio.h>char x; void password(char A[]){int i=0;for(i=0;i<6;i++){flushall();x=getch();A[i]=x;printf("*");} }void main(){char A[6];password(A);}

First of all declare a character globally or locally inside the function it doesn't matter. But you must declare the array inside the main or globally. The only parameter needed in the function is the array. As for the length for the array, i put it as 6 , you can change it. Notice inside the function it is important to put flushall() to clear to buffer otherwise some leftover characters might rune the password function. Also make sure to put the length of the loop equal to the size of the password.
As for how it works its simple, by using the getch() function we get a character,put it in x and then put it in A[0], the process is repeated until the entire array is full. Each time you input a character '*' is printed and the cycle is repeated. You can replace the '*' with X if you want or any other character, but * is used in most programs.
The main downer in the way i wrote this function is that it has a fixed length, the password needs to be exactly 6 in this case. And pressing 'Enter' would be considered a character inside the password, you can however fix that by doing a check for the ascci code of the inputed character. I think the ascci code of 'Enter' is 13 if my memory serves me well.