-1

Following is the code for a login program that inputs the username, and then the password, while echoing '*' for every character entered, and then compares the username and password to preset values to see if they match and exits the program if it is true, else it goes back to the beginning.

int main()
{
    int i = 0; string u; char parr[i + 1], ch;
    while (1)
    {
        system("cls");
        cout << "Enter username." << endl;
        cin >> u;
        system("cls");
        cout << "Enter password." << endl;
        i = 0;
        while (1)
        {
        tag:ch = getch();
            if (ch == '\r')
            {
                break;
            }
            if (ch == '\b')
            {
                cout << '\b';
                --i;
                ch = '\0';
                parr[i] = '\0';
                cout << ' ' << '\b';
                goto tag;
            }
            parr[i] = ch;
            ch = '*';
            cout << ch;
            ++i;
        }

        parr[i] = '\0';
        string p = "password";

        if (u == "username" && parr == p)
        {
            system("cls");
            cout << "Welcome!";
            break;
        }

        else
        {
            system("cls");
            cout << "Username and password entered does not match! Please try again.";
        }
        getch();
    }
    getch();
}       

Now, here's the problem: Recently, I found out that this method of input (for the password) doesn't work as intended with Backspace, Delete, or the arrow keys. All of these keys input certain symbols instead of deleting characters or moving the cursor. So I tried out a workaround (currently only) for the backspace key. All worked fine- the previous character was deleted when I pressed backspace, but in the end, after the comparison with the preset values, it showed that the password doesn't match.

If you could please help me in the following:

*Working backspace functionality (and delete and arrow keys if possible)
*Pressing Esc at ANY point of the program quits it.
*I have used goto in this program, whose atrocities we all already know of. Please suggest me a way to avoid this, and also to make all this code neater than its current messy state.

alexeykuzmin0
  • 6,344
  • 2
  • 28
  • 51
Anchith Acharya
  • 369
  • 1
  • 4
  • 16
  • 1
    No one is going to parse that formatting of yours. – LogicStuff Jul 31 '16 at 08:16
  • Think a little wht `int i = 0; char parr[i + 1]` means. Arrays in C++ are not dynamic, their size are fixed at compilation-time. – Some programmer dude Jul 31 '16 at 08:17
  • Yeah, I know. I never understood how to add code in Stack overflow, I am just waiting for someone to make it right. – Anchith Acharya Jul 31 '16 at 08:18
  • @ Joachim Pileborg So you're saying it is because of the fixed size of the array, right? But it worked perfectly well before I tried to make backspace work. Anyway, can you tell me how I can declare a dynamic array? – Anchith Acharya Jul 31 '16 at 08:24
  • `parr` has size of 1 here while your program may use the 1st, 2nd, and later elements. This is an obvious array overflow which leads to undefined behavior. You're just lucky it works. – alexeykuzmin0 Jul 31 '16 at 08:40

1 Answers1

1

Use continue instead of goto. In your case this will do the same: jump to the start of the loop.
Then run a debugger and watch what happens with your array if you input backspaces.

Tomas Dittmann
  • 424
  • 7
  • 18
  • It works perfect! Although, you don't have the liberty of pressing and holding down backspace- you have to hit backspace only till all the characters disappear. Hit it once more, and the value of 'i' becomes negative (I guess) due to the `--i` statement. Thanks anyway! – Anchith Acharya Jul 31 '16 at 08:59