Home > Back-end >  Password Validator loop until password is correct
Password Validator loop until password is correct

Time:07-28

I was asked to write a program which takes in password attempts from a user (via stdin) until the user enters "password1" followed by a newline, or until they close the stdin of the program.

  1. If the user guessed the correct password print: Password correct!
  2. if they close stdin print: Login failed!
  3. if they guessed an incorrect password continue reading in more attempts

The output should look like:

Enter password: my attempt
another bad attempt
password1
Password correct!

With my code so far, I can pass the first two test. But I have no idea where I do wrong for the last test:

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

#define MAX_BUFFER_SIZE 80

/* Prompts user for password, waits for correct password */
void wait_for_correct_password(void) {
    char buffer[MAX_BUFFER_SIZE];

    printf("Enter password: ");

    char* password = fgets(buffer, MAX_BUFFER_SIZE, stdin);
    // Write your while loop here
    while (password != NULL) {
        if (strcmp(password, "password1\n") == 0) {
            printf("Password correct!\n");
            break;
        } else {
            password = NULL;
        }
    }

    if (password == NULL) {
        printf("\nLogin failed!\n");
    }

}

int main(int argc, char** argv){
    wait_for_correct_password();
}

CodePudding user response:

You need to read password inside the loop.

void wait_for_correct_password(void) {
    char buffer[MAX_BUFFER_SIZE];

    char* password;
    do
    {
        printf("Enter password: ");
        password = fgets(buffer, MAX_BUFFER_SIZE, stdin);
        if (password && !strcmp(password, "password1\n")) 
        {
            printf("Password correct!\n");
            break;
        } else {
            password = NULL;
        }
    }while (!password);

}

You can limit the number of not successful attempts :

void wait_for_correct_password(int nAttempts) 
{
    char buffer[MAX_BUFFER_SIZE];

    char* password;
    do
    {
        printf("Enter password: ");
        password = fgets(buffer, MAX_BUFFER_SIZE, stdin);
        if (password && !strcmp(password, "password1\n")) 
        {
            printf("Password correct!\n");
            break;
        } else {
            password = NULL;
        }
    }while (!password && nAttempts--);

    if (password == NULL) {
        printf("\nLogin failed!\n");
    }

}

int main(int argc, char** argv){
    wait_for_correct_password(10);
}

CodePudding user response:

Place password in the loop to allow user to keep entering until it is correct.
This example shows techniques to attain proper execution flow, i.e to update a wrong password, and to exit when needed. Your wording can be added as necessary to match your assignment needs.

(Note, here, password is created as a char array, and uses a bool to flag success)

int wait_for_correct_password(void) {

    char  password[MAX_BUFFER_SIZE] = {0};  
    bool success = false; 

   // Write your while loop here
    printf( "Enter password\n");
   while (!success) {
       
      if(fgets(password, MAX_BUFFER_SIZE, stdin))        
      {
          password[strcspn(password, "\n")] = 0; //remove newline
          if (strcmp(password, "password1") == 0) {
             printf("Password is correct.  Hit any key to exit.\n");
             getchar();
             success = true;
          } 
          else 
          {
              printf("hit enter to Try again or ,<ctrl>-c to exit\n");
          }

      }
   }
        return 0;
}

int main(void)
{
    wait_for_correct_password();
    return 0;
}

CodePudding user response:

you should read the password from the loop to give the user a chance to correct his input. try this code below , it should work as expected i just added a variable called tries to give the user 3 tries after throwing a "failed to login" message

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

#define MAX_BUFFER_SIZE 80

void wait_for_correct_password(void){
    char buffer[MAX_BUFFER_SIZE];
    int tries = 3;//after 3 tries the login wil be failed
    printf("Enter password: ");
    while (tries > 0) {
    char* password = fgets(buffer, MAX_BUFFER_SIZE, stdin);
        if (strcmp(password, "password1\n") == 0) {
            printf("Password correct!\n");
            break;
        }
    else{
        printf("incorrect password try again: \n");
        tries--;
    }
    }

    if (tries == 0) {
        printf("%d\n", tries);}
}

int main(void){
    wait_for_correct_password();
}
  • Related