C ++帐户创建程序崩溃

时间:2018-03-01 20:35:28

标签: 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;
}

2 个答案:

答案 0 :(得分:2)

createAccount()createPass()都没有返回值,尽管有非void返回类型。这意味着您的程序具有未定义的行为。可能堆栈是在某种程度上#34;在调用这些功能后,导致您发现了崩溃。您的编译器应该已经警告过您:注意编译器的警告,然后您就会知道该怎么做。

确保您有一个return声明,以便您可以传递您的功能&#39;结果回到来电者。这些陈述可能如下所示:

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的意见,你最好用a nice loop取代这个递归。

答案 1 :(得分:2)

这是一个更好的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;
}

正如Orbit中的Lightness Races指出,你需要以类似的方式修复createAccount

当然未经测试的代码。