-4

Here is my code

char un [50] = "Username";
char pw [50]=  "Password";
char unapp[50];
char pwapp[50];

cout << "Username: ";
cin >> unapp;
system("CLS");
cout << "Username: ";
cout <<  unapp <<endl;
cout << "Password: ";
cin >> pwapp;
system ("pause");

if (unapp == un)
{
          cout <<"Gz" <<endl;
          system ("pause");
          }

cout << unapp <<endl;
cout << un <<endl;

system ("pause");

return 0;

For some reason it does not run the if statement even though after that I have printed both the unapp and un to see if they are the same and sure enough they are but still nothing?? However, it works if I use ints.

RubberDuck
  • 11,933
  • 4
  • 50
  • 95
  • 6
    Or, better yet, use `std::string`. – clcto Feb 06 '15 at 18:48
  • You are using C-strings. The `==` operator doesn't do anything special with C-strings, just as you cannot use it to compare a raw array of integers. Use `strncmp`. – DavidO Feb 06 '15 at 18:49

2 Answers2

2

To compare your two char*, you should use strcmp

if (strcmp (unapp ,un) != 0) {}

Another alternative is to use std::string

Jérôme
  • 8,016
  • 4
  • 29
  • 35
0

Since arrays decay into pointers, what you are doing is comparing pointers, not the contents of the arrays.

What you are actually trying to do is comparing the buffers (or strings) these pointers are pointing to. In order to do so just use strcmp as Jerome L explained, or just implement a loop that iterates over all the elements of the arrays and compares then till a '\0' terminator is found.

jbgs
  • 2,795
  • 2
  • 21
  • 28