Password Protection

I want to make

my console program password protected,

where I can write the password but

the text that I write will not appear

instead it should show ' * ' and when I press

enter it should test the password if it is

correct,and when I press delete or backspace

it should decrease the number of the * on the

screen.

For example :

in one moment I have pressed :

==>>protected -- 9 caracters

// in the display are shown 9 " * " like this :

==>>*********

when I press delete or backspace

the text now is ==>>protecte d--was deleted

and in the screen now are shown 8 " * "

An another problem that I have is that I cant make

my program to test the password when I press !! enter !!

,with other keys like space , tab , esc , ect.. I can and

it works correctly but with enter not

If someone can help me please give me an idea.
If you are working on windows system,use getch() to not display the character and
cout<<"*";
Since there is no getch() in UNIX,try turning off the echo attribute of the console by using system("stty -echo"), then reading the input a character at a time, and reenabling the echo afterwards.

Also try using getpass() function which leaves its result in an internal static object and returns a pointer to that object
Stars are often used, but a bad idea. I suggest you avoid them. Instead, just turn off echo.
http://www.cplusplus.com/forum/general/12256/page1.html#msg58590

If you are sticking to Windows, you can use this:
http://www.cplusplus.com/forum/general/3570/page1.html#msg15410

You can also play with the following for getting unbuffered input:
http://www.cplusplus.com/forum/articles/7312/page1.html#msg33734

Good luck!
How about this crappy code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <conio.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
char ch;
char buff[100];
char *password="asilas";
int pos=0;

while(true)
{
ch=getch();
if(ch==13) break; // enter was pressed
if(ch==8) { // discard * and char from buffer
cout <<"\b \b"; buff[pos-1]='\0'; pos--; }
else { // display * and put char to buffer
 cout<<"*"; buff[pos]=ch; buff[pos+1]='\0'; pos++;
}
if(pos<=0) pos=0;
}
if(strcmp(password,buff)==0) cout <<"\ncorrect password";
getch();
}

@Null... Why offer it when you think it's crappy yourself?
Null

Nice C...
Topic archived. No new replies allowed.