Compare Two pointer Arrays One of them from .h and one of them from cin

I am newbie. I have got a school project that there is a header file and it has got 82 usernames. Like

char *usernames[] ={a1,a2,a3,.... a82};
char *passwords[] =[p1,p2,...p82);

And I have finished large amount of my project but still I couldn't write an usefull code for login stage. My code has to do take username and then asking for password. For this stage I think basicly that loop:

char *usernamecheck;
char *passwordcheck;
cout<<"Please login. \n Username\n ";
cin >> usernamecheck ;
for(int flag=0;flag<82;flag++)
{
  if(usernamecheck==usernames[a]){
    passwordcheck==password[a];
  }
  else {
  }
}
cout<<"Please enter your password\n";
....

Then I will compare password taken from user and from header file. I want to ask that point we didn't see that point on course. I have no idea how can I compare 2 char pointers. I tried to use as string but I have failed.

Upvotes: 0

Views: 55

Answers (1)

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33904

This:

char *usernamecheck;
...
cin >> usernamecheck ;

is going to be undefined behaviour. There is no memory associated with usernamecheck. You say:

I tried to use as string but i have failed.

So dont use old archaic, methods when there are shiny new c++ ones available:

std::string usernamecheck;
...
cin >> usernamecheck;
bool isUser = usernamecheck == username;

Done.

use std::string, its the bomb.

Live example.

Upvotes: 1

Related Questions