Search code examples
cdo-whilecontinuouspassword-checker

How do I exit from do-while loop when a program was successfully terminated by user


I have been working on a program that will check the eligibility of a password.

In order for password to be eligible it needs to have at least: one uppercase letter; one number; and one dollar sign.

My program does the work checking for the requirements and determining whether the password is good to go or not.

The barrier I'm having right now is that I've tried to make program run until:

  1. User quits the program by typing "quit";
  2. Or if user types the correct form of required password.

In order to run such a repetitive process I have decided to use the do-while loop. In order for program to determine that it is time to break out, I have used the following command:

do {...} while (passwordInput != "quit" || passwordClearance != 1);

Unfortunately my program still runs even if the password was correct.

Please give me a clue how do I break out from repetitive process when it is good to go.

// challenge: 
// build a program that checks when user enters a password for an uppercase letter, a number, and a dollar sign.
// if it does output that password is good to go.

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

int main() {
    char passwordInput[50];
    int digitCount = 0;
    int upperCharacterCount = 0;
    int dollarCount = 0;
    int passwordClearance = 0;

    do {
        printf("Enter you password:\n");
        scanf(" %s", passwordInput);

        for (int i = 0; i < strlen(passwordInput); i++) {
            if (isdigit(passwordInput[i])) {
                digitCount++;
                //continue;
            } else
            if (isupper(passwordInput[i])) {
                upperCharacterCount++;
                //continue;
            } else
            if (passwordInput[i] == '$') {
                dollarCount++;
                //continue;
            }
        }

        if ((dollarCount == 0) || (upperCharacterCount == 0) || (digitCount == 0)) {
            printf("Your entered password does not contain required parameters. Work on it!\n");
        } else {
            printf("Your entered password is good to go!\n");
            passwordClearance = 1;
        }
    } while (passwordInput != "quit" || passwordClearance != 1);

    return 0;
}

Solution

  • In order for program to determine that it is time to break out, I have used the following command:

    do{...} while(passwordInput != "quit" || passwordClearance != 1);
    

    Unfortunately my program still runs even if the password was correct.

    There are two problems with that:

    1. The logic is wrong. The while expression evaluates to true, causing the loop to cycle, if either of the component relational expressions evaluates to true. Thus, to exit the loop, both must be false. You want && instead of || so that the loop exits if either one of the expressions is false.

    2. passwordInput != "quit" will always evaluate to true because you are comparing two distinct pointers. To compare the contents of array passwordInput to the contents of the array represented by "quit", you sould use the strcmp() function.