C++ Login System help.

I'm new to C++.
I have a text file named idpw.txt which is storing id and password
e.g
admin password
account1 abc
john hello

Here's my code so far:

bool login()
{
 string user[10],password[10], user1, password1;
 ifstream myfile("idpw.txt");
 
 cout << "Please enter your username" << endl;
 cin >> user1;
 cout << "Please enter your password" << endl;
 cin >> password1;
 
 int i=0;
 int j=0;
 while (!myfile.eof())
 {
  myfile >> user >> password;
  i++;
 }
 myfile.close();
 
 for (j=0; j<i; j++)
 {
  if (user1 == user[j] && password1 == password[j])
  {
   cout << "Successfully Login" << endl;
   return true;
  }
 }
}

I can login successfully, but here's the problem.
I want to do it in a sense that:
1) When username is correct, but password is wrong, error message will be : "Wrong Password"
2) When username is not in the list, error message will be : "No such user exist"
3) If a user typed in wrong password for 3 times, account will be locked.

Am I doing the login function right? The for loop for int j seems really wrong.
Is there a way to do this?

Thanks alot in advance.

bool login()
{
 string user[10],password[10], user1, password1;
 ifstream myfile("idpw.txt");
 
 cout << "Please enter your username" << endl;
 cin >> user1;
 cout << "Please enter your password" << endl;
 cin >> password1;
 
 int i=0;
 int j=0;
 while (!myfile.eof())
 {
  myfile >> user >> password;
  i++;
 }
 myfile.close();
 
 bool hasUser=false;

 for (j=0; j<i; j++)
 {
  if (user1 == user[j]) { 
     hasUser=true; 
     if(password1 == password[j]) {
       cout << "Successfully Login" << endl;
       return true;
     }
 }
 
 if(hasUser) { cout << "Wrong Password" << endl;
 else { cout << "No such user exist" << endl;

 return false;
}

to lock an account, you'd probably have to append the file to which accounts were locked ... or add "locked" after the "username password" in the text file.

You can loop through the whole thing 3 times and lock through it at the end if that's the case

1 Like

Thanks!

I'll go try and figure the locking up of account.

Where would you put the loop if the user enters 3 times wrong username or password, how will it display again "Please enter your username" etc.
Where will the loop be?
Sorry I am new and confused...:wall:
Doesnt have to lock the account , just exit from the application.