0

This is some log in code to my project. The file contains the users username, password, email, address and phone number In this order. The code reads the file and takes each lines data and stores it in a struct, this struct is then stored within a vector. The user is then asked to enter their username and password. The program checks this agaisnt the stored structs in the vectors and if they match logs the user in. However i have the problem where for some reason the code is getting the username and password of the first row fine, but on the second row it uses some data from the first row and some from the second row.

Below is the code

struct CargoAccData
{
    string email;
    string address;
    string username;
    string password;
    string phoneNumber;
};


void LogInSystem::LogInCargo() {
    string fileName = "cargoAccount.txt";
    ifstream cargoAccFile(fileName);
    Validation val;
    string attemptedUsername;
    string attemptedPassword;
    bool loopControl = true;
    int count = 0;
    if (cargoAccFile.is_open())
    {
        string line;
        while (!cargoAccFile.eof())
        {
            getline(cargoAccFile, line);
            count++;
        }
    }
    cargoAccFile.close();
    ifstream cargoAccFile2(fileName);
    std::vector<CargoAccData> accounts;
    while (loopControl)
    {
        if (!cargoAccFile2)
        {
            cout << "This file doesn't exist \n";
            loopControl = false;
        }
        for (size_t i = 0; i < count; i++)
        {
            CargoAccData account;
            cargoAccFile2 >> account.email >> account.address >> account.username >> account.password >> account.phoneNumber;
            accounts.push_back(account);
        }
        cout << "Please Enter your Username: \n";
        cin >> attemptedUsername;
        cout << "\nPlease Enter Your Password: \n";
        cin >> attemptedPassword;
        int z = 0;
        bool isCorrect = false;
        while (z < count)
        {
            cout << accounts[z].username << endl;
            cout << accounts[z].password << endl;
            if (attemptedUsername == accounts[z].username && attemptedPassword == accounts[z].password)
            {
                cout << "Welcome to the E-market {" << attemptedUsername << "}!" << endl;
                loopControl = false;
                isCorrect = true;
                break;
            }
            else`your text`
            {
                z++;
            }
        }
        if (isCorrect == true)
        {
            // This is where the main profile will lead to
        }
        else
        {
            cout << "Username or password incorrect, Try Again" << endl;
        }
    }

}

This below is the files contents -

test test test test test test test test
mrjoeyrules VerySecure1 mrjoeyrules@gmail.com 3 07879652851

This is an image of the CLI outputs

I am expecting to log in and see the username is a little message

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • 2
    You've got a `while (file.eof())` style loop for reading a file. That causes problems because the loop body is executed once after the end of file is reached (even if no data is read). If you do a search, you'll find plenty of information online about why such an approach can cause problems, up to and including undefined behaviour - and, just as important, alternatives to avoid such problems. The fact you're using such a loop to count lines in a file, and then using that count for control of a loop that reads the same file again, means you are virtually CERTAIN to run into such problems – Peter Apr 16 '23 at 14:54

1 Answers1

2

how many "test" entries are there on the first line?

You don't actually read a line of data and parse it. You read 5 entries, and then the next 5 entries and so on. To fix this, either correct the data in the input (and all input data should be verified to heck because sometime, someone will add a little extra and end up doing very bad things to your application) or discard all extraneous data between reading the required data and the end-of-line marker. This may help you there.

gbjbaanb
  • 51,617
  • 12
  • 104
  • 148