C++ 随机密码生成器C++;
程序应提示用户输入5个字母的字符串。如果字符串不是5个字符,则会显示错误消息。如果密码为5个字符,则使用while循环,通过颠倒字符串中字符的顺序并从每个字符中减去15来生成密码,从而在新的字符串变量中生成密码。只能使用iostream、iomanip、cmath和字符串库 这个问题的唯一问题是构造while循环,该循环将原始字符串反转为新字符串,并从反转字符串中的每个字符中减去15来构造新密码C++ 随机密码生成器C++;,c++,string,loops,while-loop,C++,String,Loops,While Loop,程序应提示用户输入5个字母的字符串。如果字符串不是5个字符,则会显示错误消息。如果密码为5个字符,则使用while循环,通过颠倒字符串中字符的顺序并从每个字符中减去15来生成密码,从而在新的字符串变量中生成密码。只能使用iostream、iomanip、cmath和字符串库 这个问题的唯一问题是构造while循环,该循环将原始字符串反转为新字符串,并从反转字符串中的每个字符中减去15来构造新密码 #include <iostream> #include <string>
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
// declaring/initializing variables
string password, newPass;
int index;
// greeting message
cout << "-------------------------------" << endl
<< " John Password Generator " << endl
<< "-------------------------------" << endl;
// asking user for input
cout << "Please enter a 5 character word which will be used to generate a password: " << endl;
getline(cin,password);
// condition if password is less than or greather than 5 characters
if (password.length() != 5)
{
cout << "Sorry, but that is not a 5 character string. Program will terminate. \n\n"
<< "Thank you for using John Password Generator program.\n\n";
}
else
{
index = 0;
password = string(password.rbegin(), password.rend());
while (index < password.length())
{
index++;
}
}
return 0;
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
//声明/初始化变量
字符串密码,newPass;
整数指数;
//问候语
cout您需要迭代password
中的每个元素,并从中减去15:
index = 0;
password = string(password.rbegin(), password.rend());
while (index < password.length())
{
// Minus 15 from each letter in password
password[index] -= 15;
// We've finished with this letter, so increment the index
index++;
}
那么到目前为止,你有什么代码?很难在没有看到任何代码的情况下知道你需要什么帮助。考虑到你的问题,包括我已经发布了我目前所拥有的。所以你已经成功地颠倒了<代码>字符串,为什么你甚至需要<代码>而<代码>循环?看起来就像是冻结程序,SINC。eindex=0
并且永远不会更改。我们需要为这个问题实现while循环。我在else语句中所写的一切都是我遇到的问题。你需要回顾一下你对某些术语的理解。你试图做的绝对不像随机密码生成。你只是想一个基本的字符串操作输入字。非常感谢!
else
{
// Start from the end of the string, and deduct 15 from the character and append it to the start of the string
std::transform(password.rbegin(), password.rend(), password.begin(), [](unsigned char c){ return c - 15; });
}