gpt4 book ai didi

C++ 帐户创建程序崩溃

转载 作者:行者123 更新时间:2023-11-30 01:37:48 25 4
gpt4 key购买 nike

我遇到了一些障碍,不知道该怎么办。我正在尝试编写一个程序来创建一个带有用户名和密码的帐户并将其存储到一个文本文件中。但是,在使用该程序并输入用户名和密码数据时,程序在转到加密功能时崩溃了。感谢您的宝贵时间。

bool processNewAccount (string username, string password)
{
ofstream outFile;
string outFileName = "creds.txt";
outFile.open(outFileName.c_str(), fstream::app);
if (!outFile)
return false;
outFile << username << ":" << password << endl;
outFile.close();
return true;
}

void encrypt(string& s, int key)
{
for (int i = 0; i < s.length(); i++)
{
s[i] = (s[i] + key) % 127;
if (s[i] < 33)
{
s[i] = s[i] + 33;
}
}
}

string createAccount()
{
string firstName;
string lastName;
cout << "Creating an Account:" << endl;
cout << "First Name: ";
cin >> firstName;
cout << "Last Name: ";
cin >> lastName;
cout << endl;
string username;
if (lastName.length() <5)
{
for (int i = 0; username.length() <5; i++)
username = lastName + firstName.substr(0,i);
}
else
username = lastName.substr(0,4) + firstName.at(0);

for (int i = 0; i < 5; i++)
username.at(i) = tolower(username[i]);
}

string createPass ()
{
string password1;
string password2;
cout << "Create a Password that:" << endl << endl << "-Is at least 10 characters" << endl << "-Contains no spaces" << endl << endl << "Password: ";
cin >> password1;
if (password1.length() < 10)
{
cout << "Password is not secure enough" << endl;
cout << "Enter a password at least 10 characters: " << endl << endl;
createPass();
}
if (password1.length() >= 10)
cout << "Confirm Password: ";
cin >> password2;
if (password2 != password1)
{
cout << "Passwords do not match!" << endl << endl;
createPass();
}
}

int main()
{
string user;
string pass;
char menuOption;
do
{

printMenu();
cin >> menuOption;
switch (menuOption)
{
case '1': login();
break;
case '2':
user = createAccount();
pass = createPass();
encrypt(pass, 13);
processNewAccount (user, pass);
cout << "Welcome " << "Username: " << user << endl << "Email: " << user << "@student.edu" << endl;
break;
case '3': break;
default : cout << "\nInvalid entry. Please choose from the menu.|n";
break;
}
}
while (menuOption != 3);

cout << "\n Goodbye.\n\n";

return 0;
}

最佳答案

这是 createPass 的更好版本。它实际上返回密码,并且还使用循环来避免您进行的递归调用。

string createPass ()
{
string password1;
bool password_ok = false;
do
{
cout << "Create a Password that:" << endl << endl << "-Is at least 10 characters" << endl << "-Contains no spaces" << endl << endl << "Password: ";
cin >> password1;
if (password1.length() < 10)
{
cout << "Password is not secure enough" << endl;
cout << "Enter a password at least 10 characters: " << endl << endl;
}
else
{
cout << "Confirm Password: ";
string password2;
cin >> password2;
if (password2 != password1)
{
cout << "Passwords do not match!" << endl << endl;
}
else
{
password_ok = true;
}
}
}
while (!password_ok);
return password1;
}

正如 Lightness Races in Orbit 指出的那样,您需要以类似的方式修复 createAccount

当然是未经测试的代码。

关于C++ 帐户创建程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49058291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com