【问题标题】:how to use bool, if statements, while statements, used to check the validity of user input如何使用 bool、if 语句、while 语句,用于检查用户输入的有效性
【发布时间】:2018-10-11 17:06:51
【问题描述】:

我一直在尝试使用以下代码创建密码检查器。

bool alpha = false;
bool digit = false;
bool nogo = true;
char pass[99];
int len;
len = strlen(pass);
int i, x;

while(nogo){

printf("Password checker 1.1\n");

printf("Please enter a password you would like to test.\n");
scanf("%s\n", pass);
printf("%d\n", len);

    if(len<8){
        printf("Your password is too short. Please re-enter a new password.");
        nogo = true;
    }

    if(len>20){
        printf("Your password is too long. Please re-enter a new password.");
        nogo = true;
    }

    else{
        for (int i = 0, len = strlen(pass); i < len; i++){
            if(isalpha(pass[i]) == true){
                alpha = true;
            }
        }
        for (int x = 0, len = strlen(pass); x < len; i++){
            if(isdigit(pass[x]) == true){
                digit = true;
            }
        }
    }

if(alpha == true && digit ==true ){
    printf("Your password is good.");
    nogo = false;
    return 0;
}
}

一切都没有错误地构建。但是,第一次输入密码之后的代码似乎没有正常运行。我必须输入两次它提示输入另一个密码。此外,它拒绝接受用户的第二个输入。以下是我的控制台上显示的内容。

Password checker 1.1
Please enter a password you would like to test.
asdasdada
dsadsadadasdaewqkjlks
6
Your password is too short. Please re-enter a new password.

"asdasdada dsadsadadasdaewqkjlks" ---->它们是我的输入,我输入后面的 '6' 应该是我的 strlen。

请问我的代码有什么问题?这是我第一次使用 bool,请问我使用的代码是否正确?

【问题讨论】:

  • len = strlen(pass); 这是未定义的行为,你的pass当时没有初始化。
  • 同样在您的第二个for 循环中,您将再次增加i,并且可能意味着增加x。在两个循环中,您都使用内联声明形式,因此您应该删除函数顶部的 int i, x 声明(或从两个 for 循环中删除 int 声明)。
  • 感谢@user3121023!它解决了在显示strlen之前必须第二次输入密码的问题。它还正确显示了我的strlen。我仍然无法重新输入新密码进行测试。
  • @TypeIA 不错!我现在可以重新输入密码进行测试,它给我留下了正确的 strlen。但是,它似乎并没有运行这段代码。 if(alpha == true &amp;&amp; digit ==true ){ printf("Your password is good."); nogo = false; return 0;
  • @NewbieCody isalpha()isdigit() 的返回值不是布尔值。它们是数字,其中零表示字符不是该类型,非零表示它是。因此,条件应该是 != 0 而不是 == true,它们并不等价。

标签: c loops for-loop if-statement boolean


【解决方案1】:

scanf("%s\n", pass); 之后使用 len = strlen(pass); 而不是 scanf("%s\n", pass); 它是 scanf("%s", pass); 。 您在第二个 for 循环中也犯了错误 (int x = 0, len = strlen(pass); x &lt; len; i++) 它应该是 x++ 而不是 i++

试试这个代码:-

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>

int main()
{
    bool alpha = false;
    bool digit = false;
    bool nogo = true;
    char pass[99];
    int len;
    int i, x;

    while (nogo)
    {

        printf("Password checker 1.1\n");

        printf("Please enter a password you would like to test.\n");
        scanf("%s", pass);
        len = strlen(pass);
        printf("%d\n", len);

        if (len < 8)
        {
            printf("Your password is too short. Please re-enter a new password.");
            nogo = true;
        }

        if (len > 20)
        {
            printf("Your password is too long. Please re-enter a new password.");
            nogo = true;
        }

        else
        {
            for (int i = 0; i < len; i++)
            {
                if (isalpha(pass[i])) // not needed if(isalpha(pass[i]) == true) 
                {
                    alpha = true;
                }
            }
            for (int x = 0; x < len; x++)
            {
                if (isdigit(pass[x]))// not needed if(isdigit(pass[x]) == true)
                {
                    digit = true;
                }
            }
            if (alpha == true && digit == true)
            {
                printf("Your password is good.");
                nogo = false;
            }
        }
    }
    return 0; // return 0 position was wrong.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 2019-06-18
    • 2016-02-29
    相关资源
    最近更新 更多