SO MY PROB HERE IS JUST PLAINLY THE BACKSAPCE FEATURE OF MY PROGRAM EVERYTHING WORKS FINE EXCEPT FOR THE FACT THAT THE PROGRQAM IS CONSIDERING THE BACKSPACE KEHY AS PART OF THE CHARACTERS.

int verification2()
    {
        system("cls");
        char userChar[100], choice = 'Y', input, passChar[100];

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

        while (choice == 'Y')
        {

            //system("cls");
            gotoxy(28, 10);
            cout << "ENTER USERNAME: ";
            cin.getline(userChar, 100);
            //system("cls");
            gotoxy(28, 12);
            cout << "ENTER PASSWORD: ";

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

                else if (input == 8)
                {
                    cout << "\b \b";
                    passChar[i] - 1;

            }
                else if (input == 13)
                    break;
                //system("cls");
                gotoxy(28, 12);
                cout << "ENTER PASSWORD: ";
                for (int j = 0; j < i + 1; j++)
                    cout << "*";


            }
            cout << endl;
            if (!strcmp(userChar, "veronica"))
            {
                Sleep(1000);
                gotoxy(28, 14);
                cout << "UserName correct!n" << endl;
            }
            else
                Sleep(1000);
                gotoxy(28, 14);
                cout << "Username incorrect!n";
                if (!strcmp(passChar, "password"))
                {
                    Sleep(1000);
                    gotoxy(28, 16);
                    cout << "Password correct!n" << endl;
                }
                else
                    Sleep(1000);
                gotoxy(28, 16);
                cout << "Password incorrect!n";
                if ((!strcmp(userChar, "veronica")) && (!strcmp(passChar, "password")))
                {
                    Sleep(1000);
                    gotoxy(28, 18);
                    cout << "Access Granted!";
                    Sleep(1000);
                    return 0;
                }
                else

                Sleep(1000);
             gotoxy(28, 18);
                cout << "Acess denied!n";
                Sleep(1000);
                gotoxy(28, 19);

            //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;
    }

Recommended Answers

All 17 Replies

Hi,
Can you tell me what means:
passChar[i] - 1;
I think that this expression has no effect.
Maybe it must be:

if(i > 0)
{
    i--;
    passchar[i] = '\0';
}

And, for obtaining a string, remenber to add a zero to the end:

passChar[i] = input;
passChar[i++] = '\0';

Please, forgive me if I am wrong.
Cheers.

line 30: should be --i; as previously mentioned, but -- be careful that the value of i does not go negative, such as pressing backspace too many times. So you might need something like this:

if( i > 1) { passChar[i] = '\0'; --i;}

Hi untio well to be honest that part of the code passChar[i] - 1; was just something i tried inputting in suspicion and under a "hunch" that it would work. well i tried the recorrection that you suggested and well i would say its a 50/50 solution thank you by the way. but the prob know with your recoorection of the code, whenever i press the backspace it doesnt delete the past characters it just stop whenever i press backspace as if nothings happening?

lines 3 and 4 of the code snippet untio posted are reversed -- have to zero out the current character before decrementing the value of i.

neyoibarra: repost current code.

**Ancient Dragon heres the repost of the code, well thank you for the reply, i tried all of ur solutions well as i stated with untio it was stopping the cursor and pausing it whenever i press the backsapce key **

 int verification2()
    {
        system("cls");
        char userChar[100], choice = 'Y', input, passChar[100];

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

        while (choice == 'Y')
        {

            //system("cls");
            gotoxy(28, 10);
            cout << "ENTER USERNAME: ";
            cin.getline(userChar, 100);
            //system("cls");
            gotoxy(28, 12);
            cout << "ENTER PASSWORD: ";

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

                else if (passChar[i] == 8 && i > 1)
                {

                        passChar[i] = '\0';
                        i--;



            }
                else if (input == 13)
                    break;
                //system("cls");
                gotoxy(28, 12);
                cout << "ENTER PASSWORD: ";
                for (int j = 0; j < i + 1; j++)
                    cout << "*";


            }
            cout << endl;
            if (!strcmp(userChar, "veronica"))
            {
                Sleep(1000);
                gotoxy(28, 14);
                cout << "UserName correct!\n" << endl;
            }
            else
                Sleep(1000);
                gotoxy(28, 14);
                cout << "Username incorrect!\n";
                if (!strcmp(passChar, "password"))
                {
                    Sleep(1000);
                    gotoxy(28, 16);
                    cout << "Password correct!\n" << endl;
                }
                else
                    Sleep(1000);
                gotoxy(28, 16);
                cout << "Password incorrect!\n";
                if ((!strcmp(userChar, "veronica")) && (!strcmp(passChar, "password")))
                {
                    Sleep(1000);
                    gotoxy(28, 18);
                    cout << "Access Granted!";
                    Sleep(1000);
                    return 0;
                }
                else

                Sleep(1000);
             gotoxy(28, 18);
                cout << "Acess denied!\n";
                Sleep(1000);
                gotoxy(28, 19);

            //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;
    }

line 27 is wrong.

else if (input == 8 && i > 1)

this is what i currently replaced in line 27-31, but stil to no avail of the characters being deleted if pressing the backsapce key

 if (passChar[i] == 8)
            {
                 if (i > 0){
                     passChar[i] = '\0';
                     i--;

                 }

See my previous post. passChar[i] will never contain the value of 8 because it hasn't been set yet.

**i believe that 8 was the scan code for backspace, **, what im trying to do is that when the program detects the backspace being pressed then it would initiate the part of code that would delete the Characters

yes, 8 is the scancode for backspace, but it's in input, not in passChar[i].

so something like this?

    if (input == 8)
                {

                        if (i > 0){
                            passChar[i] = '\0';
                            --i;
                        }



                }

Yes

I tried it :) but the only prob now is that when i press backspace it doesnt delete the characters, whenever i press backspace the cursor just pauses as if nothings happening, but i can still continue to type more characters

It doesn't remove the character from the window or change the cursor position because you never told it to do that. It only removes the charcter from passChar array. If you want to erase the character from the window then you need to add more code to do that.

what could or should i add for it to be able to remove the characters from the window?

I don't know how to do it in Tubo C, but basically

get current corsor position
move the cursor back one character (probably with gotoxy() )
print a space at the current cursor position

Here's some code I threw together that demonstrates what you're looking to do.

int main()
{
    using std::cout;
    using std::endl;
    using std::cin;

    const int bufsize = 100;
    const int max_pwd = bufsize - 1;
    char password[bufsize]; // = {0};
    password[0] = '\0';

    cout << "Enter the password: ";
    for (int i = 0; i < max_pwd; ++i)
    {
        int ch = _getch();

        if (ch == '\r')
        {
            // null terminate the password
            // output the line break and exit loop
            password[i] = '\0';
            cout << endl;
            break;
        }

        if (ch == '\b')
        {
            if (i > 0)
            {
                password[i] = '\0';
                // overwrite the previous character
                cout << "\b \b";
                i -= 2;
            }
            else
            {
                password[i] = '\0';               
                --i;
            }
        }
        else
        {
            password[i] = ch;
            cout << "*";
        }
    }

    cout << password << endl;

    cout << "\npress Enter to exit...";

    cin.get();

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.