The == operator doesn't work this way on arrays. It sees the arrays as pointers as compares the pointers (if you haven't learned about pointers yet, suffice it to say that it's comparing the locations of the arrays in memory, rather than what the arrays contain). Also, don't forget to NUL-terminate your second array. I'm not sure that
cin >>
does that for you, and you can't assume that the 5th byte in the array will be set to zero unless you make the array static. You have to set it manually. Here is my suggestion for corrections. I've highlighted lines I've changed or added.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char myPWD[5]="3373";
char myArray[5];
myArray[4] = 0;
cout<< "Enter your Pin Number: " << "\n";
cin >> myArray;
if (std::strcmp(myArray, myPWD) == 0)
{
cout<< "Thats The Pin Number! Good Job!" << "\n";
}
else
{
cout<< myArray << " is the bad Pin Number..." << "\n";
}
cin.get();
return 0;
}
|
You might notice that I changed string.h and stdio.h to cstring and cstdio respectively; standard headers in C++ don't end with a .h and headers from C also begin with a c. I also took out
system("pause");
and replaced it with
cin.get();
which waits for a keypress from the user. Please read
http://www.cplusplus.com/forum/articles/11153/ for an explanation of why you shouldn't use the system function. Lastly I added
return 0;
because, while the standard doesn't require it in the main function (the compiler will insert it automatically if you forget), it's best practice to always return a value (or throw one, but you'll learn about that when you study exceptions) at the end of a non-void function.