Terms of Agreement:
By using this code, you agree to the following terms...
You may use
this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
You may link to this code from another website, but ONLY if it is not wrapped in a frame.
You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
// Name: simplest password program
// Description:simplest password program... displays "*" as each letter is typed... enable backspace... good efficient
// By: Jebin Abraham Pullukalayil
// Assumes:you just need to know what pointers are that's all for this program...
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=8637&lngWId=3//for details.//**************************************
/* the most simplest logic and most simplest password program that enables
backspace character ... just need to know what pointer and basics of
that's all just that much...*/
#include <stdio.h> //for puts()
#include <conio.h> //for getch(), getche()..etc...
#include <iostream.h>//for (cout,cin)
#include <string.h>//for strcmp()
clrscr();//display screen... of course...
cout<<"\n ENter the password ";
/* Basic pecularity for good basic and simple password program is that the password
that one is typing, should not come on the screen... and that's there in this program*/
char* password, *sub;//the password string and sub -> well i'll tell u later
char ch;//the character that yer gonna enter
n->keeps track of stars that are displayed
m->well... i'll tell u later...
/*now in this statement we store the password's 0th location in sub
you will understand why i am doing this later...*/
int i=0; // i-> keep track of password
well i used getch() becoz it gets character
from the keyboard, does not echo to screen.
for example if the input is 'j' if you enter
'j' still the screen would appear blank...
/*as soon as you enter a character you want a
star to be displayed. so thats the command...*/
n++; //no of stars is incremented
*password=ch;//ch stored in password
i++; //i increments
password++; //now password will point to next location
/*now all this will only take place if
ch is not backspace '\b'*/
/* now the question comes "WHAT SHOULD BE DONE IF THE CHARACTER ENTERRED IS BACKSPACE???"
now for example your original password is "INDIA" and now you have accidently entered
so you have to delete the D there from the password and also you have to delete one star
from the output..
just follow the following simple steps
(1) display the display screen once more.
(2) decrement the password pointer... right now it will pointing to the next location after
the second D. you want to overwrite the D. so you'll have to decrement password so
that it points now to second D;
(3) decrement n because you don't want the 4the star that has appeared.
(4) now display n stars that is the computer will display 3 stars now...
else if (ch=='\b')
m=n; //m is simple variable that's used. i dont want to touch my n...
for (; m>1; m--)
cout<<"*"; // displays the new no. of stars
/* this condition above is just a small correction factor required in the program...
well for example if the if statement was not there, just imagine that the password
was "INDIA" and you by mistake typed "PAKISTAN" now you use the backspace and delete...
like that you delete pakistan after you delete pakistan you by miskatedly pressed backspace
2 more times and then you enterred india...
Now the password that is stored will be dia instead of india because location of password
will get substracted to a position lower that it's intial position or 0th position so when we
enter i and n it's will be stored some addreess that come before password and dia will be stored
in the password... so we need to stop accepting the backspaces when we reach the intial position.
Well the correct this factor this if statement is used...
sub has already password's intial address so the condition will check whether password has
reached the initial address(i.e. all letters deleted). If the position of password is not the
intial positon(ie. no equal to sub's position) the only it will decrement and i will be substracted.
other wise it'll proceed forward that's it...*/
ch=getch();//same proceedure follows unless enter is encountered.
*password='\0'; //last part of password is entered as null
for (; i>0; i--)
password--;//BRINGING THE POINTER TO FIRST LOCATION
return password;//RETURN POINTER
pa=pass();//RECIEVING THE PASSWORD
int m, n,i;
cout<<"\n The password that you enterred just now is ";
cout<<"\n It's of length "<<i;
/* well u can use this program anywhere you want cause i agree with just one thing
**"A CANDLE DOES NOT LOSE ANYTHING WHILE LIGHTING ANOTHER CANDLE"**/
Well...nice code but unnecessary. The C++ library already a better alternative. The getpass() function is used to accept passwords, is more secure that even the asteriks aren't shown. getpass() is even used in UNIX. (If this comment was disrespectful, please report it.)
2 mb http://community.borland.com/museum 7.5 mb http://www.bloodshed.net/devcpp.html email@example.com peace out (If this comment was disrespectful, please report it.)
2/24/2005 3:21:06 AM:
char* password, *sub; . . *password = ch;
Well you have used the pointer which is not initialized (thus, has a random place in the memory) and this can lead to any kind of corruption in memory if the password points to a used memory block. I would rather add a line of code like malloc and warn the user or do not allow adding more than x characters. (If this comment was disrespectful, please report it.)
i hope you are correct. well.... i'll try to correct it later.... (If this comment was disrespectful, please report it.)
Add Your Feedback
Your feedback will be posted below and an email sent to
the author. Please remember that the author was kind enough to
share this with you, so any criticisms must be stated politely, or they
will be deleted. (For feedback not related to this particular code, please
click here instead.)