Calling all C++ ninjas!

Hi guys!
I am a bit stuck I was wondering if any of you C++ ninjas could tell me why my function is not writing the string passwordin out to file?

I have been racking my brain for a good long while now, feels like doom.

any help would be awesome :smiley:

void build_complete_login(int &accountnumberin, int &pinin, string &usernamein, string &passwordin)
{

    // Stream class to read from files
    ifstream input_stream("../2FA_users.txt");
    // Stream class to write on files
    ofstream output_stream("../2FA_usersTmp.txt");

    // Vector container representing array that can change in size.
    vector<string> delimited_line;
    // Strings for manipulation.
    string line, word;
    // Set user found to false.
    bool account_number_found = false;

    // get line input_stream into line.
    while (getline(input_stream, line))
    {
        stringstream line_stream(line);

        while (getline(line_stream, word, ','))
            delimited_line.push_back(word);

        // If index 1 [AccountNumber] == account number input.
        if (stoi(delimited_line[1]) == accountnumberin && stoi(delimited_line[3]) == pinin)
        {
            if (duplicateUsernameCheck(usernamein) == false)
            {
                // index 4 [Username] is now local input new_username
                delimited_line[4] = usernamein;
                account_number_found = true;
            }

            if(passwordCheck(passwordin) == true)
            {
                cout << "pass is here!:" << passwordin << endl;
                delimited_line[5] = passwordin;

            }

            if(duplicateUsernameCheck(usernamein) == true)
            {
                cout << "Username already taken!: " << endl;
            }

        }

        for (const auto& str : delimited_line)
            output_stream << str << ",";
        output_stream << "\n";

        delimited_line.clear();
    }
    if (!account_number_found)
        cout << "Account number not found" << endl;

    input_stream.close();
    output_stream.close();


}
1 Like

Before using array indexes 1 and 3 you need to ensure that they exist. Unlike scripting languages C++ is hard-core and does not check by default (if you used delimited_line.at(1) instead it would throw an error exception.

So add a check that delimited_line has length 4.

You may also be having problems with spaces in the input, the getline with a ‘,’ might be putting spaces in where you do not expect them.

5 Likes

Brilliant! thank you mate this actually worked, I basically made a null_username and null_password and then delim size == 5 :smiley: I know its hacky but there would be passwords and users there anyway and this is my first every coursework in C++ so.

Thank you!

I cannot wait to go back to python ._.! :smiley: