Browse Source

Added comments, getUser()

- registration(), login(), getUser() are all commented
- registration() will now implements getUser() instead of attempting to
run its own check for existing username in use
- getUser() now does not require string since the string that is passed
is a shared variable

Segmentation fault error in login still persist.
Fixing-travis-yml
BinHong Lee 8 years ago
parent
commit
024a475ff9
1 changed files with 58 additions and 24 deletions
  1. +58
    -24
      LogInSystem.cpp

+ 58
- 24
LogInSystem.cpp View File

@@ -97,105 +97,136 @@ int main()
return 0; return 0;
} }


void getUser(string query)
void getUser()
{ {
//Duplicate the current stack to be checked through
stack<Person> temp = users; stack<Person> temp = users;

//Loop while the temporary stack is not empty
while (!temp.empty()) while (!temp.empty())
{ {
Person x = users.top();
if (x.getName() == query) currentUser = x;
//Check if the username match the query
//If so, set it as the currentUser
if (users.top().getName() == username) currentUser = users.top();

//Pop the checked user
users.pop(); users.pop();
cout << "User found";
} }


//Throw invalid_argument error to be caught if the person is not found
throw std::invalid_argument(""); throw std::invalid_argument("");
} }


void login() void login()
{ {
//If the user already has 3 fail attempt to login
if (wrongPass >= 3) if (wrongPass >= 3)
{ {
//Print error message and exit
cout << "Too much failed login attempt. The program will now be terminated." << endl; cout << "Too much failed login attempt. The program will now be terminated." << endl;
quit(); quit();
} }


try { try {
//Ask for username
cout << "Username:"; cout << "Username:";
cin >> username; cin >> username;


//Ask for password
cout << "Password:"; cout << "Password:";
cin >> password; cin >> password;


getUser(username);
//Get the user
getUser();


} catch (invalid_argument inae) { } catch (invalid_argument inae) {
//Print error message
cout << "Invalid username or password. Please try again."; cout << "Invalid username or password. Please try again.";
//Increase wrongPass count that indicate the amount of times of invalid
//credentials input by the user
wrongPass++; wrongPass++;
login(); login();
} }


//Check if the password is correct
if (!currentUser.checkPassword(password)) if (!currentUser.checkPassword(password))
{ {
//Print error message
cout << "Invalid username or password. Please try again."; cout << "Invalid username or password. Please try again.";
//Increase wrongPass count that indicate the amount of times of invalid
//credentials input by the user
wrongPass++; wrongPass++;
login(); login();
} }


//User is successfully logged in
loggedIn(); loggedIn();
} }


void registration() void registration()
{ {
bool available = true;
//Initialize boolean
bool available;


do do
{ {
cout << "Username : ";
cin >> username;

stack<Person> temp = users;

while(!temp.empty())
try
{ {
Person currentPerson = temp.top();

if (currentUser.getName() == username)
{
cout << "Username unavailable. Please try again." << endl;
available = false;
break;
}

temp.pop();
//Get username
cout << "Username: ";
cin >> username;

//Check if username is already in use
//If not, it will throw an invalid_argument error to be caught
getUser();

//Print error message
cout << "Username unavailable. Please try again." << endl;
//Set available as false and continue the loop
available = false;
//Catch the invalid_argument error thrown by getUser()
} catch (invalid_argument inae)
{
//Set available to true and quit the loop
available = true;
} }
} while (available == false); } while (available == false);


//Print success message
cout << "Username is available." << endl; cout << "Username is available." << endl;


do do
{ {
//Get password
cout << "Password : "; cout << "Password : ";
cin >> password; cin >> password;


//Confirm password
cout << "Confirm password :"; cout << "Confirm password :";
cin >> password2; cin >> password2;


//Print error message if both password is not the same
if (password != password2) if (password != password2)
cout << "Password unmatched. Please try again."; cout << "Password unmatched. Please try again.";
//Loop until both password input is the same
} while (password != password2); } while (password != password2);


//Get email
cout << "Email : "; cout << "Email : ";
cin >> email; cin >> email;


//Get phone number
cout << "Phone No. : "; cout << "Phone No. : ";
cin >> phoneNo; cin >> phoneNo;


//Create and push the new 'Person' into stack
Person newUser(username, password, email, phoneNo); Person newUser(username, password, email, phoneNo);
users.push(newUser); users.push(newUser);


//Print success message
cout << "Account is successfully registered." << endl; cout << "Account is successfully registered." << endl;


//Automatically login user
currentUser = newUser; currentUser = newUser;
loggedIn(); loggedIn();
} }
@@ -222,8 +253,11 @@ void loggedIn()
break; break;
case 0: case 0:
quit(); quit();
default:
cout << "Invalid option. Please try again." << endl;
cout << endl;
loggedIn();
} }

} }


void editCredentials() void editCredentials()


Loading…
Cancel
Save