Password disguise

Hi, i am new to C++ and I have just make Password program
like this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;

void main(){
	char ch;
    char password[50];
	int i=0;
	
    //13 is ascii value for "Enter" key
    printf("Enter Password: ");

    while((ch=_getch())!=13) 
	{
        printf("*");
        password[i] = ch;
	i++;
    }
	password[i] = '\0';

    //here pass will contain your password.
	cout <<"Your password: "<<password<<"";

It worked all right but if I press <- Backspace button, example I type Edward and then press <- Backspace button, I suspect output is "Your password: Edwar, but the output is Edward, and when type E output is * , type d = *, type w = *, type a = *, type r = *, type d output is *, then total output is ****** (6 stars based 6 words on " Edward)but when I press <- Backspace button I suspect the output is *****(5 stars based on Edwar), but that did not happen, the ouput is ******* Wew... why can this be ? maybe you've got the right way ... please help me
See, the backspace is treated as a character (ASCII (dec) = 8), so you'll need to create some sort of way of detecting the backspace and moving your i back one step and reseting the data at that position, as well as making it look as if one star was deleted from the console. The console's readout wasn't meant to be backspaceable (if you know what I mean), so I'd recommend just clearing the screen (preferably using a large number of newlines) and printing out the appropriate number of stars to make it look as if the backspace happened.

Also, int main(). :P

Also, why are you mixing C and C++ I/O?

-Albatross

EDIT: 5 edit limit exceeded; message: "Bad Albatross! Bad!"
Last edited on
Hmm... hmm, that means I need to change the letter array [] with null,
example, if d in array[5] and then when I press <- Backspace I need to change array[5] = "d" to array[5] = ' \0' Null ?? Ohh I am trying something new so I mix that heheh
Last edited on
I just make the program, I have successfully make program know when I press backspace, computer delete the password letter, but I don't know how to delete output,
print ***** press backspace = result = ****
This can be done if I use system("cls");, but do you have a better way?

this is the 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
26
27
28
29
30
31
32
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;

void main(){
	char ch;
    char password[50];
	int i=0;
	
    //13 is ascii value for "Enter" key
    printf("Enter Password: ");

    while((ch=_getch())!=13) 
	{	
		if(ch != 8)
		{
			printf("*");
			password[i] = ch;
			i++;
		}
		else
		{	
			password[i-1] = '\0';
			i--;
			
		}
    }
	password[i] = '\0';

    //here pass will contain your password.
	printf("\nYour password: %s", password);
Last edited on
Well, since you're using the severely deprecated header conio.h, there is clrscr().

Once you get this program writing, I'd recommend trying to port it to PDCurses, just because it's a newer and more portable library. EDIT: :)

Whole article on ways to clear the screen:
http://www.cplusplus.com/forum/articles/10515/

-Albatross
Last edited on
Not a good solution but you ask for it so.. here's quick and dirty code. Also there is a problem when the user prints too long password. You might want to some checking with that.

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
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <conio.h>

using namespace std;

int main()
{
    char ch;
    string password;

    //13 is ascii value for "Enter" key
    cout << "Enter Password: ";

    while((ch=_getch())!=13) 
    {	
        if(ch != 8)
        {
            password.push_back(ch);
        }
        else if(password.length()>0)
        {	
            string::iterator it = password.end()-1;
            password.erase(it);
        }
        
        string prompt = "Enter Password: ";
        string tmp = password;
        fill(tmp.begin(), tmp.end(), '*');
        cout << "\r" << prompt << tmp << "  ";
        cout << "\r" << prompt << tmp;	
    }

    //here pass will contain your password.
    cout << "\nYour password: " <<  password << endl;
    return 0;
}

Hi there, this is a bit untidy and dirty code...but will do the trick.
The function is call
getPass(int width)
and the argument
width
is the max length of the password:

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
string getPass(int width)
{
     cout<<"[";
     for (int i=0; i<width; i++)
         cout<<"_";
     cout<<"]";
     for (int i=0; i<(width+1); i++)
         cout<<"\b";     
     char letra,pass[width];
     int i=0;
     do
     {
          letra = getch();
          if (letra!=8 && letra!=13 && i<width)
          {
             pass[i]=letra;
             cout<<"*";
             i++;
          }
          else if (letra==8 && i>0)
          {
               cout<<"\b"<<"_"<<"\b";
               i--;
          }
     }
     while (letra!=13);
     pass[i]='\0';
     return pass;
}


For example a program may use the fucntion like these

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

string getPass(int width)
{
...
}

int main (void)
{
     string pass;
     cout<<"Write the password: ";
     pass=getPass(16);
     cout<<"\nThe password is: "<<pass;
}


For example for an input like "MyPass!" the consol output should be like these:

Write the password: [*******_________]
The password is: MyPass!


Last edited on
Topic archived. No new replies allowed.