使用c ++登录系统

时间:2017-01-12 11:21:41

标签: c++ oop login

我尝试了一个登录系统,但它无法识别来自用户向量的任何用户的任何用户名,其中包括来自类用户的用户对象;我想使用字符串来引入值以与用户的信息进行比较,但我不知道如何将char *与字符串进行比较,因为在User类中,我需要使用char *来编写用户名和密码;

class User{

private:

char *username;
char *password;
int age;

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

void Log(User users[])
{
int nrusers = 3;
char * entereduser;
char *enteredpass;
char buffer[20];

cout << "username: ";
entereduser = new char[strlen(buffer) + 1];
strcpy(enterduser, buffer);
cin >> entereduser;
cout << "password: ";
enteredpass = new char[strlen(buffer) + 1];
strcpy(enteredpass, buffer);
cin >> enteredpass;
for (int i = 0; i < nrusers; i++)
{
    if (entereduser == users[i].getUsername())
    {
        if (entereduser == users[i].getPassword())
        {
            cout << "Authentication successful!" << endl;
            system("pause");
            system("cls");
            AuthenticationMenu(users);
        }
        else
        {
            cout << "Wrong password!" << endl;
            cout << endl;
            int opt;
            cout << "Try again" << endl;
            cout << "Go to first page" << endl;
            cout << "Enter you option: ";
            cin >> opt;
            switch (opt)
            {
            case 1:
                system("pause");
                system("cls");
                Log(users);
                break;
            case 2:
                system("pause");
                system("cls");
                AuthenticationMenu(users);
                break;
            default:
                cout << "Choose from 1 to 2: ";
                cin >> opt;
            }

        }

    }
    else
    {
        cout << "Wrong username" << endl;
        cout << endl;
        int opt;
        cout << "Try again" << endl;
        cout << "Go to first page" << endl;
        cout << "Enter you option: ";
        cin >> opt;
        switch (opt)
        {
        case 1:
            system("pause");
            system("cls");
            Log(users);
            break;
        case 2:
            system("pause");
            system("cls");
            AuthenticationMenu(users);
            break;
        default:
            cout << "Choose from 1 to 2: ";
            cin >> opt;
        }

    }
}

void main()
{
User u2("Jamie15","t3456",20);
User u3("Chris","fgh6",22);

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

Log(users);

}

我尝试使用字符串entereduser和字符串enterpass更改char entereduser和char enterpass,但我不知道如何在此之后进行比较。我应该更改以使我的函数识别用户名和来自矢量中任何用户的密码?

AuthenticationMenu()是整个代码的另一个功能,我需要重定向到它

1 个答案:

答案 0 :(得分:2)

您无法将C字符串与operator==进行比较,您必须使用strcmp()。在2个指针上使用==仅比较它们的地址。

但是,因为您正在使用C ++停止使用C字符串(和new),并开始使用std::string