Browse Source

Person.cpp & Person.h implementation

- Person is now used and called in LogInSystem.cpp
- A stack is introduced to handle all the users (Person)

- Unresolved “Segmentation fault : 11”
Fixing-travis-yml
BinHong Lee 8 years ago
parent
commit
2311546377
4 changed files with 138 additions and 63 deletions
  1. +126
    -61
      LogInSystem.cpp
  2. +10
    -0
      Person.cpp
  3. +1
    -0
      Person.h
  4. +1
    -2
      database.txt

+ 126
- 61
LogInSystem.cpp View File

@@ -1,10 +1,11 @@
/* /*
* Written by : Lee Bin Hong
* Written by : BinHong Lee
* Last edited : 4/12/2016 * Last edited : 4/12/2016
*/ */


#include <iostream> #include <iostream>
#include <string> #include <string>
#include <stack>
#include <fstream> #include <fstream>
#include "Person.cpp" #include "Person.cpp"
using namespace std; using namespace std;
@@ -14,18 +15,19 @@ void login();
void loggedIn(); void loggedIn();
void registration(); void registration();
void editCredentials(); void editCredentials();
void update(Person);
void chgUsername(); void chgUsername();
void chgPassword(); void chgPassword();
void chgEmail(); void chgEmail();
void chgPhoneNo(); void chgPhoneNo();
void quit(); void quit();


const int size = 10000;
Person person[size];
string username[size];
string password[size];
string email[size];
string phoneNo[size];
stack<Person> users;
Person currentUser;
string username;
string password;
string email;
string phoneNo;
int counter; int counter;
int x = 0; int x = 0;
int wrongPass = 0; int wrongPass = 0;
@@ -37,10 +39,13 @@ int main()


while (!fin.eof()) while (!fin.eof())
{ {
fin >> username[x] >> password[x] >> email[x] >> phoneNo[x];
x++;
fin >> username >> password >> email >> phoneNo;
Person temp(username, password, email, phoneNo);
users.push(temp);
} }


users.pop();

cout << "Please choose one of the following options :" << endl; cout << "Please choose one of the following options :" << endl;
cout << "Log In - 1" << endl; cout << "Log In - 1" << endl;
cout << "Registration - 2" << endl; cout << "Registration - 2" << endl;
@@ -61,40 +66,55 @@ int main()


ofstream fout("database.txt"); ofstream fout("database.txt");


for (int i = 0; i <= x; i++)
while (!users.empty())
{ {
fout << username[i] << " " << password[i] << " " << email[i] << " " << phoneNo[i] << " " << endl;
fout << users.top().getName() << " " << users.top().getPassword() << " " << users.top().getEmail() << " " << users.top().getPhoneNo() << " " << endl;
users.pop();
} }

return 0; return 0;
} }


int getUsernameCounter(string query, int size)
void getUser(string query)
{ {
for (int i = 0; i < size; i++)
stack<Person> temp = users;
while (!temp.empty())
{ {
if (query == username[i]) return i;
Person x = users.top();
if (x.getName() == query) currentUser = x;
users.pop();
cout << "User found";
} }
return -1;

throw std::invalid_argument("");
} }


void login() void login()
{ {

if (wrongPass >= 3) if (wrongPass >= 3)
{ {
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();
} }


string usrnme, pswd;
cout << "Username:";
cin >> usrnme;
try {
string usrnme, pswd;
cout << "Username:";
cin >> username;


counter = getUsernameCounter(usrnme, x);
cout << "Password:";
cin >> password;


cout << "Password:";
cin >> pswd;
getUser(username);


if (pswd != password[counter])
} catch (invalid_argument inae) {
cout << "Invalid username or password. Please try again.";
wrongPass++;
login();
}

if (!currentUser.checkPassword(password))
{ {
cout << "Invalid username or password. Please try again."; cout << "Invalid username or password. Please try again.";
wrongPass++; wrongPass++;
@@ -106,32 +126,35 @@ void login()


void registration() void registration()
{ {
string usrnme;
string pswd = "2"; string pswd = "2";
string pswd2 = "3"; string pswd2 = "3";
bool available = false;
bool available = true;


while (available == false)
do
{ {
available = true;

cout << "Username : "; cout << "Username : ";
cin >> usrnme;
cin >> username;


for (int i = 0; i < x; i++)
stack<Person> temp = users;

while(!temp.empty())
{ {
if (username[i] == usrnme)
Person currentPerson = temp.top();

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

temp.pop();
} }
}
} while (available == false);


cout << "Username is available." << endl; cout << "Username is available." << endl;
username[x] = usrnme;


while (pswd != pswd2)
do
{ {
cout << "Password : "; cout << "Password : ";
cin >> pswd; cin >> pswd;
@@ -141,22 +164,22 @@ void registration()


if (pswd != pswd2) if (pswd != pswd2)
cout << "Password unmatched. Please try again."; cout << "Password unmatched. Please try again.";
}
} while (pswd != pswd2);


password[x] = pswd;
password = pswd;


cout << "Email : "; cout << "Email : ";
cin >> email[x];
cin >> email;


cout << "Phone No. : "; cout << "Phone No. : ";
cin >> phoneNo[x];

cout << "Account is successfully registered." << endl;
cin >> phoneNo;


counter = x;
Person newUser(username, password, email, phoneNo);
users.push(newUser);


x++;
cout << "Account is successfully registered." << endl;


currentUser = newUser;
loggedIn(); loggedIn();
} }


@@ -173,7 +196,7 @@ void loggedIn()
switch (choice) switch (choice)
{ {
case 1: case 1:
cout << "Username : " << username[counter] << "\nEmail : " << email[counter] << "\nPhone No. : " << phoneNo[counter] << endl;
cout << "Username : " << currentUser.getName() << "\nEmail : " << currentUser.getEmail() << "\nPhone No. : " << currentUser.getPhoneNo() << endl;
loggedIn(); loggedIn();
break; break;
case 2: case 2:
@@ -216,62 +239,104 @@ void editCredentials()
} }
} }


void chgUsername()
void update(Person newUser)
{ {
bool available = false;
string usrnme;
stack<Person> newUsers;


while (available == false)
while (!users.empty())
{ {
available = true;
if (users.top().getName() == currentUser.getName())
{
newUsers.push(newUser);
}
else
{
newUsers.push(users.top());
}

users.pop();
}

users = newUsers;
}

void chgUsername()
{
bool available = true;

Person newUsers;


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

stack<Person> temp = users;


for (int i = 0; i < x; i++) for (int i = 0; i < x; i++)
{ {
if (username[i] == usrnme)
if (temp.top().getName() == username)
{ {
cout << "Username unavailable. Please try again." << endl; cout << "Username unavailable. Please try again." << endl;
available = false; available = false;
break;
} }

temp.pop();
} }
}
} while (available == false);


username[counter] = usrnme;
cout << "Username is available." << endl;
Person newPerson(username, currentUser.getPassword(), currentUser.getEmail(), currentUser.getPhoneNo());
update(newPerson);


loggedIn();
cout << "Username is updated." << endl;
} }


void chgPassword() void chgPassword()
{ {
string pass;

cout << "Please input the current password : "; cout << "Please input the current password : ";
cin >> pass;
cin >> password;


if (pass != password[counter])
while (!currentUser.checkPassword(password))
{ {
cout << "Wrong password. Please try again."; cout << "Wrong password. Please try again.";
wrongPass++; wrongPass++;
chgPassword();

cout << "Please input the current password : ";
cin >> password;
} }

string password2;

do {
cout << "Please input the new password : ";
cin >> password;

cout << "Please confirm your new password : ";
cin >> password2;
} while(password2 != password);

currentUser.setPassword(password);
update(currentUser);
} }


void chgEmail() void chgEmail()
{ {
cout << "Please input the new email : "; cout << "Please input the new email : ";
cin >> email[counter];
cin >> email;


loggedIn();
currentUser.setEmail(email);
update(currentUser);
} }


void chgPhoneNo() void chgPhoneNo()
{ {
cout << "Please input the new phone no. : "; cout << "Please input the new phone no. : ";
cin >> phoneNo[counter];
cin >> phoneNo;


loggedIn();
currentUser.setPhoneNo(phoneNo);
update(currentUser);
} }


void quit() void quit()


+ 10
- 0
Person.cpp View File

@@ -51,11 +51,21 @@ void Person::setPhoneNo(string phoneNo)
this->phoneNo = phoneNo; this->phoneNo = phoneNo;
} }


string Person::getPhoneNo()
{
return phoneNo;
}

void Person::setPassword(string password) void Person::setPassword(string password)
{ {
this->password = password; this->password = password;
} }


string Person::getPassword()
{
return password;
}

//"password" do not have getter but only a checker instead //"password" do not have getter but only a checker instead
bool Person::checkPassword(string password) bool Person::checkPassword(string password)
{ {


+ 1
- 0
Person.h View File

@@ -20,5 +20,6 @@ class Person
void setPhoneNo(string); void setPhoneNo(string);
string getPhoneNo(); string getPhoneNo();
void setPassword(string); void setPassword(string);
string getPassword();
bool checkPassword(string); bool checkPassword(string);
}; };

+ 1
- 2
database.txt View File

@@ -1,4 +1,3 @@
binhonglee bhlee03655 binhonglee@hotmail.com +13479618886 binhonglee bhlee03655 binhonglee@hotmail.com +13479618886
binhong binhong binhong@binhong.me +14802527013 binhong binhong binhong@binhong.me +14802527013
who what why@binhong.me +where

Loading…
Cancel
Save