试着做一个“随机的”;密码生成器“;在C中 #包括 #包括 #包括 int main() int计数器=0; srandom(时间(空));//针对random()的正确种子设定函数 char-randChar; int密码长度; printf(“给出密码长度\n”); scanf(“%d”、&passwordLength); while(计数器
长话短说,我正在努力使这个程序工作,但由于某些原因,我不知道如何使它工作。提前感谢。首先,回顾一下您试图使用的逻辑试着做一个“随机的”;密码生成器“;在C中 #包括 #包括 #包括 int main() int计数器=0; srandom(时间(空));//针对random()的正确种子设定函数 char-randChar; int密码长度; printf(“给出密码长度\n”); scanf(“%d”、&passwordLength); while(计数器,c,logical-operators,C,Logical Operators,长话短说,我正在努力使这个程序工作,但由于某些原因,我不知道如何使它工作。提前感谢。首先,回顾一下您试图使用的逻辑 #include <stdio.h> #include <stdlib.h> #include <time.h> int main() int counter = 0; srandom(time(NULL)); // Correct seeding function for random() char randChar; int pas
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
int counter = 0;
srandom(time(NULL)); // Correct seeding function for random()
char randChar;
int passwordLength;
printf("Give password length \n");
scanf("%d", &passwordLength);
while (counter < passwordLength)
{
randChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"[random() % 62];
printf("Your password is: %c", randChar);
counter++;
}
if (passwordLength < 5 && passwordLength>25) {
printf("Your password must be from 5 to 25!!");
}
printf("\n");
return 0;
}
if(passwordLength<5&&passwordLength>25){
printf(“您的密码必须在5到25之间!!”;
}
任何值都不能同时小于5和大于25。您需要检查其中一个,而不是两个。请改用逻辑OR运算符|
也就是说,在扫描值后立即检查条件可能是有意义的。没有必要计算很多东西,然后,根据很久以前就知道的条件,把它们扔掉
另外,请始终检查scanf()的返回值,以确保扫描成功,否则可能会使用不确定的值。首先,查看您尝试使用的逻辑
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
int counter = 0;
srandom(time(NULL)); // Correct seeding function for random()
char randChar;
int passwordLength;
printf("Give password length \n");
scanf("%d", &passwordLength);
while (counter < passwordLength)
{
randChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"[random() % 62];
printf("Your password is: %c", randChar);
counter++;
}
if (passwordLength < 5 && passwordLength>25) {
printf("Your password must be from 5 to 25!!");
}
printf("\n");
return 0;
}
if(passwordLength<5&&passwordLength>25){
printf(“您的密码必须在5到25之间!!”;
}
任何值都不能同时小于5和大于25。您需要检查其中一个,而不是两个。请改用逻辑OR运算符|
也就是说,在扫描值后立即检查条件可能是有意义的。没有必要计算很多东西,然后,根据很久以前就知道的条件,把它们扔掉
另外,始终检查scanf()的返回值以确保扫描成功,否则可能会使用不确定的值。除了Sourav回答中确定的逻辑问题外,还有一个编译错误 但是,从编译器输出来看,问题并不是很明显:
/tmp/x1.c:在函数“main”中:
/tmp/x1.c:7:错误:参数“计数器”已初始化
/tmp/x1.c:8:错误:“srandom”之前应该有声明说明符
/tmp/x1.c:13:错误:“printf”之前应该有声明说明符
/tmp/x1.c:14:错误:“scanf”之前应包含声明说明符
/tmp/x1.c:16:错误:在“while”之前应该有声明说明符
/tmp/x1.c:22:错误:在“if”之前应该有声明说明符
/tmp/x1.c:26:错误:“printf”之前应该有声明说明符
/tmp/x1.c:27:错误:“return”之前应该有声明说明符
/tmp/x1.c:28:错误:在“}”标记之前需要声明说明符
/tmp/x1.c:11:错误:参数“passwordLength”的声明,但没有此类参数
/tmp/x1.c:9:错误:参数“randChar”的声明,但没有此类参数
/tmp/x1.c:7:错误:参数“counter”的声明,但没有此类参数
/tmp/x1.c:28:错误:输入末尾应为“{”
通常,你从顶部的错误开始,然后修正它,看看它是否能消除其他错误。在这种情况下,真正的线索实际上是在最后
您的
main
函数缺少一个大括号。添加它,它将成功编译并打印一个随机密码(没有正确检查长度)。除了在Sourav的回答中确定的逻辑问题外,还有一个编译错误
但是,从编译器输出来看,问题并不是很明显:
/tmp/x1.c:在函数“main”中:
/tmp/x1.c:7:错误:参数“计数器”已初始化
/tmp/x1.c:8:错误:“srandom”之前应该有声明说明符
/tmp/x1.c:13:错误:“printf”之前应该有声明说明符
/tmp/x1.c:14:错误:“scanf”之前应包含声明说明符
/tmp/x1.c:16:错误:在“while”之前应该有声明说明符
/tmp/x1.c:22:错误:在“if”之前应该有声明说明符
/tmp/x1.c:26:错误:“printf”之前应该有声明说明符
/tmp/x1.c:27:错误:“return”之前应该有声明说明符
/tmp/x1.c:28:错误:在“}”标记之前需要声明说明符
/tmp/x1.c:11:错误:参数“passwordLength”的声明,但没有此类参数
/tmp/x1.c:9:错误:参数“randChar”的声明,但没有此类参数
/tmp/x1.c:7:错误:参数“counter”的声明,但没有此类参数
/tmp/x1.c:28:错误:输入末尾应为“{”
通常,你从顶部的错误开始,然后修正它,看看它是否能消除其他错误。在这种情况下,真正的线索实际上是在最后
你的main
函数缺少一个大括号。添加它,它将成功编译并打印一个随机密码(不正确检查长度)。如果(passwordLength<5&&passwordLength>25)则不是,而是|
passwordLength
变量必须在scanf
之后立即检查passwordLength
。如果失败,程序必须返回
不需要计数器
,而是使用进行循环
如果您需要存储密码以便以后处理(我建议这样做),您应该将其存储在变量中
if (passwordLength < 5 && passwordLength>25) {
printf("Your password must be from 5 to 25!!");
}
intmain(){
srandom(时间(空));
char-randChar;
int密码长度;
printf(“给