2017-01-12 4 views
0

私はログインシステムを試しましたが、クラスのユーザーオブジェクトを含むユーザーのベクトルのユーザーからのユーザー名は認識しません。ユーザーの情報と比較するために値を導入するために文字列を使いたかったのですが、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()を使用する必要があり、それに

+0

'=='と 'strcmp'の間には違いがあります。とにかく、C++では 'std :: string'を使うべきです。 – Angew

+1

'*ユーザクラス、 'char *'' *を使ってユーザ名とパスワードを書く必要があります。 'std :: string'を使用してください。 – Biffen

+2

あなたは 'char *'を使う必要があると誰が言いますか? –

答えて

2

をリダイレクトする必要があります。 2ポインタ上で==を使用すると、そのアドレスだけが比較されます。

しかし、あなたはすべて一緒にCの文字列(とnew)を使用して停止し、std::stringの使用を開始C++を使用しているので、

関連する問題