2

So, I have to encrypt my console application with a password, i did something that's working but there is a problem, backsapce doesn't erase the character entered, it is also counted as a character, how can I make it to do its job, to erase the character?

This is the code:

void main()
{
    char password[20], my_password[20] = "password";
    int i;
    char ch;
    system("cls");
    cout << "PASSWORD: ";


    i = 0;
    do
    {
        ch = _getch();
        password[i] = ch;
        if (ch != 27 && ch != 13 && ch != 9)
            cout<<"*";
        else
            break;
        i++;
    } while (i < 19);
    password[i] = '\0';



    if (strcmp(password, my_password) != 0) 
    {
        cout << "\n\nIncorrect password !!!";
        cin.get();
        return;
    }
    cout << "\n\nPassword is correct !";
    cout <<"\n\nThe program is executed !";
    cin.get();
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
Iulian B.
  • 95
  • 1
  • 1
  • 8
  • 1
    What type of terminal and operating system are you using? – David Grayson Apr 09 '19 at 16:57
  • Please don't use [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming)). For characters that have escape sequences (like `13` for carriage-return, `'\r'`) use that characters, and for characters that don't have escape sequences (like `27` and `9`) at least create macros. – Some programmer dude Apr 09 '19 at 17:03
  • Also note that [`_getch`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=vs-2019)` returns an **`int`**, just like all standard character-reading functions. This is rather important if you ever want to compare with `EOF`. – Some programmer dude Apr 09 '19 at 17:04
  • 3
    Lastly, please don't add tags for languages that might seem similar, only use the tags for the language you're actually programming in. C and C++ are two *very* different languages. And even though you don't use many C++ features, your program is still a C++ program, so should not have the `c` tag. – Some programmer dude Apr 09 '19 at 17:05

3 Answers3

1

"how can I make it to do its job, to erase the character?"

Use a curses library. Like ncurses.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • Don't get `ncurses` involved unless you need to; it's a tool for a specific domain, and for simple stuff like this it's overpowered at best. – 0xdd Apr 09 '19 at 17:23
  • 1
    @Ctx Short term; *maybe*. But as soon as OP wants to do more advanced stuff, a `curses` library will become handy (and it can also solve the immediate problem). – Jesper Juhl Apr 09 '19 at 17:36
1

You could check if the character received is a backspace, if it is decrement i which will effectively remove the last character.

    i = 0;
    do
    {
        ch = _getch(); // get the character
        if(ch == DEL || ch == BS) // check for backspace
        {
            i--;
            cout << BS;
        }
        else if(ch >= ' ' && ch <= '~') // check if its valid ASCII
        {
            password[i] = ch;
            cout << "*";
            i++;
        }
        else if (ch == 27 || ch == 13 || ch == 9) // check if entry is complete
        {
            break;
        }


    } while (i < 19);

    password[i] = '\0';

somewhere else

#define BS '\b'
#define DEL 127
liamcomp
  • 376
  • 1
  • 8
  • 1
    Rather than macros that can wreck text-substitution havoc, consider the using something like of `constexpr char BS = '\b';` This will not silently stomp over `BS`s, be seen as a `char` allowing the compiler to type check and do all that other error and warning message goodness the macro can't, and the `'\b'` allows portability to non-ASCII systems (less of a concern than it used to be, but still something to watch out for). – user4581301 Apr 09 '19 at 18:38
  • 1
    Thanks for the addition, you’re right with overall improvement of using a escaped character rather then an integer. I changed the macro to '\b', I’m going to leave it as a macro for now tho. I only work with C, I guess the OP should not have tagged C as this should really be only C++. – liamcomp Apr 09 '19 at 18:48
  • 1
    I didn't even see the C tag. You're right. I'll remove that tag in just a second. – user4581301 Apr 09 '19 at 18:52
1
void main()
{
    char password[20], my_password[20] = "password";
    int i;
    char ch;
    system("cls");
    cout << "PASSWORD: ";


    i = 0;
    do
    {
        ch = _getch();
        if (ch == 8)
        {
            i--;
            cout << "\b \b";
            continue;
        }

        password[i] = ch;
        if (ch != 27 && ch != 13 && ch != 9)
            cout << "*";
        else
            break;
        i++;
    } while (i < 19);
    password[i] = '\0';



    if (strcmp(password, my_password) != 0)
    {
        cout << "\n\nIncorrect password !!!";
        cin.get();
        return;
    }
    cout << "\n\nPassword is correct !";
    cout << "\n\nThe program is executed !";
    cin.get();
}

Not the cleanest code but it works. Decrement the counter to over write the previous character and output two backspace characters separated by a space.

Avery17
  • 26
  • 3
  • Recommended reading before using this answer: [What is a magic number, and why is it bad?](https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad) – user4581301 Apr 09 '19 at 18:40