0

I want to mask my password with "*" while the user is inputting it in. I used the getch() function but it accepts backspace and return as characters.

This is the code I have written:

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    char password[7];
    char PASSWORD[7]="abc123";
    pass:
        cout<<"Enter the 6 character password: "<<endl;
        for(int i=0;i<6;i++) {
            password[i]=getch();
            cout<<"*"; //To mask the user input with '*'
        }
        cout<<endl;
        if(strcmp(password,PASSWORD)==0) {
            cout<<"LOGIN SUCCESSFULL..."<<endl<<flush;
            system ("PAUSE");
        }
      
        else{
            cout<<"Incorrect Password;"<<endl;
            goto pass; //goes back to "pass:" and asks for password input again.
        }
        return 0;
}

Please how can I make it accept numbers and alphabets only.

2 Answers2

0

You can check if what is entered are numbers or alphabets, and ignore the input otherwise.

for(int i=0;i<6;) {
    password[i]=getch();
    if (isalnum(password[i])) {
        cout<<"*"; //To mask the user input with '*'
        i++;
    }
}

You should add #include <cctype> to use isalnum().

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

First of all, the standard warning: this method of checking a password has essentially no security at all, so don't use it for anything where security actually matters.

You can write a small "editor" that knows how to deal with backspace and carriage return characters.

bool IsAlnum(int ch) {
    if (ch < 0 || ch > 255)
        return false;
    return isalnum(ch);
}

// use getch() to read a keystroke, including possible extended keystroke
int getkey()( { 
    int ch = getch();
    if (ch == 0 || ch == 0xe0) {     // if it's an extended key
        ch = (ch << 8) + getch();    // return lead byte + extended byte
    }
    return ch;
}

void readpassword(char *dest, size_t length) { 
    int pos = 0;
    for (;;) { 

        // when they press return, we're done
        if (ch == '\r') { 
            dest[pos] = '\0';
            return;
        }
        // if it's alphanumeric, add it to password, if there's length
        if ((IsAlnum(ch)) && pos < length) {
            char[pos++] = ch;
            printf("*");
        }
        // if it's a backspace and there's at least one character entered, 
        // erase the last character
        if (ch == '\b' && pos > 0) {
            printf("\b");
            --pos;
        }
    }
}

Note that if you use getch() on its own, you can run into problems, if the user presses something like a cursor key. These are read as two consecutive bytes, and if you don't read them together, the second byte will contain a value you don't want.

Though it's rarely useful for passwords, you can add support for more keys pretty much the same way, if you want (e.g., cursor keys, control-cursor to move by word, and so on). If you're going to support many more, you may want to use a switch statement instead of if statements (but switch doesn't work all that well with a Boolean function like isAlnum).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111