简体   繁体   中英

Program doesn't accept correct username and password after re-prompting the user to try again

Once you type an incorrect username or password and my program asks you for the input again, but my program doesn't accept the correct username or password when this happens

I checked that the program does actually work and it does accept the correct username and password, thus it confuses me to why it doesn't when it re-prompts the user for the input.

Also, is there any way I can improve my password masking system so that it doesn't include backspace and enter?

Thank you!

#include <stdio.h>
#include <conio.h>
#include <string.h>

void login();
void mainmenu();

int main ()
{
    login();
    mainmenu();
    return 0;
}

void login()
{
    char username[100], password[100];
    int i=0, logincheck=0;

    printf("Welcome to The POS!");
    
    while(logincheck != 1)
    {
        printf("\nPlease enter your username: ");
        scanf("%s", username);
    
        printf("\nPassword: ");
        do
        {
            password[i] = getch();
            printf("*");
            i++;
        }
        while(password[i-1] != '\r');
    
        password[i-1] = '\0';
        
        if(strcmp(username, "username") == 0 && strcmp(password, "password") == 0)
        {
            printf("\nWelcome to the POS!");
            logincheck = 1;
        }
        else
        {
            printf("\nIncorrect Username or Password!");
            system("CLS");
        }
    }
    
}

void mainmenu()
{
    printf("\n*Placeholder*");
}

You forgot to reset your counter in your loop.

#include <stdio.h>
#include <conio.h>
#include <string.h>

void login();
void mainmenu();

int main ()
{
    login();
    mainmenu();
    return 0;
}

void login()
{
    char username[100], password[100];
    int i, logincheck=0;

    printf("Welcome to The POS!");

    while(logincheck != 1)
    {
        i = 0; // Reset the counter
        printf("\nPlease enter your username: ");
        scanf("%s", username);

        printf("\nPassword: ");
        do
        {
            password[i] = getch();

            // Added backspace handling
            if (password[i] == '\b')
            {
                if (i > 0)
                {
                    putchar('\b');
                    i--;
                }
            }
            // Added return key handling
            else if (password[i] == '\r' || password[i] == '\n')
            {
                i++;
                break;
            }
            else
            {
                printf("*");
                i++;
            }
        }
        while(1);

        password[i-1] = '\0';

        if(strcmp(username, "username") == 0 && strcmp(password, "password") == 0)
        {
            printf("\nWelcome to the POS!");
            logincheck = 1;
        }
        else
        {
            printf("\nIncorrect Username or Password!");
            //system("CLS");
        }
    }

}

void mainmenu()
{
    printf("\n*Placeholder*");
}

From testing out your program, your issue on the retries for user/password entry is that you do not reset the counter "i" back to zero. Therefore, your program just keeps on adding the entered characters to the end of the character array. When I added some code in to reinitialize the password character array and added in the line of code to reset the value of "i" to zero, the validation worked on the retry.

for (int j = 0; j < 100; j++)
{
    password[j] = 0; /* This might be overkill, but this ensures that the character array does not have leftover characters */
}

i = 0;

printf("\nPassword: ");
    do
    {
        password[i] = getch();
        printf("*");
        i++;
    }
    while(password[i-1] != '\r');

Here is a sample of the terminal output after making those revisions.

Incorrect Username or Password!
Please enter your username: username

Password: *********
Welcome to the POS!
*Placeholder*

Hope that clarifies things.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM