试图制作一个随机的“密码生成器".在C中 [英] Trying to make a random "password generator" in C

查看:114
本文介绍了试图制作一个随机的“密码生成器".在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;
}

长话短说,我试图使该程序正常工作,但由于某些原因,我不知道如何使其正常工作.谢谢.

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.

推荐答案

首先,查看您要使用的逻辑.

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!!");
}

从来没有值,可以同时小于5 .您需要检查两者中的任何一个,而不是两者都检查.改用逻辑OR运算符||.

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.

此外,请始终检查返回的值scanf()以确保扫描成功,否则可能会使用不确定的值.

Also, always check for the returned value of scanf() to ensure successful scanning, otherwise you may end up using an indeterminate value.

这篇关于试图制作一个随机的“密码生成器".在C中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆