C++ 帐户创建程序崩溃

标签 c++ function crash passwords account

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

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/

相关文章:

c++ - 从命令提示符执行 C++ 程序

c++ - Qt 5.3 : Accessing/returning/calling checkboxes that are created dynamically(? )

c++ - 将reinterpret_cast 指针传递为void *

visual-studio-2010 - JS文件在VS 2010中不断崩溃

c++ - 由于删除而崩溃(尝试处理异常...)

java - 由于以下原因导致Android Studio崩溃:java.lang.ArrayIndexOutOfBoundsException:length = 1;指数= 6

c++ - 使用不同的枚举参数时,VC++ 函数模板实例化错误 C2664

使用模板时的 C++ 类型检查

c++ - cin 对象 - C++

javascript - 使用 for 循环或函数从数组中删除或过滤值