每次用户几乎找到密码时都尝试生成不同的密码
Trying to generate a different password everytime user nearly finds the password
首先,英语不是我的第一语言,所以如果您有什么不明白的地方,请告诉我。
赋值是,有一个9位数字,只能是整数的密码,例如:385987231.The用户将输入一个9位数字input.Everytime用户接近guessing/finding密码,密码将是 changed.If 密码和用户输入匹配的 8 位数字,密码将是 changed.The 数字的顺序不是 important.Only 重要的是如果八位数字匹配,密码变化。
例如:密码为123456789,用户输入223456789,此时密码自行更改。
有一个hiddenPassword是在开始创建的,还有一个Password是用户输入的。
我已经让 passwordChecker 和 passwordCreater functions.passwordChecker 检查数组中的 2 个数字是否匹配,每次匹配时计数器变为 1 up.If 计数器为 8,然后 hiddenPassword 更改。如果不是,则不会改变。
passwordCreater 函数创建随机 password.Because 它在 char 变量中我添加 48 使其成为参考 ASCII Table 的整数,问题开始了。
即使函数创建密码,它也不会 return 密码并且密码不正确。
而且我不认为 passwordChecker 有效,因为它是一个无效函数,所以它不会 return anything.When 我在 passwordChecker 中调用 passwordCreater 什么都没有,什么也没有 goes.Sadly.
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
char passwordCreater()
{
srand((unsigned)time(0));
char random_integer[20];
for (int index=0; index<9; index++)
{
random_integer[index] = (rand()%10)+1;
random_integer[index]=random_integer[index]+48;
cout << random_integer << endl;
}
return random_integer[9];
}
void passwordChecker (char a[9], char b[9])
{
int counter;
for (int i=0;i++;i<10)
{
if (a[i]==b[i])
{
counter=counter+1;
}
else
{
counter=counter;
}
}
printf("%d \n",counter);
if (counter==8)
{
b[9]==passwordCreater();
}
else if (counter==9)
{
printf("Password is right!");
}
else
{
printf("Password is wrong!");
return;
}
}
int main()
{
char hiddenPassword[9];
hiddenPassword[9]==passwordCreater();
char password[9];
printf("%s \n",hiddenPassword);
printf("------------------------");
printf("\n PASSWORD: ");
printf("\n------------------------\n");
scanf("%s", &password);
printf("%s \n",password);
passwordChecker(password,hiddenPassword);
return 0;
}
这是一个解决方案:单独生成随机种子一次。
相当简单地生成随机密码,然后通过分析 2 部分.
来检查用户输入
#1: 检查
之前的长度是否相同
#2:比较输入密码中的每一位数字,并据此进行反馈。如果 Counter
显示为 8,它会创建一个新密码。享受吧。
编辑: 我使用 <random>
header 而不是 srand(time(0))
和 rand()
来消除偏见.
#include <iostream>
#include <ctime>
#include <random>
#include <string>
#include <vector>
using namespace std;
mt19937 Generator;
uniform_int_distribution<int> Distribution(0,9);
void GenerateRandomSeed() {
Generator.seed(time(0));
}
vector<char> CreatePassword() {
cout << "Hidden password: ";
vector<char> Password;
for(int i = 0; i < 9; ++i) {
Password.push_back(Distribution(Generator) + 48);
cout << Password[i];
}
cout << endl;
return Password;
}
bool isPasswordCorrect(string & InputPassword, vector<char> & HiddenPassword) {
if(InputPassword.length() != HiddenPassword.size()) {
cout << "Password is wrong!" << endl;
return false;
}
int Counter = 0;
for(int i = 0; i < InputPassword.length(); ++i) {
if(InputPassword[i] == HiddenPassword[i])
++Counter;
}
if(Counter == 8) {
HiddenPassword = CreatePassword();
cout << "Password was close, new hidden password has been generated" << endl;
return false;
} else if (Counter == 9){
cout << "Password is right!" << endl;
return true;
}
cout << "Password is wrong!" << endl;
return false;
}
int main() {
GenerateRandomSeed();
string InputPassword;
vector<char> HiddenPassword;
HiddenPassword = CreatePassword();
do {
cout << "Input correct password please: ";
cin >> InputPassword;
} while (!isPasswordCorrect(InputPassword, HiddenPassword));
return 0;
}
首先,英语不是我的第一语言,所以如果您有什么不明白的地方,请告诉我。
赋值是,有一个9位数字,只能是整数的密码,例如:385987231.The用户将输入一个9位数字input.Everytime用户接近guessing/finding密码,密码将是 changed.If 密码和用户输入匹配的 8 位数字,密码将是 changed.The 数字的顺序不是 important.Only 重要的是如果八位数字匹配,密码变化。
例如:密码为123456789,用户输入223456789,此时密码自行更改。
有一个hiddenPassword是在开始创建的,还有一个Password是用户输入的。
我已经让 passwordChecker 和 passwordCreater functions.passwordChecker 检查数组中的 2 个数字是否匹配,每次匹配时计数器变为 1 up.If 计数器为 8,然后 hiddenPassword 更改。如果不是,则不会改变。
passwordCreater 函数创建随机 password.Because 它在 char 变量中我添加 48 使其成为参考 ASCII Table 的整数,问题开始了。
即使函数创建密码,它也不会 return 密码并且密码不正确。 而且我不认为 passwordChecker 有效,因为它是一个无效函数,所以它不会 return anything.When 我在 passwordChecker 中调用 passwordCreater 什么都没有,什么也没有 goes.Sadly.
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
char passwordCreater()
{
srand((unsigned)time(0));
char random_integer[20];
for (int index=0; index<9; index++)
{
random_integer[index] = (rand()%10)+1;
random_integer[index]=random_integer[index]+48;
cout << random_integer << endl;
}
return random_integer[9];
}
void passwordChecker (char a[9], char b[9])
{
int counter;
for (int i=0;i++;i<10)
{
if (a[i]==b[i])
{
counter=counter+1;
}
else
{
counter=counter;
}
}
printf("%d \n",counter);
if (counter==8)
{
b[9]==passwordCreater();
}
else if (counter==9)
{
printf("Password is right!");
}
else
{
printf("Password is wrong!");
return;
}
}
int main()
{
char hiddenPassword[9];
hiddenPassword[9]==passwordCreater();
char password[9];
printf("%s \n",hiddenPassword);
printf("------------------------");
printf("\n PASSWORD: ");
printf("\n------------------------\n");
scanf("%s", &password);
printf("%s \n",password);
passwordChecker(password,hiddenPassword);
return 0;
}
这是一个解决方案:单独生成随机种子一次。
相当简单地生成随机密码,然后通过分析 2 部分.
来检查用户输入#1: 检查
之前的长度是否相同#2:比较输入密码中的每一位数字,并据此进行反馈。如果 Counter
显示为 8,它会创建一个新密码。享受吧。
编辑: 我使用 <random>
header 而不是 srand(time(0))
和 rand()
来消除偏见.
#include <iostream>
#include <ctime>
#include <random>
#include <string>
#include <vector>
using namespace std;
mt19937 Generator;
uniform_int_distribution<int> Distribution(0,9);
void GenerateRandomSeed() {
Generator.seed(time(0));
}
vector<char> CreatePassword() {
cout << "Hidden password: ";
vector<char> Password;
for(int i = 0; i < 9; ++i) {
Password.push_back(Distribution(Generator) + 48);
cout << Password[i];
}
cout << endl;
return Password;
}
bool isPasswordCorrect(string & InputPassword, vector<char> & HiddenPassword) {
if(InputPassword.length() != HiddenPassword.size()) {
cout << "Password is wrong!" << endl;
return false;
}
int Counter = 0;
for(int i = 0; i < InputPassword.length(); ++i) {
if(InputPassword[i] == HiddenPassword[i])
++Counter;
}
if(Counter == 8) {
HiddenPassword = CreatePassword();
cout << "Password was close, new hidden password has been generated" << endl;
return false;
} else if (Counter == 9){
cout << "Password is right!" << endl;
return true;
}
cout << "Password is wrong!" << endl;
return false;
}
int main() {
GenerateRandomSeed();
string InputPassword;
vector<char> HiddenPassword;
HiddenPassword = CreatePassword();
do {
cout << "Input correct password please: ";
cin >> InputPassword;
} while (!isPasswordCorrect(InputPassword, HiddenPassword));
return 0;
}