C++ Password censorship and backspace.

I am having troubles in the part of entering the password. I can't figure out how I'm going to exclude the backspace key, delete key, enter key, and other special keys from reading it together with the actual password characters.

I have searched around for some answer but I simply don't understand their 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
33
34
35
36
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

int main()
{
    char userChar[100], choice, passChar[100];
    do
    {
         system("cls");
    cout<<"ENTER USERNAME: ";
    cin.getline(userChar,100);
    cout<<"ENTER PASSWORD: ";
    for (int i=0;;i++) //here is where the password goes
 {
    passChar[i]=getch();
    cout<<"*";
}
    cout<<endl;
    if (!strcmp(userChar, "admin") && !strcmp(passChar, "secret"))
    {
    cout<<"Acess granted!";
    exit (1);
}
    else 
    cout<<"Acess denied!\n";
    cout<<"Do you want to try again? Y/N";
    cin>>choice;
    cin.ignore(100,'\n');
    toupper(choice);
}while(choice!='N');
    system("pause>0");
    
return 0;
}


Cheers,
Abry
Last edited on
This uses the non-standard but popular conio.h header (and also manages to get strcmp from somewhere without including the relevant header file, which is definitely not very portable).

Anyway, your question needs refinement. Which is the first line you do not understand?
I only want letters, numbers and special characters to be shown with asterisk for my password. The problem is when you press the backspace, enter, and delete, it still shows asterisk.

And yea, i was rushing when i put the header files. Anyway, that not the problem.
Last edited on
Any response?
I only want letters, numbers and special characters to be shown with asterisk for my password. The problem is when you press the backspace, enter, and delete, it still shows asterisk.


My suggestion would be not to output the asterisk when the user presses backspace, enter or delete.
Its totally easy... look up the ascii in http://www.asciitable.com/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout<<"ENTER USERNAME: ";
cin.getline(userChar,100);
system("cls");
cout<<"ENTER PASSWORD: ";
for (int i=0;;i++) //here is where the password goes
{
    passChar[i]=getch();
    if (passChar[i]==8)//8 for backspace
        i-=2;
    if (passChar[i]=='\n')
        break;
    system("cls");
    cout<<"ENTER PASSWORD: ";
    for (int j=0; j<i; j++)
        cout<<"*";
    cout<<endl;
}

now you "handle" every other special characters that you don't want the computer to read.
Last edited on
Close enough.

Now the problem is even when my userChar and passChar inputs are correct, it says "Acess Denied." when its supposed to say "Acess Granted!".

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 <string>
#include <conio.h>
#include <cctype>
using namespace std;

int main()
{
    char userChar[100], choice='Y', passChar[100];
while(choice=='Y')
	{
		system ("cls");
		cout<<"ENTER USERNAME: ";
		cin.getline(userChar,100);
		system("cls");
		cout<<"ENTER PASSWORD: ";
for (int i=0, p=0;;i++) //here is where the password goes
	{
	    p++;
        passChar[i]=getch();
        if (passChar[i]==8)//8 for backspace
        p-=2;
        if (passChar[i]==13)
        break;
        system("cls");
        cout<<"ENTER PASSWORD: ";
	for (int j=0; j<p; j++)
		cout<<"*";
	}
    cout<<endl;
    if (!strcmp(userChar, "admin") && !strcmp(passChar, "secret"))
    {
		cout<<"Acess granted!";
		exit (1);
    }
    else 
		cout<<"Acess denied!\n";
    cout<<"Do you want to try again? Y/N : ";
    cin>>choice;
    toupper(choice);
    cin.ignore(100,'\n');
}
    
return 0;
}
Last edited on
Have you considered terminating that C-style string you're building?
first things first...
Your character arrays are not initialized.
when you declare
 
char userChar[100];


It doesn't create a char array with nothing in them... you have to initialize them like so...

 
for (int i=0; i<100; userChar[i++]=0);


why would you create a new variable called p and do p-=2?

the original code that i wrote...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout<<"ENTER USERNAME: ";
cin.getline(userChar,100);
system("cls");
cout<<"ENTER PASSWORD: ";
for (int i=0;;i++) //here is where the password goes
{
    passChar[i]=getch();
    if (passChar[i]==8)//8 for backspace
        i-=2;
    if (passChar[i]=='\n')
        break;
    system("cls");
    cout<<"ENTER PASSWORD: ";
    for (int j=0; j<i; j++)
        cout<<"*";
    cout<<endl;
}

was correct.

if you do what you are currently doing, you are simply "hiding" the backspace char and not removing it from the array...

my method effectively method "erases" the character that was typed. You just have to put

1
2
while (i<100)
    passChar[i++]=0;


or something equilvalent somewhere in the code
Last edited on
Well thanks everyone. I got it to work now, and its perfect. :D
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
69
70
71
#include <iostream>
#include <string>
#include <conio.h>
#include <cctype>
using namespace std;

int main()
{
    char userChar[100], choice='Y', input, passChar[100];
    
    for (int i=0; i<100; userChar[i++]=0);
    
while(choice=='Y')
	{
		system ("cls");
		cout<<"ENTER USERNAME: ";
		cin.getline(userChar,100);
		system("cls");
		cout<<"ENTER PASSWORD: ";

	for (int i=0;;i++)
	{
input=getch();
if ((input!=8)&&(input!=13))
{	
passChar[i]=input;
}

else if (input==8)
i-=2;
else if (input==13)
break;
system("cls");
        cout<<"ENTER PASSWORD: ";
	for (int j=0; j<i+1; j++)
		cout<<"*";
	
	
}
    cout<<endl;
    if (!strcmp(userChar, "admin"))
    {
		cout<<"UserName correct!\n";
    }
    else
        cout<<"Username incorrect!\n";
    if (!strcmp(passChar, "secret"))
    {
        cout<<"Password correct!\n";                  
                          }
    else
    cout<<"Password incorrect!\n";
    if ((!strcmp(userChar,"admin"))&&(!strcmp(passChar, "secret")))
    {
        cout<<"Access Granted!";
        system("pause>0");
        exit (1);                                               
                                                       }                      
    else 
		cout<<"Acess denied!\n";
    cout<<"Do you want to try again? Y/N : ";
    cin>>choice;
    choice=toupper(choice);
    cin.ignore(100,'\n');
    for (int i=0; i<100; userChar[i++]=0);
    for (int i=0; i<100; passChar[i++]=0);
    
}
    
return 0;
}
Topic archived. No new replies allowed.