私はログインシステムを試しましたが、クラスのユーザーオブジェクトを含むユーザーのベクトルのユーザーからのユーザー名は認識しません。ユーザーの情報と比較するために値を導入するために文字列を使いたかったのですが、char *を文字列と比較する方法がわかりません。ユーザークラスでchar *を使ってユーザー名とパスワードを書く必要があります。C++を使用したログインシステム
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と文字列enteredpassでのchar entereduserチャー enteredpassを変更しようとしましたが、私はthis..What後に比較するのか分からないが、私は私の関数は、ユーザ名とパスワードを認識させるために変更する必要がありますベクトル内の任意のユーザーから?
AuthenticationMenu()は全体のコードの別の関数である、私はあなたがoperator==
でC文字列を比較することはできません、あなたはstrcmp()
を使用する必要があり、それに
'=='と 'strcmp'の間には違いがあります。とにかく、C++では 'std :: string'を使うべきです。 – Angew
'*ユーザクラス、 'char *'' *を使ってユーザ名とパスワードを書く必要があります。 'std :: string'を使用してください。 – Biffen
あなたは 'char *'を使う必要があると誰が言いますか? –