试图在 C 中随机 "password generator"
Trying to make a random "password generator" in C
#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;
}
长话短说,我正在尝试让这个程序运行,但由于某些原因,我不知道如何提前work.Thanks。
首先,检查您尝试使用的逻辑。
if (passwordLength < 5 && passwordLength>25) {
printf("Your password must be from 5 to 25!!");
}
永远没有值,可以同时小于5和大于25。您需要检查其中 之一 ,而不是两者。请改用逻辑或运算符 ||
。
也就是说,在扫描值后立即检查条件可能是有意义的。计算很多东西,然后根据早就知道的条件,把它们扔掉是没有意义的。
此外,请始终检查 scanf()
的返回值以确保扫描成功,否则您最终可能会使用不确定的值。
除了 Sourav 的回答中指出的逻辑问题外,还有一个编译错误。
从编译器的输出来看,问题并没有立即显现出来,但是:
/tmp/x1.c: In function ‘main’:
/tmp/x1.c:7: error: parameter ‘counter’ is initialized
/tmp/x1.c:8: error: expected declaration specifiers before ‘srandom’
/tmp/x1.c:13: error: expected declaration specifiers before ‘printf’
/tmp/x1.c:14: error: expected declaration specifiers before ‘scanf’
/tmp/x1.c:16: error: expected declaration specifiers before ‘while’
/tmp/x1.c:22: error: expected declaration specifiers before ‘if’
/tmp/x1.c:26: error: expected declaration specifiers before ‘printf’
/tmp/x1.c:27: error: expected declaration specifiers before ‘return’
/tmp/x1.c:28: error: expected declaration specifiers before ‘}’ token
/tmp/x1.c:11: error: declaration for parameter ‘passwordLength’ but no such parameter
/tmp/x1.c:9: error: declaration for parameter ‘randChar’ but no such parameter
/tmp/x1.c:7: error: declaration for parameter ‘counter’ but no such parameter
/tmp/x1.c:28: error: expected ‘{’ at end of input
通常,您首先会查看顶部的错误并修复它,然后查看是否可以消除更下方的其他错误。在这种情况下,真正的线索其实在最后。
您的 main
函数缺少左大括号。添加它,它将成功编译并打印一个随机密码(没有正确检查长度)。
不是if(passwordLength < 5 && passwordLength >25)
而是||
passwordLength
变量 必须 在 scanf
之后立即检查。如果程序失败 must return
不需要counter
,使用for
循环代替
如果您需要存储密码以便稍后处理,这是我推荐的,您应该将其存储在一个变量中
int main(){
srandom(time(NULL));
char randChar;
int passwordLength;
printf("Give password length: ");
scanf("%d", &passwordLength);
if(passwordLength < 5 || passwordLength >25){
printf("Your password must be from 5 to 25!!");
return 1;
}
char pwd[passwordLength];
for(int i = 0; i < passwordLength; i++){
randChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"[random() % 62];
pwd[i] = randChar;
}
printf("%s\n", pwd);
return 0;
}
#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;
}
长话短说,我正在尝试让这个程序运行,但由于某些原因,我不知道如何提前work.Thanks。
首先,检查您尝试使用的逻辑。
if (passwordLength < 5 && passwordLength>25) {
printf("Your password must be from 5 to 25!!");
}
永远没有值,可以同时小于5和大于25。您需要检查其中 之一 ,而不是两者。请改用逻辑或运算符 ||
。
也就是说,在扫描值后立即检查条件可能是有意义的。计算很多东西,然后根据早就知道的条件,把它们扔掉是没有意义的。
此外,请始终检查 scanf()
的返回值以确保扫描成功,否则您最终可能会使用不确定的值。
除了 Sourav 的回答中指出的逻辑问题外,还有一个编译错误。
从编译器的输出来看,问题并没有立即显现出来,但是:
/tmp/x1.c: In function ‘main’:
/tmp/x1.c:7: error: parameter ‘counter’ is initialized
/tmp/x1.c:8: error: expected declaration specifiers before ‘srandom’
/tmp/x1.c:13: error: expected declaration specifiers before ‘printf’
/tmp/x1.c:14: error: expected declaration specifiers before ‘scanf’
/tmp/x1.c:16: error: expected declaration specifiers before ‘while’
/tmp/x1.c:22: error: expected declaration specifiers before ‘if’
/tmp/x1.c:26: error: expected declaration specifiers before ‘printf’
/tmp/x1.c:27: error: expected declaration specifiers before ‘return’
/tmp/x1.c:28: error: expected declaration specifiers before ‘}’ token
/tmp/x1.c:11: error: declaration for parameter ‘passwordLength’ but no such parameter
/tmp/x1.c:9: error: declaration for parameter ‘randChar’ but no such parameter
/tmp/x1.c:7: error: declaration for parameter ‘counter’ but no such parameter
/tmp/x1.c:28: error: expected ‘{’ at end of input
通常,您首先会查看顶部的错误并修复它,然后查看是否可以消除更下方的其他错误。在这种情况下,真正的线索其实在最后。
您的 main
函数缺少左大括号。添加它,它将成功编译并打印一个随机密码(没有正确检查长度)。
不是if(passwordLength < 5 && passwordLength >25)
而是||
passwordLength
变量 必须 在 scanf
之后立即检查。如果程序失败 must return
不需要counter
,使用for
循环代替
如果您需要存储密码以便稍后处理,这是我推荐的,您应该将其存储在一个变量中
int main(){
srandom(time(NULL));
char randChar;
int passwordLength;
printf("Give password length: ");
scanf("%d", &passwordLength);
if(passwordLength < 5 || passwordLength >25){
printf("Your password must be from 5 to 25!!");
return 1;
}
char pwd[passwordLength];
for(int i = 0; i < passwordLength; i++){
randChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"[random() % 62];
pwd[i] = randChar;
}
printf("%s\n", pwd);
return 0;
}