HOME       >>       Programming

Password Function


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.


Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.