C++帐户创建程序崩溃

C++ Account Creation Program Crashing

本文关键字:程序 崩溃 创建 C++      更新时间:2023-10-16

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

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.nn";
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

当然是未经测试的代码。

createAccount()createPass() 都不返回值,尽管具有非void返回类型。这意味着您的程序具有未定义的行为。在调用这些函数后,堆栈可能处于"某种方式",从而导致您观察到的崩溃。你的编译器应该警告你这一点:注意编译器的警告,然后你就会知道该怎么做。

确保具有 return 语句,以便可以将函数的结果传递回调用方。这些语句可能如下所示:

std::string createAccount()
{
   // ...
   return username;
}
std::string createPass()
{
   // ...
   return password1;
}

createPass() 的情况下,您还必须更改递归调用:

if (password1.length() < 10) {
   cout << "Password is not secure enough" << endl;
   cout << "Enter a password at least 10 characters: " << endl << endl;
   return createPass();
}
// ...
if (password2 != password1) { 
   cout << "Passwords do not match!" << endl << endl;
   return createPass();
}

...但我同意 John 的观点,你最好用一个漂亮的循环来替换这个递归。