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
I am expecting to log in and see the username is a little message