问题
#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;
}
Long story short im trying to make this program work but for some reasons i have no idea how to make it work.Thanks in advance.
回答1:
First of all, review the logic you're trying to use.
if (passwordLength < 5 && passwordLength>25) {
printf("Your password must be from 5 to 25!!");
}
No value ever, can be less than 5 and greater than 25 at the same time. You need to check for either of them, not both. Use logical OR operator ||
instead.
That said, it probably makes sense to check for the condition immediately after scanning the value. There's no point in calculating a lot of things and then, just throw them away, based on a condition that was already known long time ago.
Also, always check for the returned value of scanf()
to ensure successful scanning, otherwise you may end up using an indeterminate value.
回答2:
Besides the logic issue identified in Sourav's answer, there's a compile error.
The issue isn't immediately apparent from looking at the compiler output, however:
/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
Typically, you start by looking at the error at the top and fixing that, and see if that gets rid of others further down. In this case, the real clue is actually at the end.
You're missing an opening brace for your main
function. Add that and it will compile successfully and print a random password (without checking the length properly).
回答3:
It's not if(passwordLength < 5 && passwordLength >25)
but rather ||
The passwordLength
variable must be checked immediately after scanf
. If it fails the program must return
There's no need for counter
, use for
loop instead
In case you need to store the password for processing later, which is which I recommend, you should store it in a variable
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;
}
来源:https://stackoverflow.com/questions/42681149/trying-to-make-a-random-password-generator-in-c