Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login system using classes c++

Hello I try to implement a login system but I tried so many times and I still don't get it. I have a class user and a class login, and a test function..i tried this after a tutorial, but there it was a default username and password, and only that one worked in login, but i need to have the possibility of each user's login;

class User{

private:

char *username;
char *password;

public:
User(){}
User(char *,char *p){...}
~User();
friend ostream &operator<<(ostream &output, User &u);
char* getUsername(){...};
char* getPassword(){...};
};

class Loginn
{
public:
    string username;
    string password;

    Loginn()
    {
        username = "\0";
        password = "\0";
    }

    };

bool Loginn::ptlogin(User users[])
{
    int x;
    string ch_username = users[x].getUsername;
    string ch_password = users[x].getPassword;
    cout << "Enter username::\t";
    cin >> username;
    cout << "Enter password::\t";
    cin >> password;


    if ((username == ch_username)&&(password =ch_password))
    {
        return true;
    }
    else
    {
        return false;
    }
}


void MainMenu(User users[]);
{
//this is from where I want to call login function, not from main()
}

void main()
{


    User u2("Jamie15","t3456");
    User u3("Chris","fgh6");

User users[2]={u2,u3};

MainMenu(users);


}

I want my login system to test if the username and password entered match to a user's password and username, my users are u2 and u3. Thank you! Also, the data type from user needs to stay char, i cannot change it.

like image 968
Anais Avatar asked Sep 15 '26 18:09

Anais


1 Answers

In your Loginn::ptlogin method, the index variable x is not initialized.

The expression:
users[x].getUsername;

could return anything from memory or access memory that you don't have access to; all depending on the random, uninitialized value of x index.

I highly recommend using std::vector instead of arrays. Also, pass the index you are referring to (or at least initialize it).

Edit 1: The MainMenu function
In order to call the login function, you either need to have an instance (variable) of the Login class or make the login function as static.

void MainMenu(std::vector<User>& users)
{
  Loginn instance;
  instance.ptlogin(users);
}

An alternative is to create the login instance in main and pass it to MainMenu:

void MainMenu(std::vector<User>& users,
              Loginn& instance);

int main(void)
{
  std::vector<User> users;
  Loginn instance;
  MainMenu(users, instance);
  return EXIT_SUCCESS;
}

Edit 2: A static method
If you have a static method, you don't need an instance to call the method:

class Loginn
{
  public:
    static void ptlogin(std::vector<User>& users);
};

void MainMenu(std::vector<User>& users)
{
  Loginn::ptLogin(users);
}
like image 78
Thomas Matthews Avatar answered Sep 17 '26 18:09

Thomas Matthews



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!