【发布时间】:2021-07-01 22:16:29
【问题描述】:
我正在编写一个生成长度为 10 个字符的密码的代码。密码要求至少为 2 个大写字母、2 个数字、一个随机索引中的符号,其余为小写字母。该代码将询问用户 2 个字符,并将在给定的用户输入中生成字符。我遇到的麻烦是我不知道如何让它更加随机化。目前,例如,代码将大写和小写一起打印。如何使这些字符最终出现在密码的随机索引中?
这是我的代码:
#include <iostream>
#include<ctime>
using namespace std;
int main(){
char minInput, maxInput;
cout<<"Enter a letter (for minimum value)"<< endl;
cin>>minInput;
char min = toupper(minInput);
cout<<"Enter a letter (for maximum value)"<< endl;
cin>>maxInput;
char max = toupper(maxInput);
cout<<"Your password is: ";
srand(time(0));
for(int i = 0; i < 2; i++){
char upper = rand() % (max - min + 1) + min;
cout<<upper;
}
for(int i = 0; i < 2; i++){
char digit = rand() % ((57) - (48) + 1) + (48);
cout<<digit;
}
for(int i = 0; i < 5; i++){
char lower = rand() % ((max + 32) - (min + 32) + 1) + (min + 32);
cout<<lower;
}
for(int i = 0; i < 5; i++){
char symbol = rand() % ((47) - (33) + 1) + (33);
cout<<symbol;
}
return 0;
}
【问题讨论】:
标签: c++ string loops passwords character