Password Masking

I'm Taking User Name And Password From The User. Does Anyone Know How To Display Only Stars When User Enter The Password?

char pw[];
cout<<"Password: ";
cin>>pw; //Don't Want To Display The PW. Only Start Or Something Like That

Thank you
Last edited on
you have to get the length of the character in the pw array and then use the for loop to print the stars
you should have a look
here http://cplusplus.com/reference/clibrary/cstdio/getc/
and here http://cplusplus.com/reference/clibrary/cstring/strlen/

It's what I would do, I'm no expert though

ZED
@bluecoder That Idea Was In My Head. It Didn't Work. I Tried.
@ZED0815 No Thats Not What I Want. Thanks Anyways
Oh I Got It

http://www.cplusplus.com/forum/general/3766/

But Is Their Any Other Ways To Do This ?
Last edited on
MSVS2008:
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <locale>
#include <conio.h>

using namespace std;


int main()
{
	const char symb = '*', space = ' ';
	const int chars_in_line = 80, max_pass_l = 32;
	// assert(max_pass_l + strlen("Input the password:   ") < chars_in_line)

	cout << "Input the password:   ";
	char ch, password[max_pass_l + 1] = {'\0'};
	for(int inp_ch = 0; ch = _getch(); )
	{
		// Enter
		if(ch == '\n' || ch == '\r')
			break;

		// Backspace
		else if(ch == '\b' && inp_ch > 0)
		{
			password[--inp_ch] = '\0';
			cout << '\r';
			for(int i = 0; i < chars_in_line - 1; i++)
				cout << space;
			cout << "\rInput the password:   ";
			for(int i = 0; i < inp_ch; i++)
				cout << symb;
		}

		// Next char
		else if(inp_ch < max_pass_l && isprint(ch, cout.getloc()))
		{
			password[inp_ch++] = ch;
			cout << symb;
		}
	}

	cout << endl << "Password:   \"" << password << "\"" << endl;

	return 0;
}
This is not possible using only standard C++. It is the terminal that is doing this which is external to your program. You would have to use a library such as ncurses: http://en.wikipedia.org/wiki/Ncurses
Last edited on
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;


string getpass(const char *prompt, bool show_asterisk=true)
{
  const char BACKSPACE=8;
  const char RETURN=13;

  string password;
  unsigned char ch=0;

  cout <<prompt<<endl;

  DWORD con_mode;
  DWORD dwRead;

  HANDLE hIn=GetStdHandle(STD_INPUT_HANDLE);

  GetConsoleMode( hIn, &con_mode );
  SetConsoleMode( hIn, con_mode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT) );

  while(ReadConsoleA( hIn, &ch, 1, &dwRead, NULL) && ch !=RETURN)
    {
       if(ch==BACKSPACE)
         {
            if(password.length()!=0)
              {
                 if(show_asterisk)
                     cout <<"\b \b";
                 password.resize(password.length()-1);
              }
         }
       else
         {
             password+=ch;
             if(show_asterisk)
                 cout <<'*';
         }
    }
  cout <<endl;
  return password;
}



int main()
{
  const char *correct_password="null";

  cout <<"Test 1: echoing enabled"<<endl;
  string password=getpass("Please enter the password: ",true); // Show asterisks
  if(password==correct_password)
      cout <<"Correct password"<<endl;
  else
      cout <<"Incorrect password. Try again"<<endl;

  cout <<"\nTest 2: echoing disabled"<<endl;
  password=getpass("Please enter the password: ",false); // Do not show asterisks
  if(password==correct_password)
      cout <<"Correct password"<<endl;
  else
      cout <<"Incorrect password. Try again"<<endl;

  return 0;
}
this is part of my code. But it gives an error. encryption part i can do . I need to fix this error. it tells every time password not matching
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include<iostream>
#include<iomanip>
#include<conio.h>
#include<string>

using namespace std;
void encrypt(char*);

main()
{
     char uname[30],pw1[30],pw2[30],c=' ';
     char *pwPtr;
     cout<<"User Name: ";
     cin>>uname;
     cout<<"\nPassword: "<<endl;
     
     for(int i=0;c!=13;i++)
     {
          c=getch();
          pw1[i]=c;
          cout<<"*";            
     
     }
     
     c=' ';
     cout<<"\nReEnter Password: \n";
     
     for(int j=0;j<30,c!=13;j++)
     { 
          c=getch();
          pw2[j]=c;
          cout<<"*";               
     }
     
     if(pw1!=pw2)
     {
          cout<<"\n\nPasswords Entered Not Matching"<<endl;            
     }
     else
     {
          pwPtr=pw2;
          cout<<"Your Encripterd Password Is: ";
          encrypt(pwPtr);    
     }
     cin.ignore();
     cin.get();
}
Last edited on
It'll puzzle you, but you can't compare strings like so! Use strcmp or something similar! What you do just now is comparing some pointer address of string 1 and 2 -> these can't be the same unless string 1 is string 2 even in memory!
BTW: the comparison between two pointers is perfectly fine, just not what you want. That's the reason your compiler isn't bitching!
Ah Yeah...Ill try strcmp.Thanks Youuu
@ZED0thank 815 yes it works. Thanks Alot. As I'm New To This Language It Couldn't Figure It Out In The First Place. Thanks again.

But There Is Another Error. When I Enter 3 Chars It Display 4 Stars. Every time It Print One Extra Star.

This Is The Corrected Code
1
2
3
4
5
6
7
8
9
10
     if(strcmp(pw1,pw2)!=0)
     {
          cout<<"\n\nPasswords Entered Not Matching"<<endl;            
     }
     else
     {
          pwPtr=pw2;
          cout<<"Your Encripterd Password Is: ";
          encode(pwPtr);    
     }
yes of course...you print a star before checking whether input is '\n' or not....

So you enter like "bla"<RETURN> what your proggy does is:

b -> print * -> check return -> false go on...
l -> print * ->check return -> false go on...
a -> print * ->check return -> false go on...
return ->print *-> check return -> true stop...
Topic archived. No new replies allowed.