C 中 Do-While 循环的问题

Problems with a Do-While Loop in C

我在编码方面非常新,我正在努力让这段代码循环(直到满足正确的标准.. upper/lower 大小写字母和数字)我把在正确的地方做 while 循环??

非常感谢收到的任何帮助..

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

main()
{
    int i;
    int hasUpper, hasLower, hasDigit;
    char password[20];

    hasUpper = hasLower = hasDigit = 0; // initialising these three variables to false (o)

    printf("Please enter a alpha numeric password with upper and lower case\n");
    scanf(" %s", password);

    do {
        for (i = 0; i < strlen(password); i++) {
            if (isdigit(password[i])) {
                hasDigit = 1;
                continue;
            }

            if (isupper(password[i])) {
                hasUpper = 1;
                continue;
            }

            if (islower(password[i])) {
                hasLower = 1;
                continue;
            }
        }

        printf("Not so good, try again!");
        scanf(" %s", password);
    } while ((hasDigit) && (hasLower) && (hasUpper)==1);

    // This loop will only execute if all three variables are true

    if ((hasUpper) && (hasLower) && (hasDigit)) {
        printf("Great password!");
    }

    return 0;
}

您的 while 条件有误,而且每次尝试后都需要清除变量,并且需要检查失败的打印输出。此外,将 scanf() 移动到循环的开头会使事情变得更容易,并且无需在初始输入时在循环外添加额外的一个。

#include <stdio.h>
#include <string.h>
#include <stdbool.h>    // Use for boolean types

int main(int argc, const char argv[]) {    // Correct function signature
    int i = 0, plen = 0;
    bool hasUpper = false, hasLower = false, hasDigit = false;    //Change types to boolean
    char password[20] = {0};    // Initialise string with all '[=10=]'

    printf("Please enter an alpha-numeric password with upper and lower case\n");

    do {
        hasUpper = false;    // Clear boolean variables for each new password
        hasLower = false;
        hasDigit = false;

        scanf("%s", password);
        password[19] = 0;    // ensure string is correctly terminated with '[=10=]'
        plen = strlen(password); // Get the string length *once* per new password

        for (i = 0; i < plen; ++i) {
            if (isdigit(password[i])) {    // Use 'if...else' in place of 'continue'
                hasDigit = true;
            } 
            else if (isupper(password[i])) {
                hasUpper = true;
            }
            else if (islower(password[i])) {
                hasLower = true;
            }
        }

        if ((!hasDigit) || (!hasLower) || (!hasUpper)) {    // Check the booleans before printing fail message
            printf("Not so good, try again!");
            for (i = 0; i < 20; ++i) {
                password[i] = 0;    // Clear the string with all '[=10=]'
            }
        }
    } while ((!hasDigit) || (!hasLower) || (!hasUpper));   // Correct the logic here

    printf("Great password!");   // Remove the unneeded boolean check here
    return 0;
}

我也会考虑用 if...else if 替换 if...continue 模式,因为使用 continue 是不好的做法。

由于一些逻辑问题,代码无法正常工作。
首先,即使所有值(hasDigithasLowerhasUpper)都为 1(while 中的条件错误),do while 循环仍将继续。

你也在打印 "Not so good" 语句,即使它们都是 1。

还有一个问题是,如果你输入了一次错误的密码,值被分配给三个变量,但它们并没有重置为0,因此,当你输入一个新密码时,三个变量的值三个变量就是它在上一次循环中得到的值(也就是说,如果在你之前的密码中,如果其中任何一个被设置为1,那么即使下一次密码,该值也将保持为1)。

现在,这是我已修复错误的代码

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

int main()
 {

    int i;
    int hasUpper,hasLower,hasDigit;
    char password [20];
    hasUpper=hasLower=hasDigit=0; // initialising these three variables to false (o)

    printf("Please enter a alpha numeric password with upper and lower case\n");
    scanf(" %s", password);

    do
     {
       hasUpper=hasLower=hasDigit=0;   // you need to initialize them to 0 here as well

       for(i=0;i<strlen(password); i++)
         {
            if (isdigit(password[i]))
             {
               hasDigit = 1;
             }

            if (isupper(password[i]))
             {
               hasUpper = 1;
             }

            if (islower(password[i]))
             {
               hasLower = 1; 
             }
         }

       if( ( (hasUpper) && (hasLower) && (hasDigit) ) !=1 )  // with this if statement, only if all the criteria are not met, the printf is executed
         { 
           printf("Not so good, try again!");
           scanf(" %s", password);
         }

     }while( ( (hasDigit) && (hasLower) && (hasUpper) ) !=1 );  // loop continues as long as all of them are not 1

    // This statement will only execute if all three variables are true

    printf("Great password!");

    return 0;
 }

请注意,我已从

中删除了 if 条件
if  ((hasUpper)&&(hasLower)&&(hasDigit))
{
    printf("Great password!");
}

这是因为程序仅在您输入 Good Password 时才退出 do while 循环,因此不再需要 if 语句,只需 printf 就足够了。