A Little Help?

This is very simple but i can't figure it out!
I made the pin number: 3373
But it will not let me true...

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
  #include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;
int main()
{
    char myPWD[5]="3373";
    char myArray[5];
    
    cout<< "Enter your Pin Number: " << "\n";
    cin >> myArray;
    
    if (myArray == myPWD)
    {
         cout<< "Thats The Pin Number! Good Job!" << "\n";
    }
    else 
    {
         cout<< myArray << " is the bad Pin Number..." << "\n";
    }
    
    system("pause");
    
}

Output:
1
2
3
4
    Enter your Pin Number:
    3373
    3373 is the bad Pin Number...
    P r e s s   a n y   k e y   t o   c o n t i n u e   . . .  
if (myArray == myPWD) is not comparing two strings (the char data inside the array) it is comparing two char pointers they will never be same.

if you want to use array of char then you need to replace that with if( strcmp(myPWD, myArray) ==0 )

You are using C++ so you can replace char arrays with std::string instead.
Wow, thanks for the help.
Here is the working code:
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
#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;
int main()
{
    char myPWD[5]="3373";
    char myArray[5];
    
    cout<< "Enter your Pin Number: " << "\n";
    cin >> myArray;
    
    if( strcmp(myPWD, myArray) ==0 )
    {
         cout<< "Thats The Pin Number! Good Job!" << "\n";
    }
    else 
    {
         cout<< myArray << " is the bad Pin Number..." << "\n";
    }
    
    system("pause");
    
}

Thanks again.
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.
Last edited on
Topic archived. No new replies allowed.