I'm attempting to make a function that reads a user ID from user input, checks a file for that user ID, and logs them in if it is found. No password.
I have it somewhat working, but it seems to only work with the very first user ID, and it also isn't quite how it should be.
This is what I'm doing currently:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
bool logIn(string &login, bool found)
{
fstream accounts;
accounts.open("customers.txt");
for (int i = 0; !accounts.eof(); i++)
{
getline(accounts, user.userID, ' '); //Reads up to a space, should go to next line after but I don't know how to do that
cout << "Line 22. login = " << login << ", user.userID = " << user.userID << endl; //Test code. Tells me what's what and when
if (login == user.userID)
{
found = true;
return true;
}
else
; //Should point to next line? not sure.
}
if (!found)
return false;
}
int main()
{
//stuff leading up to this section of code
i = 0;
do {
cout << "Enter your four-digit user ID: ";
cin >> login;
cout << endl;
found = logIn(login, found);
if (found == true)
cout << "Successfully logged in as " << login << "." << endl;
else {
cout << "Failed to log in, " << 2 - i << " attempts remaining." << endl;
i++;
}
} while (i < 3 && found == false);
//stuff that happens if found is true
}
|
"customers.txt" reads as follows:
1111 John Doe 11 MyStreet MyCity 12345 NY 23000 200
1112 Kevin Moe 23 HisStreet HerCity 54321 MA 15 3500
1113 Mary Joe 78 SomeStreet NewTown 32451 PA 6500 12000
If my input is 1111, it works and logs me in as John Doe. Any other input doesn't work. It also currently reads the whole line while !accounts.eof() (ex. user.userID = 1111 first run, then John second run.) I want it to read the first 4 digits and skip the rest, which I think will fix it.
Sorry this is all over the place, I don't really know if I'm formulating this right.